Skip to content

Commit 96527f4

Browse files
author
john lim
committed
testing app
1 parent e2d9c44 commit 96527f4

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

app/src/components/left/HTMLItem.tsx

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { useDrag } from 'react-dnd';
33
import { ItemTypes } from '../../constants/ItemTypes';
44
import Grid from '@material-ui/core/Grid';
55
import { makeStyles } from '@material-ui/core/styles';
6+
import List from '@material-ui/core/List';
7+
import ListItem from '@material-ui/core/ListItem';
8+
import ListItemText from '@material-ui/core/ListItemText';
9+
import createModal from '../right/createModal';
610

711
const buttonClasses =
812
'MuiButtonBase-root MuiButton-root MuiButton-text makeStyles-button-12 MuiButton-textPrimary';
@@ -34,7 +38,7 @@ const HTMLItem: React.FC<{
3438
handleDelete: (id: number) => void;
3539
}> = ({ name, id, Icon, handleDelete }) => {
3640
const classes = useStyles();
37-
41+
const [modal, setModal] = useState(null);
3842
const [{ isDragging }, drag] = useDrag({
3943
item: {
4044
type: ItemTypes.INSTANCE,
@@ -48,6 +52,64 @@ const HTMLItem: React.FC<{
4852
})
4953
});
5054

55+
const closeModal = () => setModal(null);
56+
57+
// creates modal that asks if user wants to clear workspace
58+
// if user clears their workspace, then their components are removed from state and the modal is closed
59+
const deleteAllInstances = (id: number) => {
60+
// set modal options
61+
const children = (
62+
<List className="export-preference">
63+
<ListItem
64+
key={'clear'}
65+
button
66+
onClick={() => handleDelete(id)}
67+
style={{
68+
border: '1px solid #3f51b5',
69+
marginBottom: '2%',
70+
marginTop: '5%'
71+
}}
72+
>
73+
<ListItemText
74+
primary={'Yes, delete all instances'}
75+
style={{ textAlign: 'center' }}
76+
onClick={closeModal}
77+
/>
78+
</ListItem>
79+
<ListItem
80+
key={'close'}
81+
button
82+
onClick={closeModal}
83+
style={{
84+
border: '1px solid #3f51b5',
85+
marginBottom: '2%',
86+
marginTop: '5%'
87+
}}
88+
>
89+
<ListItemText
90+
primary={'No, do not delete element'}
91+
style={{ textAlign: 'center' }}
92+
onClick={closeModal}
93+
/>
94+
</ListItem>
95+
</List>
96+
);
97+
98+
// create modal
99+
setModal(
100+
createModal({
101+
closeModal,
102+
children,
103+
message: 'Deleting this element will delete all instances of this element within the application.\nDo you still wish to proceed?',
104+
primBtnLabel: null,
105+
primBtnAction: null,
106+
secBtnAction: null,
107+
secBtnLabel: null,
108+
open: true
109+
})
110+
);
111+
};
112+
51113
return (
52114
<Grid item xs={5} key={`html-${name}`}>
53115
<div ref={drag} className={classes.HTMLPanelItem}>
@@ -62,8 +124,9 @@ const HTMLItem: React.FC<{
62124
{Icon && <Icon />}
63125
</span>
64126
{id > 11 &&
65-
<button className={buttonClasses} onClick={() => { handleDelete(id) }} > X </button> }
127+
<button className={buttonClasses} onClick={() => deleteAllInstances(id)} > X </button> }
66128
</div>
129+
{modal}
67130
</Grid>
68131
);
69132
}

app/src/reducers/componentReducer.ts

Lines changed: 18 additions & 15 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+
146161
const deleteById = (id: number): Component[] => {
147162
const name: string = state.components[id - 1].name;
148163
// 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) {
@@ -571,11 +571,14 @@ const reducer = (state: State, action: Action) => {
571571

572572
case 'DELETE ELEMENT': {
573573
const HTMLTypes = [...state.HTMLTypes];
574+
const components = [...state.components];
575+
deleteById(action.payload);
574576
for (let i = 0; i < HTMLTypes.length; i += 1) {
575577
if (HTMLTypes[i].id === action.payload) {
576578
HTMLTypes.splice(i, 1);
577579
}
578580
}
581+
579582
return {
580583
...state,
581584
HTMLTypes

0 commit comments

Comments
 (0)