-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreducer.js
More file actions
54 lines (50 loc) · 1.41 KB
/
reducer.js
File metadata and controls
54 lines (50 loc) · 1.41 KB
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
/* eslint-disable import/prefer-default-export */
// eslint-disable-next-line consistent-return
export const reducer = (state, action) => {
if (action.type === "CHANGE_CELL") {
const stateCells = state.cells.map((cell) => {
if (cell.id === action.payload.id) {
return action.payload;
}
return cell;
});
return {
...state,
cells: stateCells,
};
}
if (action.type === "ADD_CELL") {
// Id generation is been done here because of the issue with dupluicate
// ids generated when using an existing note
const newCell = [...state.cells];
const getMax = Math.max(
// eslint-disable-next-line func-names
...newCell.map(function (o) {
// eslint-disable-next-line radix
return parseInt(o.id.split("_")[1]);
})
);
const { payload } = action;
payload.id = `cell_${getMax + 1}`;
newCell.splice(action.currentId, 0, payload);
console.log(newCell);
return {
...state,
cells: newCell,
};
}
if (action.type === "DELETE_CELL") {
if (state.cells.length > 1) {
const newCell = state.cells.filter((cell) => {
console.log(cell);
return cell.id !== action.payload;
});
return { ...state, cells: newCell };
}
return { ...state };
}
if (action.type === "LOAD_NOTE") {
// console.log(action.payload);
return { ...state, cells: action.payload };
}
};