soapbox/app/soapbox/entity-store/actions.ts

126 wiersze
2.8 KiB
TypeScript
Czysty Zwykły widok Historia

2022-12-04 23:53:56 +00:00
import type { Entity, EntityListState } from './types';
2022-12-04 22:58:13 +00:00
const ENTITIES_IMPORT = 'ENTITIES_IMPORT' as const;
2023-03-14 19:13:49 +00:00
const ENTITIES_DELETE = 'ENTITIES_DELETE' as const;
const ENTITIES_DISMISS = 'ENTITIES_DISMISS' as const;
2023-03-23 00:45:02 +00:00
const ENTITIES_INCREMENT = 'ENTITIES_INCREMENT' as const;
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;
const ENTITIES_INVALIDATE_LIST = 'ENTITIES_INVALIDATE_LIST' as const;
2022-12-04 22:58:13 +00: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 21:52:09 +00:00
interface DeleteEntitiesOpts {
preserveLists?: boolean
}
function deleteEntities(ids: Iterable<string>, entityType: string, opts: DeleteEntitiesOpts = {}) {
2023-03-14 19:13:49 +00:00
return {
type: ENTITIES_DELETE,
ids,
entityType,
2023-03-15 21:52:09 +00:00
opts,
2023-03-14 19:13:49 +00:00
};
}
function dismissEntities(ids: Iterable<string>, entityType: string, listKey: string) {
return {
type: ENTITIES_DISMISS,
ids,
entityType,
listKey,
};
}
2023-03-23 00:45:02 +00:00
function incrementEntities(entityType: string, listKey: string, diff: number) {
return {
type: ENTITIES_INCREMENT,
entityType,
listKey,
diff,
};
}
function entitiesFetchRequest(entityType: string, listKey?: string) {
return {
type: ENTITIES_FETCH_REQUEST,
entityType,
listKey,
};
}
function entitiesFetchSuccess(
entities: Entity[],
entityType: string,
listKey?: string,
newState?: EntityListState,
overwrite = false,
) {
return {
type: ENTITIES_FETCH_SUCCESS,
entityType,
entities,
listKey,
2022-12-04 23:53:56 +00:00
newState,
overwrite,
};
}
2022-12-04 23:53:56 +00:00
function entitiesFetchFail(entityType: string, listKey: string | undefined, error: any) {
return {
type: ENTITIES_FETCH_FAIL,
entityType,
listKey,
2022-12-04 23:53:56 +00:00
error,
};
}
function invalidateEntityList(entityType: string, listKey: string) {
return {
type: ENTITIES_INVALIDATE_LIST,
entityType,
listKey,
};
}
2022-12-04 22:58:13 +00:00
/** Any action pertaining to entities. */
type EntityAction =
ReturnType<typeof importEntities>
2023-03-14 19:13:49 +00:00
| ReturnType<typeof deleteEntities>
| ReturnType<typeof dismissEntities>
2023-03-23 00:45:02 +00:00
| ReturnType<typeof incrementEntities>
| ReturnType<typeof entitiesFetchRequest>
| ReturnType<typeof entitiesFetchSuccess>
| ReturnType<typeof entitiesFetchFail>
| ReturnType<typeof invalidateEntityList>;
2022-12-04 22:58:13 +00:00
export {
ENTITIES_IMPORT,
2023-03-14 19:13:49 +00:00
ENTITIES_DELETE,
ENTITIES_DISMISS,
2023-03-23 00:45:02 +00:00
ENTITIES_INCREMENT,
ENTITIES_FETCH_REQUEST,
ENTITIES_FETCH_SUCCESS,
ENTITIES_FETCH_FAIL,
ENTITIES_INVALIDATE_LIST,
2022-12-04 22:58:13 +00:00
importEntities,
2023-03-14 19:13:49 +00:00
deleteEntities,
dismissEntities,
2023-03-23 00:45:02 +00:00
incrementEntities,
entitiesFetchRequest,
entitiesFetchSuccess,
entitiesFetchFail,
invalidateEntityList,
2022-12-04 22:58:13 +00:00
EntityAction,
2023-03-15 21:52:09 +00:00
};
export type { DeleteEntitiesOpts };