about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/reducers/pinned_accounts_editor.js
blob: 144418d12adeef0146df13772578505c7352a4b0 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import {
  PINNED_ACCOUNTS_EDITOR_RESET,
  PINNED_ACCOUNTS_FETCH_REQUEST,
  PINNED_ACCOUNTS_FETCH_SUCCESS,
  PINNED_ACCOUNTS_FETCH_FAIL,
  PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_READY,
  PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CLEAR,
  PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CHANGE,
  ACCOUNT_PIN_SUCCESS,
  ACCOUNT_UNPIN_SUCCESS,
} from '../actions/accounts';

const initialState = ImmutableMap({
  accounts: ImmutableMap({
    items: ImmutableList(),
    loaded: false,
    isLoading: false,
  }),

  suggestions: ImmutableMap({
    value: '',
    items: ImmutableList(),
  }),
});

export default function listEditorReducer(state = initialState, action) {
  switch(action.type) {
  case PINNED_ACCOUNTS_EDITOR_RESET:
    return initialState;
  case PINNED_ACCOUNTS_FETCH_REQUEST:
    return state.setIn(['accounts', 'isLoading'], true);
  case PINNED_ACCOUNTS_FETCH_FAIL:
    return state.setIn(['accounts', 'isLoading'], false);
  case PINNED_ACCOUNTS_FETCH_SUCCESS:
    return state.update('accounts', accounts => accounts.withMutations(map => {
      map.set('isLoading', false);
      map.set('loaded', true);
      map.set('items', ImmutableList(action.accounts.map(item => item.id)));
    }));
  case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CHANGE:
    return state.setIn(['suggestions', 'value'], action.value);
  case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_READY:
    return state.setIn(['suggestions', 'items'], ImmutableList(action.accounts.map(item => item.id)));
  case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CLEAR:
    return state.update('suggestions', suggestions => suggestions.withMutations(map => {
      map.set('items', ImmutableList());
      map.set('value', '');
    }));
  case ACCOUNT_PIN_SUCCESS:
    return state.updateIn(['accounts', 'items'], list => list.unshift(action.relationship.id));
  case ACCOUNT_UNPIN_SUCCESS:
    return state.updateIn(['accounts', 'items'], list => list.filterNot(item => item === action.relationship.id));
  default:
    return state;
  }
}