Fakultas Ilmu Komputer UI

reducer.tsx 2.1 KB
Newer Older
1
import { AnyAction } from "redux";
2
3
import { IEcosystem } from "../../types/firestore/ecosystems";
import { IReduxEcosystem } from "../../types/redux/ecosystem";
4

5
6
7
8
9
10
11
12
13
14
15
16
const initialState: IReduxEcosystem = {
  ecosystem: {
    categoryId: "",
    creatorId: "",
    description: "",
    followerCount: 0,
    name: "",
    pic: "",
    raters: 0,
    rating: 0,
    visibility: "public",
  },
17
18
19
20
21
22
  members: {
    supplier: {},
    main: {},
    customer: {},
    support: {},
  },
23
};
24
25
26
27

const ecosystemReducer = (
  state = initialState,
  action: AnyAction
28
): IReduxEcosystem => {
29
  switch (action.type) {
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
    case "SET_ECOSYSTEM": {
      return action.payload as IReduxEcosystem;
    }
    case "SET_ECOSYSTEM_DETAILS": {
      return { ...state, ecosystem: action.payload as IEcosystem };
    }
    case "ADD_MEMBER": {
      const payload = action.payload as {
        type: string;
        categoryId: string;
        userId: string;
      };
      return {
        ...state,
        members: {
          ...state.members,
          [payload.type]: {
            ...state.members[payload.type],
            [payload.categoryId]: {
              ...state.members[payload.type][payload.categoryId],
              [payload.userId]: true,
            },
          },
        },
      };
    }
    case "DELETE_MEMBER": {
      const payload = action.payload as {
        type: string;
        categoryId: string;
        userId: string;
      };
      const _state = state;
      delete _state.members[payload.type][payload.categoryId][payload.userId];
      if (
        Object.keys(_state.members[payload.type][payload.categoryId]).length ===
        0
      ) {
        delete _state.members[payload.type][payload.categoryId];
      }
      return { ..._state };
    }
    case "DELETE_CATEGORY": {
      const payload = action.payload as {
        type: string;
        categoryId: string;
      };
      const _state = state;
      delete _state.members[payload.type][payload.categoryId];
      return { ..._state };
    }
    case "RESET": {
      return initialState;
83
84
85
86
87
88
89
    }
    default:
      return state;
  }
};

export default ecosystemReducer;