2022-12-04 15:53:56 -08:00
|
|
|
import type { Entity, EntityListState } from './types';
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2022-12-04 15:26:28 -08:00
|
|
|
const ENTITIES_IMPORT = 'ENTITIES_IMPORT' as const;
|
2023-03-14 12:13:49 -07:00
|
|
|
const ENTITIES_DELETE = 'ENTITIES_DELETE' as const;
|
2023-03-22 12:34:10 -07:00
|
|
|
const ENTITIES_DISMISS = 'ENTITIES_DISMISS' as const;
|
2022-12-04 15:26:28 -08:00
|
|
|
const ENTITIES_FETCH_REQUEST = 'ENTITIES_FETCH_REQUEST' as const;
|
|
|
|
const ENTITIES_FETCH_SUCCESS = 'ENTITIES_FETCH_SUCCESS' as const;
|
|
|
|
const ENTITIES_FETCH_FAIL = 'ENTITIES_FETCH_FAIL' as const;
|
2023-03-22 16:17:28 -07:00
|
|
|
const ENTITIES_INVALIDATE_LIST = 'ENTITIES_INVALIDATE_LIST' as const;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
/** Action to import entities into the cache. */
|
|
|
|
function importEntities(entities: Entity[], entityType: string, listKey?: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_IMPORT,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:52:09 -07:00
|
|
|
interface DeleteEntitiesOpts {
|
|
|
|
preserveLists?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEntities(ids: Iterable<string>, entityType: string, opts: DeleteEntitiesOpts = {}) {
|
2023-03-14 12:13:49 -07:00
|
|
|
return {
|
|
|
|
type: ENTITIES_DELETE,
|
|
|
|
ids,
|
|
|
|
entityType,
|
2023-03-15 14:52:09 -07:00
|
|
|
opts,
|
2023-03-14 12:13:49 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:34:10 -07:00
|
|
|
function dismissEntities(ids: Iterable<string>, entityType: string, listKey: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_DISMISS,
|
|
|
|
ids,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:26:28 -08:00
|
|
|
function entitiesFetchRequest(entityType: string, listKey?: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_REQUEST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:47:10 -07:00
|
|
|
function entitiesFetchSuccess(
|
|
|
|
entities: Entity[],
|
|
|
|
entityType: string,
|
|
|
|
listKey?: string,
|
|
|
|
newState?: EntityListState,
|
|
|
|
overwrite = false,
|
|
|
|
) {
|
2022-12-04 15:26:28 -08:00
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_SUCCESS,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
2022-12-04 15:53:56 -08:00
|
|
|
newState,
|
2023-03-22 16:47:10 -07:00
|
|
|
overwrite,
|
2022-12-04 15:26:28 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:53:56 -08:00
|
|
|
function entitiesFetchFail(entityType: string, listKey: string | undefined, error: any) {
|
2022-12-04 15:26:28 -08:00
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_FAIL,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
2022-12-04 15:53:56 -08:00
|
|
|
error,
|
2022-12-04 15:26:28 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:17:28 -07:00
|
|
|
function invalidateEntityList(entityType: string, listKey: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_INVALIDATE_LIST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:58:13 -08:00
|
|
|
/** Any action pertaining to entities. */
|
2022-12-04 15:26:28 -08:00
|
|
|
type EntityAction =
|
|
|
|
ReturnType<typeof importEntities>
|
2023-03-14 12:13:49 -07:00
|
|
|
| ReturnType<typeof deleteEntities>
|
2023-03-22 12:34:10 -07:00
|
|
|
| ReturnType<typeof dismissEntities>
|
2022-12-04 15:26:28 -08:00
|
|
|
| ReturnType<typeof entitiesFetchRequest>
|
|
|
|
| ReturnType<typeof entitiesFetchSuccess>
|
2023-03-22 16:17:28 -07:00
|
|
|
| ReturnType<typeof entitiesFetchFail>
|
|
|
|
| ReturnType<typeof invalidateEntityList>;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
export {
|
|
|
|
ENTITIES_IMPORT,
|
2023-03-14 12:13:49 -07:00
|
|
|
ENTITIES_DELETE,
|
2023-03-22 12:34:10 -07:00
|
|
|
ENTITIES_DISMISS,
|
2022-12-04 15:26:28 -08:00
|
|
|
ENTITIES_FETCH_REQUEST,
|
|
|
|
ENTITIES_FETCH_SUCCESS,
|
|
|
|
ENTITIES_FETCH_FAIL,
|
2023-03-22 16:17:28 -07:00
|
|
|
ENTITIES_INVALIDATE_LIST,
|
2022-12-04 14:58:13 -08:00
|
|
|
importEntities,
|
2023-03-14 12:13:49 -07:00
|
|
|
deleteEntities,
|
2023-03-22 12:34:10 -07:00
|
|
|
dismissEntities,
|
2022-12-04 15:26:28 -08:00
|
|
|
entitiesFetchRequest,
|
|
|
|
entitiesFetchSuccess,
|
|
|
|
entitiesFetchFail,
|
2023-03-22 16:17:28 -07:00
|
|
|
invalidateEntityList,
|
2022-12-04 14:58:13 -08:00
|
|
|
EntityAction,
|
2023-03-15 14:52:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export type { DeleteEntitiesOpts };
|