2023-06-23 12:12:12 -07:00
|
|
|
import type { EntitiesTransaction, Entity, EntityListState, ImportPosition } 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;
|
2023-03-22 17:45:02 -07:00
|
|
|
const ENTITIES_INCREMENT = 'ENTITIES_INCREMENT' 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;
|
2023-06-23 12:12:12 -07:00
|
|
|
const ENTITIES_TRANSACTION = 'ENTITIES_TRANSACTION' as const;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
/** Action to import entities into the cache. */
|
2024-05-12 16:18:04 -07:00
|
|
|
const importEntities = (
|
|
|
|
entities: Entity[],
|
|
|
|
entityType: string,
|
|
|
|
listKey?: string,
|
|
|
|
pos?: ImportPosition,
|
|
|
|
) => ({
|
|
|
|
type: ENTITIES_IMPORT,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
|
|
|
pos,
|
|
|
|
});
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2023-03-15 14:52:09 -07:00
|
|
|
interface DeleteEntitiesOpts {
|
2023-10-02 11:54:02 -07:00
|
|
|
preserveLists?: boolean;
|
2023-03-15 14:52:09 -07:00
|
|
|
}
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const deleteEntities = (ids: Iterable<string>, entityType: string, opts: DeleteEntitiesOpts = {}) => ({
|
|
|
|
type: ENTITIES_DELETE,
|
|
|
|
ids,
|
|
|
|
entityType,
|
|
|
|
opts,
|
|
|
|
});
|
2023-03-14 12:13:49 -07:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const dismissEntities = (ids: Iterable<string>, entityType: string, listKey: string) => ({
|
|
|
|
type: ENTITIES_DISMISS,
|
|
|
|
ids,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
});
|
2023-03-22 12:34:10 -07:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const incrementEntities = (entityType: string, listKey: string, diff: number) => ({
|
|
|
|
type: ENTITIES_INCREMENT,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
diff,
|
|
|
|
});
|
2023-03-22 17:45:02 -07:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const entitiesFetchRequest = (entityType: string, listKey?: string) => ({
|
|
|
|
type: ENTITIES_FETCH_REQUEST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
});
|
2022-12-04 15:26:28 -08:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const entitiesFetchSuccess = (
|
2023-03-22 16:47:10 -07:00
|
|
|
entities: Entity[],
|
|
|
|
entityType: string,
|
|
|
|
listKey?: string,
|
2023-04-11 08:04:31 -07:00
|
|
|
pos?: ImportPosition,
|
2023-03-22 16:47:10 -07:00
|
|
|
newState?: EntityListState,
|
|
|
|
overwrite = false,
|
2024-05-12 16:18:04 -07:00
|
|
|
) => ({
|
|
|
|
type: ENTITIES_FETCH_SUCCESS,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
|
|
|
pos,
|
|
|
|
newState,
|
|
|
|
overwrite,
|
|
|
|
});
|
2022-12-04 15:26:28 -08:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const entitiesFetchFail = (entityType: string, listKey: string | undefined, error: any) => ({
|
|
|
|
type: ENTITIES_FETCH_FAIL,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
error,
|
|
|
|
});
|
2022-12-04 15:26:28 -08:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const invalidateEntityList = (entityType: string, listKey: string) => ({
|
|
|
|
type: ENTITIES_INVALIDATE_LIST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
});
|
2023-03-22 16:17:28 -07:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const entitiesTransaction = (transaction: EntitiesTransaction) => ({
|
|
|
|
type: ENTITIES_TRANSACTION,
|
|
|
|
transaction,
|
|
|
|
});
|
2023-06-23 12:12:12 -07:00
|
|
|
|
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>
|
2023-03-22 17:45:02 -07:00
|
|
|
| ReturnType<typeof incrementEntities>
|
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>
|
2023-06-23 12:12:12 -07:00
|
|
|
| ReturnType<typeof invalidateEntityList>
|
|
|
|
| ReturnType<typeof entitiesTransaction>;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
export {
|
2024-05-13 10:00:42 -07:00
|
|
|
type DeleteEntitiesOpts,
|
|
|
|
type EntityAction,
|
2022-12-04 14:58:13 -08:00
|
|
|
ENTITIES_IMPORT,
|
2023-03-14 12:13:49 -07:00
|
|
|
ENTITIES_DELETE,
|
2023-03-22 12:34:10 -07:00
|
|
|
ENTITIES_DISMISS,
|
2023-03-22 17:45:02 -07:00
|
|
|
ENTITIES_INCREMENT,
|
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,
|
2023-06-23 12:12:12 -07:00
|
|
|
ENTITIES_TRANSACTION,
|
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,
|
2023-03-22 17:45:02 -07:00
|
|
|
incrementEntities,
|
2022-12-04 15:26:28 -08:00
|
|
|
entitiesFetchRequest,
|
|
|
|
entitiesFetchSuccess,
|
|
|
|
entitiesFetchFail,
|
2023-03-22 16:17:28 -07:00
|
|
|
invalidateEntityList,
|
2023-06-23 12:12:12 -07:00
|
|
|
entitiesTransaction,
|
2023-03-15 14:52:09 -07:00
|
|
|
};
|