From ab63ba1c541b03fd8b6b0a66ed321cb7d3def734 Mon Sep 17 00:00:00 2001 From: Sli Date: Mon, 21 Oct 2024 13:26:11 +0200 Subject: [PATCH] Remove ajax_select from accounting --- .../components/ajax-select-index.ts | 60 +++++++++++++++++++ .../templates/accounting/operation_edit.jinja | 8 +-- accounting/views.py | 45 +++++++++++--- accounting/widgets/select.py | 45 ++++++++++++++ 4 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 accounting/static/webpack/accounting/components/ajax-select-index.ts create mode 100644 accounting/widgets/select.py diff --git a/accounting/static/webpack/accounting/components/ajax-select-index.ts b/accounting/static/webpack/accounting/components/ajax-select-index.ts new file mode 100644 index 00000000..3fc93cf3 --- /dev/null +++ b/accounting/static/webpack/accounting/components/ajax-select-index.ts @@ -0,0 +1,60 @@ +import { AjaxSelect } from "#core:core/components/ajax-select-base"; +import { registerComponent } from "#core:utils/web-components"; +import type { TomOption } from "tom-select/dist/types/types"; +import type { escape_html } from "tom-select/dist/types/utils"; +import { + type ClubAccountSchema, + type CompanySchema, + accountingSearchClubAccount, + accountingSearchCompany, +} from "#openapi"; + +@registerComponent("club-account-ajax-select") +export class ClubAccountAjaxSelect extends AjaxSelect { + protected valueField = "id"; + protected labelField = "name"; + protected searchField = ["code", "name"]; + + protected async search(query: string): Promise { + const resp = await accountingSearchClubAccount({ query: { search: query } }); + if (resp.data) { + return resp.data.results; + } + return []; + } + + protected renderOption(item: ClubAccountSchema, sanitize: typeof escape_html) { + return `
+ ${sanitize(item.name)} +
`; + } + + protected renderItem(item: ClubAccountSchema, sanitize: typeof escape_html) { + return `${sanitize(item.name)}`; + } +} + +@registerComponent("company-ajax-select") +export class CompanyAjaxSelect extends AjaxSelect { + protected valueField = "id"; + protected labelField = "name"; + protected searchField = ["code", "name"]; + + protected async search(query: string): Promise { + const resp = await accountingSearchCompany({ query: { search: query } }); + if (resp.data) { + return resp.data.results; + } + return []; + } + + protected renderOption(item: CompanySchema, sanitize: typeof escape_html) { + return `
+ ${sanitize(item.name)} +
`; + } + + protected renderItem(item: CompanySchema, sanitize: typeof escape_html) { + return `${sanitize(item.name)}`; + } +} diff --git a/accounting/templates/accounting/operation_edit.jinja b/accounting/templates/accounting/operation_edit.jinja index e8c34364..4a75cb83 100644 --- a/accounting/templates/accounting/operation_edit.jinja +++ b/accounting/templates/accounting/operation_edit.jinja @@ -61,10 +61,10 @@