useEntityLookup: fix lookup redirect
This commit is contained in:
parent
2c9bdf03a4
commit
00aa373997
1 changed files with 3 additions and 1 deletions
|
@ -25,10 +25,11 @@ function useEntityLookup<TEntity extends Entity>(
|
||||||
const { schema = z.custom<TEntity>() } = opts;
|
const { schema = z.custom<TEntity>() } = opts;
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
const [fetchedEntity, setFetchedEntity] = useState<TEntity | undefined>();
|
||||||
const [isFetching, setPromise] = useLoading(true);
|
const [isFetching, setPromise] = useLoading(true);
|
||||||
const [error, setError] = useState<unknown>();
|
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 isEnabled = opts.enabled ?? true;
|
||||||
const isLoading = isFetching && !entity;
|
const isLoading = isFetching && !entity;
|
||||||
|
|
||||||
|
@ -36,6 +37,7 @@ function useEntityLookup<TEntity extends Entity>(
|
||||||
try {
|
try {
|
||||||
const response = await setPromise(entityFn());
|
const response = await setPromise(entityFn());
|
||||||
const entity = schema.parse(response.data);
|
const entity = schema.parse(response.data);
|
||||||
|
setFetchedEntity(entity);
|
||||||
dispatch(importEntities([entity], entityType));
|
dispatch(importEntities([entity], entityType));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e);
|
setError(e);
|
||||||
|
|
Loading…
Reference in a new issue