diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2025-07-29 09:46:05 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2025-07-29 09:46:05 +0700 |
| commit | 077467cf53b46d8049df8b812577cd1a03011eba (patch) | |
| tree | 0dc641a9acb1237a3caca3f7f8a157a3e938c0b8 /src-migrate/utils/checkBoxState.js | |
| parent | 0d28dc8ff5fb8c5399e356ed6ecae4fce2019ca6 (diff) | |
| parent | dc31efb2fec4c7b79917324d922ae820c4b5bb50 (diff) | |
<hafid> merging new release
Diffstat (limited to 'src-migrate/utils/checkBoxState.js')
| -rw-r--r-- | src-migrate/utils/checkBoxState.js | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src-migrate/utils/checkBoxState.js b/src-migrate/utils/checkBoxState.js new file mode 100644 index 00000000..9568c321 --- /dev/null +++ b/src-migrate/utils/checkBoxState.js @@ -0,0 +1,90 @@ +/** + * State manager for checkbox updates + * Tracks global and individual checkbox update states + */ +class CheckboxUpdateState { + constructor() { + this.updateCount = 0; + this.listeners = new Set(); + this.updatingCheckboxIds = new Set(); + } + + // Global update state (for buttons quotation and checkout) + isUpdating() { + return this.updateCount > 0; + } + + // Individual checkbox state + isCheckboxUpdating(itemId) { + return this.updatingCheckboxIds.has(String(itemId)); + } + + // Start update + startUpdate(itemId = null) { + this.updateCount++; + + if (itemId !== null) { + this.updatingCheckboxIds.add(String(itemId)); + } + + this.notifyListeners(); + return this.updateCount; + } + + // End update + endUpdate(itemId = null) { + this.updateCount = Math.max(0, this.updateCount - 1); + + if (itemId !== null) { + this.updatingCheckboxIds.delete(String(itemId)); + } + + this.notifyListeners(); + return this.updateCount; + } + + // Reset all states + reset() { + this.updateCount = 0; + this.updatingCheckboxIds.clear(); + this.notifyListeners(); + } + + // Listener management + addListener(callback) { + if (typeof callback === 'function') { + this.listeners.add(callback); + // Immediate callback with current state + callback(this.isUpdating()); + } + } + + removeListener(callback) { + this.listeners.delete(callback); + } + + // Debug helpers + getUpdateCount() { + return this.updateCount; + } + + getUpdatingCheckboxIds() { + return [...this.updatingCheckboxIds]; + } + + // Private method to notify listeners + notifyListeners() { + const isUpdating = this.isUpdating(); + + this.listeners.forEach((listener) => { + try { + listener(isUpdating); + } catch (error) { + console.error('Checkbox update state listener error:', error); + } + }); + } +} + +const checkboxUpdateState = new CheckboxUpdateState(); +export default checkboxUpdateState; |
