1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import { useDrag } from 'react-dnd' ;
3
3
import { ItemTypes } from '../../constants/ItemTypes' ;
4
4
import Grid from '@material-ui/core/Grid' ;
5
5
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' ;
6
10
7
11
const buttonClasses =
8
12
'MuiButtonBase-root MuiButton-root MuiButton-text makeStyles-button-12 MuiButton-textPrimary' ;
@@ -34,7 +38,7 @@ const HTMLItem: React.FC<{
34
38
handleDelete : ( id : number ) => void ;
35
39
} > = ( { name, id, Icon, handleDelete } ) => {
36
40
const classes = useStyles ( ) ;
37
-
41
+ const [ modal , setModal ] = useState ( null ) ;
38
42
const [ { isDragging } , drag ] = useDrag ( {
39
43
item : {
40
44
type : ItemTypes . INSTANCE ,
@@ -48,6 +52,64 @@ const HTMLItem: React.FC<{
48
52
} )
49
53
} ) ;
50
54
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
+
51
113
return (
52
114
< Grid item xs = { 5 } key = { `html-${ name } ` } >
53
115
< div ref = { drag } className = { classes . HTMLPanelItem } >
@@ -62,8 +124,9 @@ const HTMLItem: React.FC<{
62
124
{ Icon && < Icon /> }
63
125
</ span >
64
126
{ id > 11 &&
65
- < button className = { buttonClasses } onClick = { ( ) => { handleDelete ( id ) } } > X </ button > }
127
+ < button className = { buttonClasses } onClick = { ( ) => deleteAllInstances ( id ) } > X </ button > }
66
128
</ div >
129
+ { modal }
67
130
</ Grid >
68
131
) ;
69
132
}
0 commit comments