108 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-20 11:34:15 +01:00
/**
*
* ListPage reducer
*
*/
2018-02-20 15:51:20 +01:00
import { fromJS, List, Map } from 'immutable';
2018-02-20 11:34:15 +01:00
// ListPage constants
import {
2018-05-18 16:46:13 +02:00
ADD_FILTER,
2018-02-20 19:20:31 +01:00
CHANGE_PARAMS,
DELETE_DATA_SUCCESS,
2018-02-20 14:25:45 +01:00
GET_DATA_SUCCEEDED,
2018-05-18 17:54:38 +02:00
ON_CHANGE,
2018-05-22 11:52:29 +02:00
ON_CLICK_REMOVE,
2018-05-16 11:27:12 +02:00
ON_TOGGLE_FILTERS,
OPEN_FILTERS_WITH_SELECTION,
2018-05-21 17:16:04 +02:00
REMOVE_ALL_FILTERS,
2018-05-18 16:46:13 +02:00
REMOVE_FILTER,
SET_PARAMS,
2018-05-21 17:16:04 +02:00
SUBMIT,
2018-02-20 11:34:15 +01:00
} from './constants';
const initialState = fromJS({
2018-05-17 17:57:45 +02:00
appliedFilters: List([]),
2018-05-21 17:16:04 +02:00
count: 0,
filters: List([]),
filtersUpdated: false,
filterToFocus: null,
2018-02-20 11:34:15 +01:00
params: Map({
_limit: 10,
_page: 1,
_sort: '',
2018-02-20 11:34:15 +01:00
}),
2018-02-20 15:51:20 +01:00
records: List([]),
2018-05-16 11:27:12 +02:00
showFilter: false,
2018-02-20 11:34:15 +01:00
});
function listPageReducer(state = initialState, action) {
switch (action.type) {
2018-05-18 16:46:13 +02:00
case ADD_FILTER:
return state.update('appliedFilters', list => list.push(Map(action.filter)));
2018-02-20 19:20:31 +01:00
case DELETE_DATA_SUCCESS:
2018-04-09 15:58:22 +02:00
return state
.update('records', (list) => (
list.filter(obj => {
if (obj._id) {
return obj._id !== action.id;
}
2018-02-20 19:20:31 +01:00
2018-04-09 15:58:22 +02:00
return obj.id !== parseInt(action.id, 10);
})
))
.update('count', (v) => v = v - 1);
2018-02-20 19:20:31 +01:00
case CHANGE_PARAMS:
return state.updateIn(action.keys, () => action.value);
2018-02-20 14:25:45 +01:00
case GET_DATA_SUCCEEDED:
return state
2018-02-20 15:51:20 +01:00
.update('count', () => action.data[0].count)
.update('records', () => List(action.data[1]));
2018-05-18 17:54:38 +02:00
case ON_CHANGE:
return state.updateIn(['appliedFilters', action.index, action.key], () => action.value);
2018-05-22 11:52:29 +02:00
case ON_CLICK_REMOVE:
return state
.update('appliedFilters', list => list.splice(action.index, 1))
2018-05-22 16:55:58 +02:00
.update('filters', list => list.splice(action.index, 1))
.update('filtersUpdated', v => v = !v);
2018-05-16 11:27:12 +02:00
case ON_TOGGLE_FILTERS:
return state
.update('filterToFocus', () => null)
.update('showFilter', v => !v);
case OPEN_FILTERS_WITH_SELECTION:
return state
.update('showFilter', () => true)
.update('filterToFocus', () => action.index);
2018-05-21 17:16:04 +02:00
case REMOVE_ALL_FILTERS:
return state
.update('appliedFilters', () => List([]))
2018-05-22 16:55:58 +02:00
.update('filters', () => List([]))
.update('filtersUpdated', v => v = !v);
2018-05-18 16:46:13 +02:00
case REMOVE_FILTER:
return state.update('appliedFilters', list => list.splice(action.index, 1));
case SET_PARAMS:
2018-05-22 15:25:21 +02:00
return state
.update('params', () => Map(action.params))
2018-05-22 16:55:58 +02:00
.update('appliedFilters', (list) => {
if (state.get('showFilter') === true) {
return list;
}
return fromJS(action.filters);
})
.update('filters', () => fromJS(action.filters))
.update('showFilter', () => false);
2018-05-21 17:16:04 +02:00
case SUBMIT:
return state
2018-05-22 11:52:29 +02:00
.update('filters', () => state.get('appliedFilters').filter(filter => filter.get('value') !== ''))
.update('appliedFilters', (list) => list.filter(filter => filter.get('value') !== ''))
2018-05-22 16:55:58 +02:00
.update('showFilter', () => false)
.update('filtersUpdated', v => v = !v);
2018-02-20 11:34:15 +01:00
default:
return state;
}
}
export default listPageReducer;