~cytrogen/masto-fe

ref: f8dc013b0d81d5d463c086e291ce99ab1d796a9d masto-fe/app/javascript/mastodon/reducers/list_editor.js -rw-r--r-- 3.1 KiB
f8dc013b — Cytrogen [feature] Add language selector to local settings 5 days 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Map as ImmutableMap, List as ImmutableList } from "immutable";

import {
  LIST_CREATE_REQUEST,
  LIST_CREATE_FAIL,
  LIST_CREATE_SUCCESS,
  LIST_UPDATE_REQUEST,
  LIST_UPDATE_FAIL,
  LIST_UPDATE_SUCCESS,
  LIST_EDITOR_RESET,
  LIST_EDITOR_SETUP,
  LIST_EDITOR_TITLE_CHANGE,
  LIST_ACCOUNTS_FETCH_REQUEST,
  LIST_ACCOUNTS_FETCH_SUCCESS,
  LIST_ACCOUNTS_FETCH_FAIL,
  LIST_EDITOR_SUGGESTIONS_READY,
  LIST_EDITOR_SUGGESTIONS_CLEAR,
  LIST_EDITOR_SUGGESTIONS_CHANGE,
  LIST_EDITOR_ADD_SUCCESS,
  LIST_EDITOR_REMOVE_SUCCESS,
} from "../actions/lists";

const initialState = ImmutableMap({
  listId: null,
  isSubmitting: false,
  isChanged: false,
  title: "",
  isExclusive: false,

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

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

export default function listEditorReducer(state = initialState, action) {
  switch(action.type) {
    case LIST_EDITOR_RESET:
      return initialState;
    case LIST_EDITOR_SETUP:
      return state.withMutations(map => {
        map.set("listId", action.list.get("id"));
        map.set("title", action.list.get("title"));
        map.set("isExclusive", action.list.get("is_exclusive"));
        map.set("isSubmitting", false);
      });
    case LIST_EDITOR_TITLE_CHANGE:
      return state.withMutations(map => {
        map.set("title", action.value);
        map.set("isChanged", true);
      });
    case LIST_CREATE_REQUEST:
    case LIST_UPDATE_REQUEST:
      return state.withMutations(map => {
        map.set("isSubmitting", true);
        map.set("isChanged", false);
      });
    case LIST_CREATE_FAIL:
    case LIST_UPDATE_FAIL:
      return state.set("isSubmitting", false);
    case LIST_CREATE_SUCCESS:
    case LIST_UPDATE_SUCCESS:
      return state.withMutations(map => {
        map.set("isSubmitting", false);
        map.set("listId", action.list.id);
      });
    case LIST_ACCOUNTS_FETCH_REQUEST:
      return state.setIn(["accounts", "isLoading"], true);
    case LIST_ACCOUNTS_FETCH_FAIL:
      return state.setIn(["accounts", "isLoading"], false);
    case LIST_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 LIST_EDITOR_SUGGESTIONS_CHANGE:
      return state.setIn(["suggestions", "value"], action.value);
    case LIST_EDITOR_SUGGESTIONS_READY:
      return state.setIn(["suggestions", "items"], ImmutableList(action.accounts.map(item => item.id)));
    case LIST_EDITOR_SUGGESTIONS_CLEAR:
      return state.update("suggestions", suggestions => suggestions.withMutations(map => {
        map.set("items", ImmutableList());
        map.set("value", "");
      }));
    case LIST_EDITOR_ADD_SUCCESS:
      return state.updateIn(["accounts", "items"], list => list.unshift(action.accountId));
    case LIST_EDITOR_REMOVE_SUCCESS:
      return state.updateIn(["accounts", "items"], list => list.filterNot(item => item === action.accountId));
    default:
      return state;
  }
}