From 6e724a9c74ed420fccfae4efd1c2a5b2d5420d07 Mon Sep 17 00:00:00 2001 From: imperosol Date: Mon, 30 Jun 2025 18:17:29 +0200 Subject: [PATCH] extract AlertMessage to its own file --- core/static/bundled/utils/alert-message.ts | 38 +++++++++++++++++++ .../bundled/counter/counter-click-index.ts | 26 +++---------- .../bundled/counter/product-type-index.ts | 33 +++++----------- 3 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 core/static/bundled/utils/alert-message.ts diff --git a/core/static/bundled/utils/alert-message.ts b/core/static/bundled/utils/alert-message.ts new file mode 100644 index 00000000..85d72a2e --- /dev/null +++ b/core/static/bundled/utils/alert-message.ts @@ -0,0 +1,38 @@ +interface AlertParams { + success?: boolean; + duration?: number; +} + +export class AlertMessage { + public open: boolean; + public success: boolean; + public content: string; + private timeoutId?: number; + private readonly defaultDuration: number; + + constructor(params?: { defaultDuration: number }) { + this.open = false; + this.content = ""; + this.timeoutId = null; + this.defaultDuration = params?.defaultDuration ?? 2000; + } + + public display(message: string, params: AlertParams) { + this.clear(); + this.open = true; + this.content = message; + this.success = params.success ?? true; + this.timeoutId = setTimeout(() => { + this.open = false; + this.timeoutId = null; + }, params.duration ?? this.defaultDuration); + } + + public clear() { + if (this.timeoutId !== null) { + clearTimeout(this.timeoutId); + this.timeoutId = null; + } + this.open = false; + } +} diff --git a/counter/static/bundled/counter/counter-click-index.ts b/counter/static/bundled/counter/counter-click-index.ts index 99f6bdb7..7a51344b 100644 --- a/counter/static/bundled/counter/counter-click-index.ts +++ b/counter/static/bundled/counter/counter-click-index.ts @@ -1,18 +1,14 @@ import { BasketItem } from "#counter:counter/basket"; import type { CounterConfig, ErrorMessage } from "#counter:counter/types"; import type { CounterProductSelect } from "./components/counter-product-select-index.ts"; +import { AlertMessage } from "#core:utils/alert-message"; document.addEventListener("alpine:init", () => { Alpine.data("counter", (config: CounterConfig) => ({ basket: {} as Record, - errors: [], customerBalance: config.customerBalance, codeField: null as CounterProductSelect | null, - alertMessage: { - content: "", - show: false, - timeout: null, - }, + alertMessage: new AlertMessage({ defaultDuration: 2000 }), init() { // Fill the basket with the initial data @@ -77,22 +73,10 @@ document.addEventListener("alpine:init", () => { return total; }, - showAlertMessage(message: string) { - if (this.alertMessage.timeout !== null) { - clearTimeout(this.alertMessage.timeout); - } - this.alertMessage.content = message; - this.alertMessage.show = true; - this.alertMessage.timeout = setTimeout(() => { - this.alertMessage.show = false; - this.alertMessage.timeout = null; - }, 2000); - }, - addToBasketWithMessage(id: string, quantity: number) { const message = this.addToBasket(id, quantity); if (message.length > 0) { - this.showAlertMessage(message); + this.alertMessage.display(message, { success: false }); } }, @@ -109,7 +93,9 @@ document.addEventListener("alpine:init", () => { finish() { if (this.getBasketSize() === 0) { - this.showAlertMessage(gettext("You can't send an empty basket.")); + this.alertMessage.display(gettext("You can't send an empty basket."), { + success: false, + }); return; } this.$refs.basketForm.submit(); diff --git a/counter/static/bundled/counter/product-type-index.ts b/counter/static/bundled/counter/product-type-index.ts index f200e9b2..e576a583 100644 --- a/counter/static/bundled/counter/product-type-index.ts +++ b/counter/static/bundled/counter/product-type-index.ts @@ -1,15 +1,11 @@ import Alpine from "alpinejs"; import { producttypeReorder } from "#openapi"; +import { AlertMessage } from "#core:utils/alert-message"; document.addEventListener("alpine:init", () => { Alpine.data("productTypesList", () => ({ loading: false, - alertMessage: { - open: false, - success: true, - content: "", - timeout: null, - }, + alertMessage: new AlertMessage({ defaultDuration: 2000 }), async reorder(itemId: number, newPosition: number) { // The sort plugin of Alpine doesn't manage dynamic lists with x-sort @@ -41,23 +37,14 @@ document.addEventListener("alpine:init", () => { }, openAlertMessage(response: Response) { - if (response.ok) { - this.alertMessage.success = true; - this.alertMessage.content = gettext("Products types reordered!"); - } else { - this.alertMessage.success = false; - this.alertMessage.content = interpolate( - gettext("Product type reorganisation failed with status code : %d"), - [response.status], - ); - } - this.alertMessage.open = true; - if (this.alertMessage.timeout !== null) { - clearTimeout(this.alertMessage.timeout); - } - this.alertMessage.timeout = setTimeout(() => { - this.alertMessage.open = false; - }, 2000); + const success = response.ok; + const content = response.ok + ? gettext("Products types reordered!") + : interpolate( + gettext("Product type reorganisation failed with status code : %d"), + [response.status], + ); + this.alertMessage.display(content, { success: success }); this.loading = false; }, }));