Skip to content

Commit f371f4c

Browse files
author
john lim
committed
testing delete elements
1 parent 96527f4 commit f371f4c

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

app/src/components/left/HTMLItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ const HTMLItem: React.FC<{
5656

5757
// creates modal that asks if user wants to clear workspace
5858
// if user clears their workspace, then their components are removed from state and the modal is closed
59-
const deleteAllInstances = (id: number) => {
59+
const deleteAllInstances = (deleteID: number) => {
6060
// set modal options
6161
const children = (
6262
<List className="export-preference">
6363
<ListItem
6464
key={'clear'}
6565
button
66-
onClick={() => handleDelete(id)}
66+
onClick={() => handleDelete(deleteID)}
6767
style={{
6868
border: '1px solid #3f51b5',
6969
marginBottom: '2%',

app/src/reducers/componentReducer.ts

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,24 @@ const reducer = (state: State, action: Action) => {
143143
return roots;
144144
};
145145

146-
const checkChildren = child => {
147-
child.forEach(el => {
148-
if (el.children.length) {
149-
const arr = [];
150-
for (let i = 0; i < el.children.length; i++) {
151-
if (el.children[i].name !== name) {
152-
arr.push(el.children[i]);
153-
}
154-
}
155-
el.children = arr;
156-
checkChildren(el.children);
157-
}
158-
});
159-
};
160-
161146
const deleteById = (id: number): Component[] => {
162147
const name: string = state.components[id - 1].name;
163148
// console.log('name: ', name);
149+
150+
const checkChildren = child => {
151+
child.forEach(el => {
152+
if (el.children.length) {
153+
const arr = [];
154+
for (let i = 0; i < el.children.length; i++) {
155+
if (el.children[i].name !== name) {
156+
arr.push(el.children[i]);
157+
}
158+
}
159+
el.children = arr;
160+
checkChildren(el.children);
161+
}
162+
});
163+
};
164164
const copyComp = [...state.components];
165165
console.log('before check children', copyComp);
166166
if (copyComp.length) {
@@ -570,18 +570,36 @@ const reducer = (state: State, action: Action) => {
570570
}
571571

572572
case 'DELETE ELEMENT': {
573-
const HTMLTypes = [...state.HTMLTypes];
574-
const components = [...state.components];
575-
deleteById(action.payload);
576-
for (let i = 0; i < HTMLTypes.length; i += 1) {
577-
if (HTMLTypes[i].id === action.payload) {
578-
HTMLTypes.splice(i, 1);
573+
let name: string = '';
574+
const HTMLTypes = [...state.HTMLTypes].filter(el => {
575+
if (el.id === action.payload) {
576+
name = el.name;
579577
}
580-
}
578+
return el.id !== action.payload;
579+
});
581580

581+
const deleteElements = compCopy => {
582+
compCopy.forEach(child => {
583+
if (child.children.length) {
584+
const arr = [];
585+
for (let i = 0; i < child.children.length; i+=1) {
586+
if (child.name !== name) {
587+
arr.push(child.children[i]);
588+
}
589+
child.children = arr;
590+
deleteElements(child.children);
591+
}
592+
}
593+
})
594+
}
595+
const components = [...state.components];
596+
console.log("components in delete element: ", components);
597+
deleteElements(components);
598+
console.log("components in delete element after deleteElements: ", components);
582599
return {
583600
...state,
584-
HTMLTypes
601+
HTMLTypes,
602+
components
585603
};
586604
}
587605

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"cookie-parser": "^1.4.5",
112112
"cors": "^2.8.5",
113113
"d3": "^6.2.0",
114+
"dotenv": "^8.2.0",
114115
"electron-debug": "^3.1.0",
115116
"electron-devtools-installer": "^2.2.4",
116117
"electron-splashscreen": "^1.0.0",
@@ -157,7 +158,6 @@
157158
"cross-env": "^5.2.1",
158159
"csp-html-webpack-plugin": "^4.0.0",
159160
"css-loader": "^2.1.1",
160-
"dotenv": "^8.2.0",
161161
"dotenv-webpack": "^5.0.1",
162162
"electron": "^9.1.0",
163163
"electron-builder": "^22.7.0",

0 commit comments

Comments
 (0)