useEntityLookup: fix lookup redirect

This commit is contained in:
Alex Gleason 2024-04-06 20:45:54 -05:00
parent 2c9bdf03a4
commit 00aa373997
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -25,10 +25,11 @@ function useEntityLookup<TEntity extends Entity>(
const { schema = z.custom<TEntity>() } = opts;
const dispatch = useAppDispatch();
const [fetchedEntity, setFetchedEntity] = useState<TEntity | undefined>();
const [isFetching, setPromise] = useLoading(true);
const [error, setError] = useState<unknown>();
const entity = useAppSelector(state => findEntity(state, entityType, lookupFn));
const entity = useAppSelector(state => findEntity(state, entityType, lookupFn) ?? fetchedEntity);
const isEnabled = opts.enabled ?? true;
const isLoading = isFetching && !entity;
@ -36,6 +37,7 @@ function useEntityLookup<TEntity extends Entity>(
try {
const response = await setPromise(entityFn());
const entity = schema.parse(response.data);
setFetchedEntity(entity);
dispatch(importEntities([entity], entityType));
} catch (e) {
setError(e);