diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-05-31 21:39:53 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-05-31 21:39:53 +0700 |
| commit | 77f976d3bd09d9e00d4d55bbd40579b439405d96 (patch) | |
| tree | a8959bba3d8a51570c439789f92653409a0065ae /src-migrate/utils/checkBoxState.js | |
| parent | ca05a70e98e9066882de6394ffbd89db7af2cb9d (diff) | |
| parent | 2a1dea70b8f0062fe8eebeb7139a7b77a24e220b (diff) | |
Merge branch 'new-release' of https://bitbucket.org/altafixco/next-indoteknik into biteship-merge
# Conflicts:
# src/lib/checkout/components/Checkout.jsx
# src/lib/transaction/components/Transaction.jsx
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; |
