diff --git a/src/hooks/useFetchStores.tsx b/src/hooks/useFetchStores.tsx index 45afef2..d44604b 100644 --- a/src/hooks/useFetchStores.tsx +++ b/src/hooks/useFetchStores.tsx @@ -1,6 +1,7 @@ import React from "react"; import { useDispatch, useSelector } from "react-redux"; import { addToHashMapStores } from "../state/features/storeSlice"; +import { removeFromHashMapStores } from "../state/features/storeSlice"; import { RootState } from "../state/store"; import { fetchAndEvaluateProducts } from "../utils/fetchPosts"; @@ -22,9 +23,12 @@ export const useFetchStores = () => { storeId, content }); - - dispatch(addToHashMapStores(res)); - return res; + if (res?.isValid) { + dispatch(addToHashMapStores(res)); + return res; + } else { + dispatch(removeFromHashMapStores(storeId)); + } }; const checkAndUpdateResource = React.useCallback( diff --git a/src/pages/StoreList/StoreList.tsx b/src/pages/StoreList/StoreList.tsx index 9aae277..7f0ff94 100644 --- a/src/pages/StoreList/StoreList.tsx +++ b/src/pages/StoreList/StoreList.tsx @@ -115,12 +115,10 @@ export const StoreList = () => { // Memoize the filtered stores to prevent rerenders const filteredStores = useMemo(() => { - if (filterUserStores) { - return myStores; - } else { - return stores; - } - }, [filterUserStores, stores, myStores, user?.name]); + let filtered = filterUserStores ? myStores : stores; + filtered = filtered.filter((store: Store) => hashMapStores[store.id]?.isValid); + return filtered; + }, [filterUserStores, stores, myStores, user?.name, hashMapStores]); return ( <> diff --git a/src/state/features/storeSlice.ts b/src/state/features/storeSlice.ts index 553b0c0..cb71612 100644 --- a/src/state/features/storeSlice.ts +++ b/src/state/features/storeSlice.ts @@ -150,6 +150,10 @@ export const storeSlice = createSlice({ const store = action.payload; state.hashMapStores[store?.id] = store; }, + removeFromHashMapStores: (state, action) => { + const storeId = action.payload; + delete state.hashMapStores[storeId]; + }, addToHashMapStoreReviews: (state, action) => { const review = action.payload; state.hashMapStoreReviews[review.id] = review; @@ -267,6 +271,7 @@ export const { upsertPostsBeginning, upsertFilteredPosts, addToHashMapStores, + removeFromHashMapStores, setStoreId, setStoreOwner, upsertStores,