summaryrefslogtreecommitdiff
path: root/src-migrate/utils/checkBoxState.js
diff options
context:
space:
mode:
Diffstat (limited to 'src-migrate/utils/checkBoxState.js')
-rw-r--r--src-migrate/utils/checkBoxState.js141
1 files changed, 72 insertions, 69 deletions
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;