2022-04-13 07:07:50 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
|
|
|
|
|
|
|
import {
|
|
|
|
LIST_FETCH_SUCCESS,
|
|
|
|
LIST_FETCH_FAIL,
|
|
|
|
LISTS_FETCH_SUCCESS,
|
|
|
|
LIST_CREATE_SUCCESS,
|
|
|
|
LIST_UPDATE_SUCCESS,
|
|
|
|
LIST_DELETE_SUCCESS,
|
|
|
|
} from 'soapbox/actions/lists';
|
|
|
|
import { normalizeList } from 'soapbox/normalizers';
|
|
|
|
|
2022-06-09 12:08:51 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
|
2022-04-13 07:07:50 -07:00
|
|
|
type ListRecord = ReturnType<typeof normalizeList>;
|
|
|
|
type APIEntity = Record<string, any>;
|
|
|
|
type APIEntities = Array<APIEntity>;
|
|
|
|
|
|
|
|
type State = ImmutableMap<string, ListRecord | false>;
|
|
|
|
|
|
|
|
const initialState: State = ImmutableMap();
|
|
|
|
|
|
|
|
const importList = (state: State, list: APIEntity) => state.set(list.id, normalizeList(list));
|
|
|
|
|
|
|
|
const importLists = (state: State, lists: APIEntities) => {
|
|
|
|
lists.forEach(list => {
|
|
|
|
state = importList(state, list);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function lists(state: State = initialState, action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case LIST_FETCH_SUCCESS:
|
|
|
|
case LIST_CREATE_SUCCESS:
|
|
|
|
case LIST_UPDATE_SUCCESS:
|
|
|
|
return importList(state, action.list);
|
|
|
|
case LISTS_FETCH_SUCCESS:
|
|
|
|
return importLists(state, action.lists);
|
|
|
|
case LIST_DELETE_SUCCESS:
|
|
|
|
case LIST_FETCH_FAIL:
|
|
|
|
return state.set(action.id, false);
|
|
|
|
default:
|
|
|
|
return state;
|
2022-04-13 07:07:50 -07:00
|
|
|
}
|
|
|
|
}
|