about summary refs log tree commit diff
path: root/app/javascript/mastodon/reducers/filters.js
blob: 14b7040273ec7f58dd19f627371ad4e256e8d6a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { FILTERS_IMPORT } from '../actions/importer';
import { Map as ImmutableMap, is, fromJS } from 'immutable';

const normalizeFilter = (state, filter) => {
  const normalizedFilter = fromJS({
    id: filter.id,
    title: filter.title,
    context: filter.context,
    filter_action: filter.filter_action,
    expires_at: filter.expires_at ? Date.parse(filter.expires_at) : null,
  });

  if (is(state.get(filter.id), normalizedFilter)) {
    return state;
  } else {
    return state.set(filter.id, normalizedFilter);
  }
};

const normalizeFilters = (state, filters) => {
  filters.forEach(filter => {
    state = normalizeFilter(state, filter);
  });

  return state;
};

export default function filters(state = ImmutableMap(), action) {
  switch(action.type) {
  case FILTERS_IMPORT:
    return normalizeFilters(state, action.filters);
  default:
    return state;
  }
};