EntityStore: allow passing an undefined endpoint (to skip fetch), prevent race conditions in isFetching

environments/review-group-enti-e2lbwq/deployments/2781
Alex Gleason 2 years ago
parent a3b1f541bc
commit ad583c89f8
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -6,6 +6,7 @@ import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
import { entitiesFetchFail, entitiesFetchRequest, entitiesFetchSuccess } from '../actions';
import type { Entity } from '../types';
import type { RootState } from 'soapbox/store';
/** Tells us where to find/store the entity in the cache. */
type EntityPath = [
@ -30,8 +31,8 @@ interface UseEntitiesOpts<TEntity> {
function useEntities<TEntity extends Entity>(
/** Tells us where to find/store the entity in the cache. */
path: EntityPath,
/** API route to GET, eg `'/api/v1/notifications'` */
endpoint: string,
/** API route to GET, eg `'/api/v1/notifications'`. If undefined, nothing will be fetched. */
endpoint: string | undefined,
/** Additional options for the hook. */
opts: UseEntitiesOpts<TEntity> = {},
) {
@ -64,6 +65,10 @@ function useEntities<TEntity extends Entity>(
const hasPreviousPage = Boolean(list?.state.prev);
const fetchPage = async(url: string): Promise<void> => {
// Get `isFetching` state from the store again to prevent race conditions.
const isFetching = dispatch((_, getState: () => RootState) => Boolean(getState().entities[entityType]?.lists[listKey]?.state.fetching));
if (isFetching) return;
dispatch(entitiesFetchRequest(entityType, listKey));
try {
const response = await api.get(url);
@ -80,7 +85,9 @@ function useEntities<TEntity extends Entity>(
};
const fetchEntities = async(): Promise<void> => {
await fetchPage(endpoint);
if (endpoint) {
await fetchPage(endpoint);
}
};
const fetchNextPage = async(): Promise<void> => {

Loading…
Cancel
Save