diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-05-26 20:00:17 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-05-26 20:00:17 +0700 |
| commit | 3feaad9127ff429b27f0eb69fa6ea539de2f2e8c (patch) | |
| tree | d2b65790861531e08fd9eb3e1d1cd64eb5805e15 /src-migrate/utils | |
| parent | cca6d803fc4db729865def23004ab1c4bd279e24 (diff) | |
<miqdad> Cleaning code
Diffstat (limited to 'src-migrate/utils')
| -rw-r--r-- | src-migrate/utils/cart.js | 40 | ||||
| -rw-r--r-- | src-migrate/utils/checkBoxState.js | 141 |
2 files changed, 112 insertions, 69 deletions
diff --git a/src-migrate/utils/cart.js b/src-migrate/utils/cart.js index ebd771e5..4bdee49a 100644 --- a/src-migrate/utils/cart.js +++ b/src-migrate/utils/cart.js @@ -413,3 +413,43 @@ export const removeSelectedItemsFromCookie = (productIds) => { return {}; } }; + +class QuantityUpdateState { + constructor() { + this.updateItems = new Set(); + this.listeners = new Set(); + } + + startUpdate(itemId) { + this.updateItems.add(itemId); + this.notifyListeners(); + } + + endUpdate(itemId) { + this.updateItems.delete(itemId); + this.notifyListeners(); + } + + isAnyQuantityUpdating() { + return this.updateItems.size > 0; + } + + isItemUpdating(itemId) { + return this.updateItems.has(itemId); + } + + addListener(callback) { + this.listeners.add(callback); + } + + removeListener(callback) { + this.listeners.delete(callback); + } + + notifyListeners() { + const isUpdating = this.isAnyQuantityUpdating(); + this.listeners.forEach(callback => callback(isUpdating)); + } +} + +export const quantityUpdateState = new QuantityUpdateState();
\ No newline at end of file diff --git a/src-migrate/utils/checkBoxState.js b/src-migrate/utils/checkBoxState.js index 8f7236c3..9568c321 100644 --- a/src-migrate/utils/checkBoxState.js +++ b/src-migrate/utils/checkBoxState.js @@ -1,87 +1,90 @@ /** - * Enhanced state manager for checkbox updates - * Tracks both global update state and individual checkbox update states + * 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++; -// Track the number of ongoing updates -let updateCount = 0; -let listeners = []; - -// Track which checkboxes are currently updating by ID -let updatingCheckboxIds = new Set(); - -const checkboxUpdateState = { - // Check if any checkboxes are currently updating (for buttons quotation and checkout) - isUpdating: () => updateCount > 0, - - // Check if a specific checkbox is updating (for disabling just that checkbox) - isCheckboxUpdating: (itemId) => updatingCheckboxIds.has(itemId.toString()), - - // Start an update for a specific checkbox - startUpdate: (itemId = null) => { - updateCount++; - - // If an item ID is provided, mark it as updating if (itemId !== null) { - updatingCheckboxIds.add(itemId.toString()); + this.updatingCheckboxIds.add(String(itemId)); } - notifyListeners(); - return updateCount; - }, + this.notifyListeners(); + return this.updateCount; + } - // End an update for a specific checkbox - endUpdate: (itemId = null) => { - updateCount = Math.max(0, updateCount - 1); + // End update + endUpdate(itemId = null) { + this.updateCount = Math.max(0, this.updateCount - 1); - // If an item ID is provided, remove it from updating set if (itemId !== null) { - updatingCheckboxIds.delete(itemId.toString()); + this.updatingCheckboxIds.delete(String(itemId)); } - notifyListeners(); - return updateCount; - }, - - // Reset the update counter and clear all updating checkboxes - reset: () => { - updateCount = 0; - updatingCheckboxIds.clear(); - notifyListeners(); - }, + this.notifyListeners(); + return this.updateCount; + } - // Get IDs of all checkboxes currently updating (for debugging) - // getUpdatingCheckboxIds: () => [...updatingCheckboxIds], + // Reset all states + reset() { + this.updateCount = 0; + this.updatingCheckboxIds.clear(); + this.notifyListeners(); + } - // Add a listener function to be called when update state changes - addListener: (callback) => { + // Listener management + addListener(callback) { if (typeof callback === 'function') { - listeners.push(callback); - - // Immediately call with current state - callback(updateCount > 0); - } - }, - - // Remove a listener - removeListener: (callback) => { - listeners = listeners.filter((listener) => listener !== callback); - }, - - // Get current counter (for debugging) - getUpdateCount: () => updateCount, -}; - -// Private function to notify all listeners of state changes -function notifyListeners() { - const isUpdating = updateCount > 0; - listeners.forEach((listener) => { - try { - listener(isUpdating); - } catch (error) { - console.error('Error in checkbox update state listener:', error); + 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; |
