summaryrefslogtreecommitdiff
path: root/frontend/src/redux/errors.js
blob: eb807fdcc6906c792e2d769ed7e8936e96c7eba4 (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
import Immutable from "seamless-immutable";

export const types = {
  ERROR_RECEIVED_ON_WS: "ERROR/RECEIVED_ON_WS"
};

export const actions = {
  errorReceived: error => ({
    type: types.ERROR_RECEIVED_ON_WS,
    error
  })
};

const initialState = Immutable.from({
  nextId: 0,
  history: []
});

export default (state = initialState, action) => {
  switch (action.type) {
    case types.ERROR_RECEIVED_ON_WS:
      let error = Object.assign(
        { id: state.nextId, timestamp: new Date() },
        action.error
      );
      let newState = state.set("nextId", state.nextId + 1);
      newState = addErrorToHistory(newState, error);
      return newState;
    default:
      return state;
  }
};

function addErrorToHistory(state, error) {
  return addToArray(state, "history", error);
}

function addToArray(state, arrayKey, element) {
  return state.set(arrayKey, state[arrayKey].concat([element]));
}
bgstack15