~cytrogen/masto-fe

ref: 110c8fb8ccb1363f649d3eb30ca822f415145e6a masto-fe/app/javascript/mastodon/reducers/list_adder.js -rw-r--r-- 1.4 KiB
110c8fb8 — Sqx. Flann [bugfix] account for data structure change in instance API fallback (#63) 9 months ago
                                                                                
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
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';

import {
  LIST_ADDER_RESET,
  LIST_ADDER_SETUP,
  LIST_ADDER_LISTS_FETCH_REQUEST,
  LIST_ADDER_LISTS_FETCH_SUCCESS,
  LIST_ADDER_LISTS_FETCH_FAIL,
  LIST_EDITOR_ADD_SUCCESS,
  LIST_EDITOR_REMOVE_SUCCESS,
} from '../actions/lists';

const initialState = ImmutableMap({
  accountId: null,

  lists: ImmutableMap({
    items: ImmutableList(),
    loaded: false,
    isLoading: false,
  }),
});

export default function listAdderReducer(state = initialState, action) {
  switch(action.type) {
  case LIST_ADDER_RESET:
    return initialState;
  case LIST_ADDER_SETUP:
    return state.withMutations(map => {
      map.set('accountId', action.account.get('id'));
    });
  case LIST_ADDER_LISTS_FETCH_REQUEST:
    return state.setIn(['lists', 'isLoading'], true);
  case LIST_ADDER_LISTS_FETCH_FAIL:
    return state.setIn(['lists', 'isLoading'], false);
  case LIST_ADDER_LISTS_FETCH_SUCCESS:
    return state.update('lists', lists => lists.withMutations(map => {
      map.set('isLoading', false);
      map.set('loaded', true);
      map.set('items', ImmutableList(action.lists.map(item => item.id)));
    }));
  case LIST_EDITOR_ADD_SUCCESS:
    return state.updateIn(['lists', 'items'], list => list.unshift(action.listId));
  case LIST_EDITOR_REMOVE_SUCCESS:
    return state.updateIn(['lists', 'items'], list => list.filterNot(item => item === action.listId));
  default:
    return state;
  }
}