bigbuffet-rw/app/soapbox/reducers/list_adder.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

import { List as ImmutableList, Record as ImmutableRecord } from 'immutable';
import { AnyAction } from 'redux';
2020-03-27 13:59:38 -07:00
import {
LIST_ADDER_RESET,
LIST_ADDER_SETUP,
LIST_ADDER_LISTS_FETCH_REQUEST,
LIST_ADDER_LISTS_FETCH_SUCCESS,
LIST_ADDER_LISTS_FETCH_FAIL,
LIST_EDITOR_ADD_SUCCESS,
LIST_EDITOR_REMOVE_SUCCESS,
} from '../actions/lists';
const ListsRecord = ImmutableRecord({
items: ImmutableList<string>(),
loaded: false,
isLoading: false,
});
const ReducerRecord = ImmutableRecord({
accountId: null as string | null,
2020-03-27 13:59:38 -07:00
lists: ListsRecord(),
2020-03-27 13:59:38 -07:00
});
type State = ReturnType<typeof ReducerRecord>;
export default function listAdderReducer(state: State = ReducerRecord(), action: AnyAction) {
2020-03-27 13:59:38 -07:00
switch(action.type) {
case LIST_ADDER_RESET:
return ReducerRecord();
2020-03-27 13:59:38 -07:00
case LIST_ADDER_SETUP:
return state.withMutations(map => {
map.set('accountId', action.account.get('id'));
});
case LIST_ADDER_LISTS_FETCH_REQUEST:
return state.setIn(['lists', 'isLoading'], true);
case LIST_ADDER_LISTS_FETCH_FAIL:
return state.setIn(['lists', 'isLoading'], false);
case LIST_ADDER_LISTS_FETCH_SUCCESS:
return state.update('lists', lists => lists.withMutations(map => {
map.set('isLoading', false);
map.set('loaded', true);
map.set('items', ImmutableList(action.lists.map((item: { id: string }) => item.id)));
2020-03-27 13:59:38 -07:00
}));
case LIST_EDITOR_ADD_SUCCESS:
return state.updateIn(['lists', 'items'], list => (list as ImmutableList<string>).unshift(action.listId));
2020-03-27 13:59:38 -07:00
case LIST_EDITOR_REMOVE_SUCCESS:
return state.updateIn(['lists', 'items'], list => (list as ImmutableList<string>).filterNot(item => item === action.listId));
2020-03-27 13:59:38 -07:00
default:
return state;
}
2021-08-03 12:22:51 -07:00
}