Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2326e62

Browse files
committed
fixed duplicate cell id bug
1 parent 0af66d5 commit 2326e62

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/Cell.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ export default function Cell({
9595
}
9696
};
9797

98-
const setId = () => {
99-
// eslint-disable-next-line radix
100-
const id = currentCell || parseInt(cell.id.split("_")[1]);
98+
const setId = (id) => {
10199
setCurrentCell(id);
102100
};
103101

@@ -111,7 +109,9 @@ export default function Cell({
111109
refCode.current.getCodeMirror().setValue(cell.input);
112110
refOutput.current.innerHTML = cell.output;
113111
}
114-
setId();
112+
// eslint-disable-next-line radix
113+
const id = parseInt(cell.id.split("_")[1]);
114+
setId(id);
115115
// eslint-disable-next-line react-hooks/exhaustive-deps
116116
}, [cell]);
117117

@@ -130,7 +130,6 @@ export default function Cell({
130130
const newCell = (id, type) => {
131131
// eslint-disable-next-line no-shadow
132132
const newCell = {
133-
id: `cell_${currentCell + 1}`,
134133
input: "",
135134
};
136135

src/pages/demo/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { downLoad_notebook, load_notebook } from "../../utils";
55
import Header from "../../components/header/header";
66

77
const defaultState = {
8-
cells: [{ id: "cell_1", input: "", output: "", type: "code" }],
8+
cells: [{ input: "", output: "", type: "code", id: "cell_1" }],
99
};
1010

1111
export default function Demo() {

src/reducer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ export const reducer = (state, action) => {
1515
}
1616

1717
if (action.type === "ADD_CELL") {
18+
// Id generation is been done here because of the issue with dupluicate
19+
// ids generated when using an existing note
1820
const newCell = [...state.cells];
19-
newCell.splice(action.currentId, 0, action.payload);
20-
console.log(state);
21+
const getMax = Math.max(
22+
// eslint-disable-next-line func-names
23+
...newCell.map(function (o) {
24+
// eslint-disable-next-line radix
25+
return parseInt(o.id.split("_")[1]);
26+
})
27+
);
28+
const { payload } = action;
29+
payload.id = `cell_${getMax + 1}`;
30+
console.log(payload);
31+
newCell.splice(action.currentId, 0, payload);
32+
console.log(newCell);
2133
return {
2234
...state,
2335
cells: newCell,

0 commit comments

Comments
 (0)