filters.js 881 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { FILTERS_IMPORT } from '../actions/importer';
  2. import { Map as ImmutableMap, is, fromJS } from 'immutable';
  3. const normalizeFilter = (state, filter) => {
  4. const normalizedFilter = fromJS({
  5. id: filter.id,
  6. title: filter.title,
  7. context: filter.context,
  8. filter_action: filter.filter_action,
  9. expires_at: filter.expires_at ? Date.parse(filter.expires_at) : null,
  10. });
  11. if (is(state.get(filter.id), normalizedFilter)) {
  12. return state;
  13. } else {
  14. return state.set(filter.id, normalizedFilter);
  15. }
  16. };
  17. const normalizeFilters = (state, filters) => {
  18. filters.forEach(filter => {
  19. state = normalizeFilter(state, filter);
  20. });
  21. return state;
  22. };
  23. export default function filters(state = ImmutableMap(), action) {
  24. switch(action.type) {
  25. case FILTERS_IMPORT:
  26. return normalizeFilters(state, action.filters);
  27. default:
  28. return state;
  29. }
  30. };