1
1
// CARET
2
- import React , { useState , useContext , useCallback } from "react" ;
2
+ import React , { useState , useContext , useCallback , useEffect } from "react" ;
3
3
import {
4
4
createStyles ,
5
5
makeStyles ,
@@ -22,37 +22,31 @@ import {
22
22
} from "@material-ui/core" ;
23
23
24
24
import StateContext from "../../context/context" ;
25
- import ComponentPanelItem from "./ComponentPanelItem" ;
26
- import ComponentPanelRoutingItem from "./ComponentPanelRoutingItem" ;
27
-
28
25
import TableStateProps from "./TableStateProps" ;
29
- import { exists } from "node:fs" ;
30
26
31
27
const StatePropsPanel = ( { isThemeLight } ) : JSX . Element => {
32
28
const classes = useStyles ( ) ;
33
- const [ state , dispatch ] = useContext ( StateContext ) ;
29
+ const [ state ] = useContext ( StateContext ) ;
34
30
35
31
const [ inputKey , setInputKey ] = useState ( "" ) ;
36
32
const [ inputValue , setInputValue ] = useState ( "" ) ;
37
33
const [ inputType , setInputType ] = useState ( "" ) ;
38
34
39
- /*************** TEMPORARY FIX VIA FORCED RENDER ***********/
40
- // const [, updateState] = useState();
41
- // const forceUpdate = useCallback(() => updateState({}), []);
42
- /************************************************************/
35
+ const [ stateProps , setStateProps ] = useState ( [ ] ) ;
36
+
37
+ // get currentComponent by using currently focused component's id
38
+ const currentId = state . canvasFocus . componentId ;
39
+ const currentComponent = state . components [ currentId - 1 ] ;
40
+
41
+ // debug console button for development purposes
43
42
const debug = ( ) => {
44
- const currentId = state . canvasFocus . componentId ;
45
- const currentComponent = state . components [ currentId - 1 ] ;
46
43
console . log ( "currentComponent:" , currentComponent ) ;
47
44
console . log ( "currentComponent.stateProps:" , currentComponent . stateProps ) ;
48
- console . log (
49
- "currentComponent.useStateCodes:" ,
50
- currentComponent . useStateCodes
51
- ) ;
45
+ console . log ( "currentComponent.useStateCodes:" , currentComponent . useStateCodes ) ;
52
46
} ;
53
47
48
+ // convert value to correct type based on user input
54
49
const typeConversion = ( value , type ) => {
55
- // based on user input for value, convert value to correct type
56
50
switch ( type ) {
57
51
case "String" :
58
52
return String ( value ) ;
@@ -65,98 +59,71 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
65
59
}
66
60
} ;
67
61
62
+ // clears the input key, value, and type on Form
63
+ const clearForm = ( ) => {
64
+ setInputKey ( "" ) ;
65
+ setInputValue ( "" ) ;
66
+ setInputType ( "" ) ;
67
+ } ;
68
+
69
+ // submit new stateProps entries to state context
68
70
const submitNewState = ( e ) => {
69
71
e . preventDefault ( ) ;
70
- // currently focused component's id
71
- const currentId = state . canvasFocus . componentId ;
72
- // current component
73
- const currentComponent = state . components [ currentId - 1 ] ;
74
72
const statesArray = currentComponent . stateProps ;
75
73
const newState = {
74
+ // check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
76
75
id : statesArray . length > 0 ? statesArray [ statesArray . length - 1 ] . id + 1 : 1 ,
77
- // check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
78
76
key : inputKey ,
79
77
value : typeConversion ( inputValue , inputType ) ,
80
78
type : inputType ,
81
79
} ;
82
- console . log ( 'newState {}:' , newState )
83
80
// store this newStateProp obj to our Component's stateProps array
84
81
currentComponent . stateProps . push ( newState ) ;
85
- console . log ( 'currentComponent.stateProps []:' , currentComponent . stateProps )
86
82
// reset newStateProp to empty for future new state prop entries
87
83
updateUseStateCodes ( ) ;
88
- setInputKey ( "" ) ;
89
- setInputValue ( "" ) ;
90
- setInputType ( "" ) ;
84
+ clearForm ( ) ;
91
85
} ;
92
-
86
+
87
+ // generates React Hook code snippets for each new stateProp entry
93
88
const updateUseStateCodes = ( ) => {
94
- // currently focused component's id
95
- const currentId = state . canvasFocus . componentId ;
96
- // current component
97
- const currentComponent = state . components [ currentId - 1 ] ;
98
-
89
+ // array of snippets of state prop codes
99
90
const localStateCode = [ ] ;
100
- // iterate thru current component's state props to generate code expression for each new state prop entry
101
- currentComponent . stateProps . forEach ( ( el ) => {
102
- const useStateCode = `const [${ el . key } , set${
103
- el . key . charAt ( 0 ) . toUpperCase ( ) + el . key . slice ( 1 )
104
- } ] = useState<${ el . type } | undefined>(${ JSON . stringify ( el . value ) } )`;
91
+
92
+ currentComponent . stateProps . forEach ( ( stateProp ) => {
93
+ const useStateCode = `const [${ stateProp . key } , set${
94
+ stateProp . key . charAt ( 0 ) . toUpperCase ( ) + stateProp . key . slice ( 1 )
95
+ } ] = useState<${ stateProp . type } | undefined>(${ JSON . stringify ( stateProp . value ) } )`;
105
96
localStateCode . push ( useStateCode ) ;
106
97
} ) ;
98
+ // store localStateCodes in global state context
107
99
currentComponent . useStateCodes = localStateCode ;
108
100
} ;
109
- ////////////////////////////////////////////////////////////////////////////////////
110
- const handlerTableSelect = ( data ) => {
111
- // currently focused component's id
112
- const currentId = state . canvasFocus . componentId ;
113
- // current component
114
- const currentComponent = state . components [ currentId - 1 ] ;
115
- //iterate and delete index
101
+
102
+ // find table row using its id and if it exists, populate form with its details
103
+ const handlerRowSelect = ( table ) => {
116
104
let exists = false ;
117
- // [ { id, key, value, type } ]
118
-
119
- console . log ( "currentComponent.stateProps: " , currentComponent . stateProps ) ;
120
-
121
- currentComponent . stateProps . forEach ( ( element ) => {
122
- console . log ( 'element.id:' , element . id ) ;
123
- if ( element . id === data . rows . id ) exists = true ;
124
- } ) ;
125
-
126
- // if (exists) {
127
- // setInputKey(data.row.key);
128
- // setInputType(data.row.type);
129
- // setInputValue(data.row.value);
130
- // } else {
131
- // setInputKey("");
132
- // setInputValue("");
133
- // setInputType("");
134
-
135
- // }
136
-
137
- // setInputKey(data.row.key);
138
- setInputType ( data . row . type ) ;
139
- // setInputValue(data.row.value);
140
- // forceUpdate();
141
- }
142
-
143
- const handlerDeleteState = ( id ) => {
144
- // currently focused component's id
145
- const currentId = state . canvasFocus . componentId ;
146
- // current component
147
- const currentComponent = state . components [ currentId - 1 ] ;
148
- //iterate and delete index
149
- currentComponent . stateProps = currentComponent . stateProps . filter ( element => ( element . id != id ) ) ;
150
-
105
+ currentComponent . stateProps . forEach ( ( stateProp ) => {
106
+ // if stateProp id matches current row's id (table.row.id), flip exists to true
107
+ if ( stateProp . id === table . row . id ) exists = true ;
108
+ } ) ;
109
+ // if row id exists, populate form with corresponding inputs (key, value, type) from table row
110
+ if ( exists ) {
111
+ setInputKey ( table . row . key ) ;
112
+ setInputType ( table . row . type ) ;
113
+ setInputValue ( table . row . value ) ;
114
+ } else clearForm ( ) ;
115
+ } ;
116
+
117
+ // find & delete table row using its id
118
+ const handlerRowDelete = ( id :any ) => {
119
+ // iterate and filter out stateProps with matching row id
120
+ currentComponent . stateProps = currentComponent . stateProps . filter ( element => element . id !== id ) ;
151
121
updateUseStateCodes ( ) ;
152
- setInputKey ( '' ) ;
153
- setInputValue ( '' ) ;
154
- setInputType ( '' ) ;
155
-
156
- }
157
-
122
+ setStateProps ( currentComponent . stateProps . slice ( ) ) ;
123
+ } ;
124
+
158
125
return (
159
- < div style = { { border : ` ${ 3 } px solid red` } } >
126
+ < div style = { { 'background-color' : `#ececea` } } >
160
127
< div >
161
128
< FormControl >
162
129
< label > Create New State</ label >
@@ -213,12 +180,10 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
213
180
debug
214
181
</ MyButton >
215
182
< br > </ br >
216
- < br > </ br >
217
183
< MyButton type = "submit" onClick = { submitNewState } >
218
184
Save
219
185
</ MyButton >
220
186
< br > </ br >
221
- < br > </ br >
222
187
</ FormControl >
223
188
</ div >
224
189
< hr > </ hr >
@@ -230,9 +195,8 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
230
195
< label >
231
196
Name: { state . components [ state . canvasFocus . componentId - 1 ] . name }
232
197
</ label >
233
- { /* CARET - HANGING TABLE STATE PROPS */ }
234
- < div style = { { border : `${ 3 } px solid green` } } >
235
- < TableStateProps selectHandler = { handlerTableSelect } deleteHandler = { handlerDeleteState } />
198
+ < div style = { { 'background-color' :`#ececea` } } >
199
+ < TableStateProps selectHandler = { handlerRowSelect } deleteHandler = { handlerRowDelete } />
236
200
</ div >
237
201
</ div >
238
202
</ div >
0 commit comments