Update Entity Store with bug fixes
This commit is contained in:
parent
283935e837
commit
20ccd26a6e
3 changed files with 8 additions and 3 deletions
|
@ -131,7 +131,12 @@ function useEntities<TEntity extends Entity>(
|
||||||
const selectCache = (state: RootState, path: EntityPath) => state.entities[path[0]];
|
const selectCache = (state: RootState, path: EntityPath) => state.entities[path[0]];
|
||||||
|
|
||||||
/** Get list at path from Redux. */
|
/** Get list at path from Redux. */
|
||||||
const selectList = (state: RootState, path: EntityPath) => selectCache(state, path)?.lists[path[1]];
|
const selectList = (state: RootState, path: EntityPath) => {
|
||||||
|
const [, ...listKeys] = path;
|
||||||
|
const listKey = listKeys.join(':');
|
||||||
|
|
||||||
|
return selectCache(state, path)?.lists[listKey];
|
||||||
|
};
|
||||||
|
|
||||||
/** Select a particular item from a list state. */
|
/** Select a particular item from a list state. */
|
||||||
function selectListState<K extends keyof EntityListState>(state: RootState, path: EntityPath, key: K) {
|
function selectListState<K extends keyof EntityListState>(state: RootState, path: EntityPath, key: K) {
|
||||||
|
|
|
@ -39,7 +39,7 @@ function useEntity<TEntity extends Entity>(
|
||||||
const fetchEntity = () => {
|
const fetchEntity = () => {
|
||||||
setIsFetching(true);
|
setIsFetching(true);
|
||||||
api.get(endpoint).then(({ data }) => {
|
api.get(endpoint).then(({ data }) => {
|
||||||
const entity = schema.parse(data);
|
const entity = schema.parse(Array.isArray(data) ? data[0] : data);
|
||||||
dispatch(importEntities([entity], entityType));
|
dispatch(importEntities([entity], entityType));
|
||||||
setIsFetching(false);
|
setIsFetching(false);
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
|
|
|
@ -65,7 +65,7 @@ function useEntityActions<TEntity extends Entity = Entity, P = any>(
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
createEntity: endpoints.post ? createEntity : undefined,
|
createEntity: createEntity,
|
||||||
deleteEntity: endpoints.delete ? deleteEntity : undefined,
|
deleteEntity: endpoints.delete ? deleteEntity : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue