From b8430adc501ad6b455e14defffc7665b8815ad8d Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 22 Dec 2024 13:01:31 +0100 Subject: [PATCH] Split counter-click-index.ts --- counter/static/bundled/counter/basket.ts | 25 ++++++++++ .../bundled/counter/counter-click-index.ts | 50 +------------------ counter/static/bundled/counter/types.d.ts | 23 +++++++++ package.json | 3 +- tsconfig.json | 3 +- 5 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 counter/static/bundled/counter/basket.ts create mode 100644 counter/static/bundled/counter/types.d.ts diff --git a/counter/static/bundled/counter/basket.ts b/counter/static/bundled/counter/basket.ts new file mode 100644 index 00000000..34d244e1 --- /dev/null +++ b/counter/static/bundled/counter/basket.ts @@ -0,0 +1,25 @@ +import type { Product } from "#counter:counter/types"; + +export class BasketItem { + quantity: number; + product: Product; + quantityForTrayPrice: number; + errors: string[]; + + constructor(product: Product, quantity: number) { + this.quantity = quantity; + this.product = product; + this.errors = []; + } + + getBonusQuantity(): number { + if (!this.product.hasTrayPrice) { + return 0; + } + return Math.floor(this.quantity / this.product.quantityForTrayPrice); + } + + sum(): number { + return (this.quantity - this.getBonusQuantity()) * this.product.price; + } +} diff --git a/counter/static/bundled/counter/counter-click-index.ts b/counter/static/bundled/counter/counter-click-index.ts index f7886bee..46f61860 100644 --- a/counter/static/bundled/counter/counter-click-index.ts +++ b/counter/static/bundled/counter/counter-click-index.ts @@ -1,52 +1,6 @@ import { exportToHtml } from "#core:utils/globals"; - -interface InitialFormData { - /* Used to refill the form when the backend raises an error */ - id?: Pick; - quantity?: number; - errors?: string[]; -} - -interface CounterConfig { - customerBalance: number; - customerId: number; - products: Record; - formInitial: InitialFormData[]; - cancelUrl: string; -} - -interface Product { - id: string; - code: string; - name: string; - price: number; - hasTrayPrice: boolean; - quantityForTrayPrice: number; -} - -class BasketItem { - quantity: number; - product: Product; - quantityForTrayPrice: number; - errors: string[]; - - constructor(product: Product, quantity: number) { - this.quantity = quantity; - this.product = product; - this.errors = []; - } - - getBonusQuantity(): number { - if (!this.product.hasTrayPrice) { - return 0; - } - return Math.floor(this.quantity / this.product.quantityForTrayPrice); - } - - sum(): number { - return (this.quantity - this.getBonusQuantity()) * this.product.price; - } -} +import { BasketItem } from "#counter:counter/basket"; +import type { CounterConfig } from "#counter:counter/types"; exportToHtml("loadCounter", (config: CounterConfig) => { document.addEventListener("alpine:init", () => { diff --git a/counter/static/bundled/counter/types.d.ts b/counter/static/bundled/counter/types.d.ts new file mode 100644 index 00000000..b28cb01a --- /dev/null +++ b/counter/static/bundled/counter/types.d.ts @@ -0,0 +1,23 @@ +export interface InitialFormData { + /* Used to refill the form when the backend raises an error */ + id?: keyof Record; + quantity?: number; + errors?: string[]; +} + +export interface CounterConfig { + customerBalance: number; + customerId: number; + products: Record; + formInitial: InitialFormData[]; + cancelUrl: string; +} + +export interface Product { + id: string; + code: string; + name: string; + price: number; + hasTrayPrice: boolean; + quantityForTrayPrice: number; +} diff --git a/package.json b/package.json index 77572a6f..9721eea4 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "imports": { "#openapi": "./staticfiles/generated/openapi/index.ts", "#core:*": "./core/static/bundled/*", - "#pedagogy:*": "./pedagogy/static/bundled/*" + "#pedagogy:*": "./pedagogy/static/bundled/*", + "#counter:*": "./counter/static/bundled/*" }, "devDependencies": { "@babel/core": "^7.25.2", diff --git a/tsconfig.json b/tsconfig.json index 6edae1f7..7b3be5fc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,8 @@ "paths": { "#openapi": ["./staticfiles/generated/openapi/index.ts"], "#core:*": ["./core/static/bundled/*"], - "#pedagogy:*": ["./pedagogy/static/bundled/*"] + "#pedagogy:*": ["./pedagogy/static/bundled/*"], + "#counter:*": ["./counter/static/bundled/*"] } } }