EntityStory: add dismissEntities action for deleting ids from a list

environments/review-group-requ-7youoa/deployments/2945
Alex Gleason 2023-03-22 14:34:10 -05:00
rodzic 4049de50aa
commit 3d72e6305f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
3 zmienionych plików z 63 dodań i 1 usunięć

Wyświetl plik

@ -1,4 +1,10 @@
import { deleteEntities, entitiesFetchFail, entitiesFetchRequest, importEntities } from '../actions';
import {
deleteEntities,
dismissEntities,
entitiesFetchFail,
entitiesFetchRequest,
importEntities,
} from '../actions';
import reducer, { State } from '../reducer';
import { createListState } from '../utils';
@ -97,4 +103,24 @@ test('deleting items', () => {
expect(result.TestEntity!.store).toMatchObject({ '2': { id: '2' } });
expect([...result.TestEntity!.lists['']!.ids]).toEqual(['2']);
});
test('dismiss items', () => {
const state: State = {
TestEntity: {
store: { '1': { id: '1' }, '2': { id: '2' }, '3': { id: '3' } },
lists: {
'yolo': {
ids: new Set(['1', '2', '3']),
state: createListState(),
},
},
},
};
const action = dismissEntities(['3', '1'], 'TestEntity', 'yolo');
const result = reducer(state, action);
expect(result.TestEntity!.store).toMatchObject(state.TestEntity!.store);
expect([...result.TestEntity!.lists.yolo!.ids]).toEqual(['2']);
});

Wyświetl plik

@ -2,6 +2,7 @@ import type { Entity, EntityListState } from './types';
const ENTITIES_IMPORT = 'ENTITIES_IMPORT' as const;
const ENTITIES_DELETE = 'ENTITIES_DELETE' as const;
const ENTITIES_DISMISS = 'ENTITIES_DISMISS' 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;
@ -29,6 +30,15 @@ function deleteEntities(ids: Iterable<string>, entityType: string, opts: DeleteE
};
}
function dismissEntities(ids: Iterable<string>, entityType: string, listKey: string) {
return {
type: ENTITIES_DISMISS,
ids,
entityType,
listKey,
};
}
function entitiesFetchRequest(entityType: string, listKey?: string) {
return {
type: ENTITIES_FETCH_REQUEST,
@ -60,6 +70,7 @@ function entitiesFetchFail(entityType: string, listKey: string | undefined, erro
type EntityAction =
ReturnType<typeof importEntities>
| ReturnType<typeof deleteEntities>
| ReturnType<typeof dismissEntities>
| ReturnType<typeof entitiesFetchRequest>
| ReturnType<typeof entitiesFetchSuccess>
| ReturnType<typeof entitiesFetchFail>;
@ -67,11 +78,13 @@ type EntityAction =
export {
ENTITIES_IMPORT,
ENTITIES_DELETE,
ENTITIES_DISMISS,
ENTITIES_FETCH_REQUEST,
ENTITIES_FETCH_SUCCESS,
ENTITIES_FETCH_FAIL,
importEntities,
deleteEntities,
dismissEntities,
entitiesFetchRequest,
entitiesFetchSuccess,
entitiesFetchFail,

Wyświetl plik

@ -3,6 +3,7 @@ import produce, { enableMapSet } from 'immer';
import {
ENTITIES_IMPORT,
ENTITIES_DELETE,
ENTITIES_DISMISS,
ENTITIES_FETCH_REQUEST,
ENTITIES_FETCH_SUCCESS,
ENTITIES_FETCH_FAIL,
@ -68,6 +69,26 @@ const deleteEntities = (
});
};
const dismissEntities = (
state: State,
entityType: string,
ids: Iterable<string>,
listKey: string,
) => {
return produce(state, draft => {
const cache = draft[entityType] ?? createCache();
const list = cache.lists[listKey];
if (list) {
for (const id of ids) {
list.ids.delete(id);
}
draft[entityType] = cache;
}
});
};
const setFetching = (
state: State,
entityType: string,
@ -96,6 +117,8 @@ function reducer(state: Readonly<State> = {}, action: EntityAction): State {
return importEntities(state, action.entityType, action.entities, action.listKey);
case ENTITIES_DELETE:
return deleteEntities(state, action.entityType, action.ids, action.opts);
case ENTITIES_DISMISS:
return dismissEntities(state, action.entityType, action.ids, action.listKey);
case ENTITIES_FETCH_SUCCESS:
return importEntities(state, action.entityType, action.entities, action.listKey, action.newState);
case ENTITIES_FETCH_REQUEST: