mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
fix(phone/marketplace): fix state dispatch when selector is loading
Should fix edge cases where broadcast events are received and processed before the original fetch promise resolves
This commit is contained in:
1 parent
44599c882d
commit
4b724dfa73
2 files changed
+14
-4
No files matched your search
@@ -26,7 +26,7 @@ const defaultData: MarketplaceListing[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const listingState = atom<MarketplaceListing[]>({
|
export const listingState = atom<MarketplaceListing[]>({
|
||||||
key: 'listings',
|
key: 'listings',
|
||||||
default: selector({
|
default: selector({
|
||||||
key: 'defaultListings',
|
key: 'defaultListings',
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { useSetListings } from './state';
|
import { listingState, useSetListings } from './state';
|
||||||
import { MarketplaceListing } from '../../../../../typings/marketplace';
|
import { MarketplaceListing } from '../../../../../typings/marketplace';
|
||||||
|
import { useRecoilValueLoadable } from 'recoil';
|
||||||
|
|
||||||
interface MarketplaceActionValues {
|
interface MarketplaceActionValues {
|
||||||
deleteListing: (id: number) => void;
|
deleteListing: (id: number) => void;
|
||||||
@@ -8,20 +9,29 @@ interface MarketplaceActionValues {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useMarketplaceActions = (): MarketplaceActionValues => {
|
export const useMarketplaceActions = (): MarketplaceActionValues => {
|
||||||
|
const { state: valueLoading, contents } = useRecoilValueLoadable(listingState);
|
||||||
const setListings = useSetListings();
|
const setListings = useSetListings();
|
||||||
|
|
||||||
const deleteListing = useCallback(
|
const deleteListing = useCallback(
|
||||||
(id: number) => {
|
(id: number) => {
|
||||||
|
// Make sure our atom is actually loaded before we attempt a dispatch
|
||||||
|
if (valueLoading !== 'hasValue') return;
|
||||||
|
// Tts possible for the init fetch to fail and empty array is passed. In that case
|
||||||
|
// block dispatch
|
||||||
|
if (!contents.length) return;
|
||||||
|
|
||||||
setListings((curListings) => [...curListings].filter((listing) => listing.id !== id));
|
setListings((curListings) => [...curListings].filter((listing) => listing.id !== id));
|
||||||
},
|
},
|
||||||
[setListings],
|
[setListings, valueLoading, contents],
|
||||||
);
|
);
|
||||||
|
|
||||||
const addListing = useCallback(
|
const addListing = useCallback(
|
||||||
(listing: MarketplaceListing) => {
|
(listing: MarketplaceListing) => {
|
||||||
|
// Make sure our atom is actually loaded before we attempt a dispatch
|
||||||
|
if (valueLoading !== 'hasValue') return;
|
||||||
setListings((curListings) => [...curListings, listing]);
|
setListings((curListings) => [...curListings, listing]);
|
||||||
},
|
},
|
||||||
[setListings],
|
[setListings, valueLoading, contents],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { deleteListing, addListing };
|
return { deleteListing, addListing };
|
||||||
|
|||||||
Reference in new issue
Block a user