2023-05-23 15:15:17 +00:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
|
|
|
|
2017-12-05 22:02:27 +00:00
|
|
|
import {
|
|
|
|
LIST_FETCH_SUCCESS,
|
|
|
|
LIST_FETCH_FAIL,
|
|
|
|
LISTS_FETCH_SUCCESS,
|
|
|
|
LIST_CREATE_SUCCESS,
|
|
|
|
LIST_UPDATE_SUCCESS,
|
|
|
|
LIST_DELETE_SUCCESS,
|
|
|
|
} from '../actions/lists';
|
2017-11-24 23:35:37 +00:00
|
|
|
|
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
const normalizeList = (state, list) => state.set(list.id, fromJS(list));
|
|
|
|
|
2017-12-05 22:02:27 +00:00
|
|
|
const normalizeLists = (state, lists) => {
|
|
|
|
lists.forEach(list => {
|
|
|
|
state = normalizeList(state, list);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2017-11-24 23:35:37 +00:00
|
|
|
export default function lists(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case LIST_FETCH_SUCCESS:
|
2017-12-05 22:02:27 +00:00
|
|
|
case LIST_CREATE_SUCCESS:
|
|
|
|
case LIST_UPDATE_SUCCESS:
|
2017-11-24 23:35:37 +00:00
|
|
|
return normalizeList(state, action.list);
|
2017-12-05 22:02:27 +00:00
|
|
|
case LISTS_FETCH_SUCCESS:
|
|
|
|
return normalizeLists(state, action.lists);
|
|
|
|
case LIST_DELETE_SUCCESS:
|
|
|
|
case LIST_FETCH_FAIL:
|
|
|
|
return state.set(action.id, false);
|
2017-11-24 23:35:37 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2022-12-18 15:51:37 +00:00
|
|
|
}
|