summaryrefslogtreecommitdiff
path: root/buildscripts/libdeps/graph_visualizer_web_stack/src/redux/nodes.js
blob: 755fd17d2535ddc611d00b6d986af31152730a96 (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
import { initialState } from "./store";

export const nodes = (state = initialState, action) => {
  switch (action.type) {
    case "addNode":
      var arr = Object.assign(state);
      return [...arr, action.payload];
    case "setNodes":
      return action.payload;
    case "updateSelected":
      var newState = Object.assign(state);
      newState[action.payload.index].selected = action.payload.value;
      return newState;
    case "updateCheckbox":
      var newState = Object.assign(state);
      newState = state.map((stateNode) => {
        if (stateNode.node == action.payload.node) {
          if (action.payload.value == "flip") {
            stateNode.selected = !stateNode.selected;
          } else {
            stateNode.selected = action.payload.value;
          }
        }
        return stateNode;
      });
      return newState;
    case "updateCheckboxes":
      var newState = state.map((stateNode, index) => {
        const nodeToUpdate = action.payload.filter(
          (node) => stateNode.node == node.node
        );
        if (nodeToUpdate.length > 0) {
          stateNode.selected = nodeToUpdate[0].value;
        }
        return stateNode;
      });
      return newState;
    default:
      return state;
  }
};

export const addNode = (node) => ({
  type: "addNode",
  payload: node,
});

export const setNodes = (nodes) => ({
  type: "setNodes",
  payload: nodes,
});

export const updateSelected = (newValue) => ({
  type: "updateSelected",
  payload: newValue,
});

export const updateCheckbox = (newValue) => ({
  type: "updateCheckbox",
  payload: newValue,
});

export const updateCheckboxes = (newValue) => ({
  type: "updateCheckboxes",
  payload: newValue,
});