diff --git a/src/App.jsx b/src/App.jsx index 5a25e4d..451ae6d 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -70,8 +70,8 @@ function App() { const handleClientChange = (clientId) => { setSelectedClient(clientId); - // Optionally reset store when client changes if they are not compatible - // setSelectedStore(null); + // Reset store when client changes to avoid country mismatch + setSelectedStore(null); }; const handleExecute = async (sql) => { diff --git a/src/components/StoreSelector.jsx b/src/components/StoreSelector.jsx index a8aabb1..566b9ae 100644 --- a/src/components/StoreSelector.jsx +++ b/src/components/StoreSelector.jsx @@ -28,6 +28,7 @@ export default function StoreSelector({ selectedStore, onSelect, onClientChange const loadStoresWithStatus = async () => { setLoading(true); setError(null); + setStores([]); // IMPORTANT: Clear stores list when loading new data/client try { // Fetch both store list and status in parallel @@ -36,19 +37,27 @@ export default function StoreSelector({ selectedStore, onSelect, onClientChange fetchStoreStatus(selectedClient) ]); - // Merge store data with status information - const mergedStores = storeList.map(store => { + // Merge store data with status information and DE-DUPLICATE by ID + const storeMap = new Map(); + + storeList.forEach(store => { const statusInfo = statusData.find(s => s.store_id === parseInt(store.id)); - return { + const mergedStore = { ...store, connectivity: statusInfo?.connectivity || 'OFFLINE', last_ping: statusInfo?.last_ping, seconds_since_ping: statusInfo?.seconds_since_ping, status: statusInfo?.connectivity === 'ONLINE' ? 'online' : 'offline' }; + + // If the store ID isn't in our Map yet, add it + // This handles cases where the API returns the same store ID twice + if (!storeMap.has(store.id)) { + storeMap.set(store.id, mergedStore); + } }); - setStores(mergedStores); + setStores(Array.from(storeMap.values())); } catch (err) { setError("Error cargando tiendas: " + err.message); } finally {