aboutsummaryrefslogtreecommitdiff
path: root/src/client/react/users.js
blob: 01ff093cbef412a5dbbe34e1243ffd4a02b64f1e (plain)
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
/* global USERS */

import { combineReducers, createStore } from 'redux';

const getId = ({ type, value }) => `${type}/${value}`;

const byId = (state = {}, action) => {
  switch (action.type) {
    case 'USERS/ADD_USER':
      return {
        ...state,
        [action.user.id]: {
          ...action.user,
        },
      };
    default:
      return state;
  }
};

const allIds = (state = [], action) => {
  switch (action.type) {
    case 'USERS/ADD_USER':
      return [
        ...state,
        action.user.id,
      ];
    default:
      return state;
  }
};

const allUsers = (state = [], action) => {
  switch (action.type) {
    case 'USERS/ADD_USER':
      return [
        ...state,
        {
          ...action.user,
        },
      ];
    default:
      return state;
  }
};

const store = createStore(combineReducers({
  byId,
  allIds,
  allUsers,
}));

USERS.forEach((user) => {
  store.dispatch({
    type: 'USERS/ADD_USER',
    user: {
      type: user.type,
      value: user.value,
      id: getId(user),
    },
  });
});

const users = store.getState();

export default users;