Răsfoiți Sursa

fix: handle store_sync_config as array in autosync toggle #100

Fixed TypeError: U.map is not a function when toggling autosync on the
webshops page. The issue was that store_sync_config was not always being
treated as an array in the optimistic UI update, causing .map() to fail.

- Ensure store_sync_config is always properly initialized as an array
- Add proper fallback with all required properties (enabled, sync_frequency)
- Fix both the optimistic update and error revert handlers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Claude 5 luni în urmă
părinte
comite
8245e51d49
1 a modificat fișierele cu 16 adăugiri și 6 ștergeri
  1. 16 6
      shopcall.ai-main/src/components/IntegrationsContent.tsx

+ 16 - 6
shopcall.ai-main/src/components/IntegrationsContent.tsx

@@ -350,12 +350,17 @@ export function IntegrationsContent() {
     // Optimistic UI update - immediately update the local state
     setConnectedShops(prev => prev.map(shop => {
       if (shop.id === storeId) {
+        // Ensure store_sync_config is always an array with proper structure
+        const currentConfig = Array.isArray(shop.store_sync_config) && shop.store_sync_config.length > 0
+          ? shop.store_sync_config[0]
+          : { enabled: false, sync_frequency: 'hourly' };
+
         return {
           ...shop,
-          store_sync_config: shop.store_sync_config?.map(config => ({
-            ...config,
+          store_sync_config: [{
+            ...currentConfig,
             enabled: newEnabled
-          })) || [{ enabled: newEnabled }]
+          }]
         };
       }
       return shop;
@@ -399,12 +404,17 @@ export function IntegrationsContent() {
       // Revert the optimistic update on error
       setConnectedShops(prev => prev.map(shop => {
         if (shop.id === storeId) {
+          // Ensure store_sync_config is always an array with proper structure
+          const existingConfig = Array.isArray(shop.store_sync_config) && shop.store_sync_config.length > 0
+            ? shop.store_sync_config[0]
+            : { enabled: false, sync_frequency: 'hourly' };
+
           return {
             ...shop,
-            store_sync_config: shop.store_sync_config?.map(config => ({
-              ...config,
+            store_sync_config: [{
+              ...existingConfig,
               enabled: currentEnabled // Revert to original value
-            })) || [{ enabled: currentEnabled }]
+            }]
           };
         }
         return shop;