Skip to content

Commit

Permalink
Split counter-click-index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
klmp200 committed Dec 22, 2024
1 parent eed434a commit b8430ad
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 50 deletions.
25 changes: 25 additions & 0 deletions counter/static/bundled/counter/basket.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 2 additions & 48 deletions counter/static/bundled/counter/counter-click-index.ts
Original file line number Diff line number Diff line change
@@ -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<Product, "id">;
quantity?: number;
errors?: string[];
}

interface CounterConfig {
customerBalance: number;
customerId: number;
products: Record<string, Product>;
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", () => {
Expand Down
23 changes: 23 additions & 0 deletions counter/static/bundled/counter/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface InitialFormData {
/* Used to refill the form when the backend raises an error */
id?: keyof Record<string, Product>;
quantity?: number;
errors?: string[];
}

export interface CounterConfig {
customerBalance: number;
customerId: number;
products: Record<string, Product>;
formInitial: InitialFormData[];
cancelUrl: string;
}

export interface Product {
id: string;
code: string;
name: string;
price: number;
hasTrayPrice: boolean;
quantityForTrayPrice: number;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]
}
}
}

0 comments on commit b8430ad

Please sign in to comment.