1
- import React , { useState , useContext , useEffect , useMemo } from 'react' ;
2
-
1
+ import React , { useState , useContext , useEffect , useMemo , Component } from 'react' ;
3
2
import { makeStyles } from '@material-ui/core/styles' ;
4
3
import FormControl from '@material-ui/core/FormControl' ;
5
4
import Select from '@material-ui/core/Select' ;
6
5
import MenuItem from '@material-ui/core/MenuItem' ;
7
6
import TextField from '@material-ui/core/TextField' ;
8
7
import Button from '@material-ui/core/Button' ;
9
-
10
8
import { stateContext } from '../context/context' ;
11
-
9
+ import HTMLTypes from '../context/HTMLTypes' ;
12
10
import ProjectManager from '../components/right/ProjectManager' ;
11
+ import Dialog from '@material-ui/core/Dialog' ;
12
+ import DialogActions from '@material-ui/core/DialogActions' ;
13
+ import DialogContent from '@material-ui/core/DialogContent' ;
14
+ import DialogContentText from '@material-ui/core/DialogContentText' ;
15
+ import DialogTitle from '@material-ui/core/DialogTitle' ;
16
+ import ErrorMessages from '../constants/ErrorMessages' ;
13
17
14
18
// need to pass in props to use the useHistory feature of react router
15
19
const RightContainer = ( props ) : JSX . Element => {
@@ -22,10 +26,11 @@ const RightContainer = (props): JSX.Element => {
22
26
const [ BGColor , setBGColor ] = useState ( '' ) ;
23
27
const [ compWidth , setCompWidth ] = useState ( '' ) ;
24
28
const [ compHeight , setCompHeight ] = useState ( '' ) ;
29
+ const [ dialogError , setDialogError ] = useState ( false ) ;
30
+ const [ deleteIndexError , setDeleteIndexError ] = useState ( false ) ;
31
+ const [ deleteComponentError , setDeleteComponentError ] = useState ( false ) ;
25
32
26
33
const resetFields = ( ) => {
27
- //console.log(configTarget);
28
- //console.log(configTarget.children);
29
34
const style = configTarget . child
30
35
? configTarget . child . style
31
36
: configTarget . style ;
@@ -137,6 +142,25 @@ const RightContainer = (props): JSX.Element => {
137
142
. some ( el => el . id === configTarget . id ) ;
138
143
}
139
144
145
+ const isIndex = ( ) : boolean => configTarget . id === 1 ;
146
+
147
+ const isChildOfPage = ( ) : boolean => {
148
+ // TODO: refactor
149
+ // TODO: output parent name and id to refocus canvas on parent
150
+ let isChild : boolean = false ;
151
+ const { id } = configTarget ;
152
+ state . components . forEach ( comp => {
153
+ comp . children . forEach ( child => {
154
+ if ( child . type === 'Component' && child . typeId === id ) {
155
+ isChild = true ;
156
+ }
157
+ } ) ;
158
+ } ) ;
159
+ return isChild ;
160
+ }
161
+
162
+
163
+
140
164
// dispatch to 'UPDATE CSS' called when save button is clicked,
141
165
// passing in style object constructed from all changed input values
142
166
const handleSave = ( ) : Object => {
@@ -163,11 +187,16 @@ const RightContainer = (props): JSX.Element => {
163
187
} ;
164
188
165
189
const handlePageDelete = ( id ) => ( ) => {
166
- dispatch ( { type : 'DELETE PAGE' , payload : { id } } ) ;
190
+ // TODO: return modal
191
+ isIndex ( )
192
+ ? handleDialogError ( 'index' )
193
+ : dispatch ( { type : 'DELETE PAGE' , payload : { id } } ) ;
167
194
}
168
-
195
+
169
196
const handleDeleteReusableComponent = ( ) => {
170
- dispatch ( { type : 'DELETE REUSABLE COMPONENT' , payload : { } } ) ;
197
+ isChildOfPage ( )
198
+ ? handleDialogError ( 'component' )
199
+ : dispatch ( { type : 'DELETE REUSABLE COMPONENT' , payload : { } } ) ;
171
200
}
172
201
173
202
const isReusable = ( configTarget ) : boolean => {
@@ -177,6 +206,16 @@ const RightContainer = (props): JSX.Element => {
177
206
. some ( el => el . id == configTarget . id ) ;
178
207
}
179
208
209
+ const handleDialogError = ( err ) => {
210
+ if ( err === 'index' ) setDeleteIndexError ( true ) ;
211
+ else setDeleteComponentError ( true ) ;
212
+ }
213
+
214
+ const handleCloseDialogError = ( ) => {
215
+ setDeleteIndexError ( false ) ;
216
+ setDeleteComponentError ( false ) ;
217
+ }
218
+
180
219
return (
181
220
< div className = "column right " >
182
221
< div className = "rightPanelWrapper" >
@@ -387,22 +426,57 @@ const RightContainer = (props): JSX.Element => {
387
426
DELETE PAGE
388
427
</ Button >
389
428
</ div >
390
- ) : isReusable ( configTarget ) ? (
429
+ ) : (
391
430
< div className = { classes . buttonRow } >
392
431
< Button
393
432
color = "secondary"
394
433
className = { classes . button }
395
434
onClick = { handleDeleteReusableComponent }
396
435
>
397
- DELETE PAGE
436
+ DELETE REUSABLE COMPONENT
398
437
</ Button >
399
438
</ div >
400
- ) :
401
- ''
439
+ )
402
440
) }
403
441
</ div >
404
442
< ProjectManager />
405
443
</ div >
444
+ < Dialog
445
+ open = { deleteIndexError }
446
+ onClose = { handleCloseDialogError }
447
+ aria-labelledby = "alert-dialog-title"
448
+ aria-describedby = "alert-dialog-description"
449
+ >
450
+ < DialogTitle id = "alert-dialog-title" > { ErrorMessages . deleteIndexTitle } </ DialogTitle >
451
+ < DialogContent >
452
+ < DialogContentText id = "alert-dialog-description" >
453
+ { ErrorMessages . deleteIndexMessage }
454
+ </ DialogContentText >
455
+ </ DialogContent >
456
+ < DialogActions >
457
+ < Button onClick = { handleCloseDialogError } color = "primary" >
458
+ OK
459
+ </ Button >
460
+ </ DialogActions >
461
+ </ Dialog >
462
+ < Dialog
463
+ open = { deleteComponentError }
464
+ onClose = { handleCloseDialogError }
465
+ aria-labelledby = "alert-dialog-title"
466
+ aria-describedby = "alert-dialog-description"
467
+ >
468
+ < DialogTitle id = "alert-dialog-title" > { ErrorMessages . deleteComponentTitle } </ DialogTitle >
469
+ < DialogContent >
470
+ < DialogContentText id = "alert-dialog-description" >
471
+ { ErrorMessages . deleteComponentMessage }
472
+ </ DialogContentText >
473
+ </ DialogContent >
474
+ < DialogActions >
475
+ < Button onClick = { handleCloseDialogError } color = "primary" >
476
+ OK
477
+ </ Button >
478
+ </ DialogActions >
479
+ </ Dialog >
406
480
</ div >
407
481
) ;
408
482
} ;
0 commit comments