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

74 wiersze
1.7 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_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;
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-14 19:13:49 +00:00
function deleteEntities(ids: Iterable<string>, entityType: string) {
return {
type: ENTITIES_DELETE,
ids,
entityType,
};
}
function entitiesFetchRequest(entityType: string, listKey?: string) {
return {
type: ENTITIES_FETCH_REQUEST,
entityType,
listKey,
};
}
2022-12-04 23:53:56 +00:00
function entitiesFetchSuccess(entities: Entity[], entityType: string, listKey?: string, newState?: EntityListState) {
return {
type: ENTITIES_FETCH_SUCCESS,
entityType,
entities,
listKey,
2022-12-04 23:53:56 +00:00
newState,
};
}
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,
};
}
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 entitiesFetchRequest>
| ReturnType<typeof entitiesFetchSuccess>
| ReturnType<typeof entitiesFetchFail>;
2022-12-04 22:58:13 +00:00
export {
ENTITIES_IMPORT,
2023-03-14 19:13:49 +00:00
ENTITIES_DELETE,
ENTITIES_FETCH_REQUEST,
ENTITIES_FETCH_SUCCESS,
ENTITIES_FETCH_FAIL,
2022-12-04 22:58:13 +00:00
importEntities,
2023-03-14 19:13:49 +00:00
deleteEntities,
entitiesFetchRequest,
entitiesFetchSuccess,
entitiesFetchFail,
2022-12-04 22:58:13 +00:00
EntityAction,
};