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';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
import type { List } from 'pl-api';
|
2022-06-09 12:08:51 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
type State = ImmutableMap<string, List | false>;
|
2022-04-13 07:07:50 -07:00
|
|
|
|
|
|
|
const initialState: State = ImmutableMap();
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const importList = (state: State, list: List) => state.set(list.id, list);
|
2022-04-13 07:07:50 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const importLists = (state: State, lists: Array<List>) => {
|
2022-04-13 07:07:50 -07:00
|
|
|
lists.forEach(list => {
|
|
|
|
state = importList(state, list);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const 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:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.set(action.listId, false);
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2022-04-13 07:07:50 -07:00
|
|
|
}
|
2024-05-12 16:18:04 -07:00
|
|
|
};
|
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
export { lists as default };
|