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,11 +22,7 @@ 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 ( ) ;
@@ -36,13 +32,19 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
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
+ // detect changes to component.stateProps[], renders and prints its contents
38
+ useEffect ( ( ) => {
39
+ console . log ( new Date ( ) . toLocaleDateString ( ) , 'stateProps:' , stateProps ) ;
40
+ } , [ stateProps ] )
41
+
42
+ // get currentComponent by using currently focused component's id
43
+ const currentId = state . canvasFocus . componentId ;
44
+ const currentComponent = state . components [ currentId - 1 ] ;
45
+
46
+ // debug console button for development purposes
43
47
const debug = ( ) => {
44
- const currentId = state . canvasFocus . componentId ;
45
- const currentComponent = state . components [ currentId - 1 ] ;
46
48
console . log ( "currentComponent:" , currentComponent ) ;
47
49
console . log ( "currentComponent.stateProps:" , currentComponent . stateProps ) ;
48
50
console . log (
@@ -51,8 +53,8 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
51
53
) ;
52
54
} ;
53
55
56
+ // convert value to correct type based on user input
54
57
const typeConversion = ( value , type ) => {
55
- // based on user input for value, convert value to correct type
56
58
switch ( type ) {
57
59
case "String" :
58
60
return String ( value ) ;
@@ -64,99 +66,72 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
64
66
return value ;
65
67
}
66
68
} ;
69
+ // clears the input key, value, and type on Form
70
+ const clearForm = ( ) => {
71
+ setInputKey ( "" ) ;
72
+ setInputValue ( "" ) ;
73
+ setInputType ( "" ) ;
74
+ } ;
67
75
76
+ // submit new stateProps entries to state context
68
77
const submitNewState = ( e ) => {
69
78
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
79
const statesArray = currentComponent . stateProps ;
75
80
const newState = {
81
+ // check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
76
82
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
83
key : inputKey ,
79
84
value : typeConversion ( inputValue , inputType ) ,
80
85
type : inputType ,
81
86
} ;
82
- console . log ( 'newState {}:' , newState )
83
87
// store this newStateProp obj to our Component's stateProps array
84
88
currentComponent . stateProps . push ( newState ) ;
85
- console . log ( 'currentComponent.stateProps []:' , currentComponent . stateProps )
86
89
// reset newStateProp to empty for future new state prop entries
87
90
updateUseStateCodes ( ) ;
88
- setInputKey ( "" ) ;
89
- setInputValue ( "" ) ;
90
- setInputType ( "" ) ;
91
+ clearForm ( ) ;
91
92
} ;
92
-
93
+
94
+ // generates React Hook code snippets for each new stateProp entry
93
95
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
-
96
+ // array of snippets of state prop codes
99
97
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 ) } )`;
98
+
99
+ currentComponent . stateProps . forEach ( ( stateProp ) => {
100
+ const useStateCode = `const [${ stateProp . key } , set${
101
+ stateProp . key . charAt ( 0 ) . toUpperCase ( ) + stateProp . key . slice ( 1 )
102
+ } ] = useState<${ stateProp . type } | undefined>(${ JSON . stringify ( stateProp . value ) } )`;
105
103
localStateCode . push ( useStateCode ) ;
106
104
} ) ;
105
+ // store localStateCodes in global state context
107
106
currentComponent . useStateCodes = localStateCode ;
108
107
} ;
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
108
+
109
+ // find table row using its id and if it exists, populate form with its details
110
+ const handlerRowSelect = ( table ) => {
116
111
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();
112
+ currentComponent . stateProps . forEach ( ( stateProp ) => {
113
+ // if stateProp id matches current row's id (table.row.id), flip exists to true
114
+ if ( stateProp . id === table . row . id ) exists = true ;
115
+ } ) ;
116
+ // if row id exists, populate form with corresponding inputs (key, value, type) from table row
117
+ if ( exists ) {
118
+ setInputKey ( table . row . key ) ;
119
+ setInputType ( table . row . type ) ;
120
+ setInputValue ( table . row . value ) ;
121
+ }
122
+ else clearForm ( ) ;
141
123
}
142
124
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
-
125
+ // find & delete table row using its id
126
+ const handlerRowDelete = ( id ) => {
127
+ //iterate and filter out stateProps with matching row id
128
+ currentComponent . stateProps = currentComponent . stateProps . filter ( element => element . id != id ) ;
151
129
updateUseStateCodes ( ) ;
152
- setInputKey ( '' ) ;
153
- setInputValue ( '' ) ;
154
- setInputType ( '' ) ;
155
-
130
+ setStateProps ( currentComponent . stateProps . slice ( ) ) ;
156
131
}
157
-
132
+
158
133
return (
159
- < div style = { { border : ` ${ 3 } px solid red` } } >
134
+ < div style = { { 'background-color' : `#ececea` } } >
160
135
< div >
161
136
< FormControl >
162
137
< label > Create New State</ label >
@@ -213,12 +188,10 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
213
188
debug
214
189
</ MyButton >
215
190
< br > </ br >
216
- < br > </ br >
217
191
< MyButton type = "submit" onClick = { submitNewState } >
218
192
Save
219
193
</ MyButton >
220
194
< br > </ br >
221
- < br > </ br >
222
195
</ FormControl >
223
196
</ div >
224
197
< hr > </ hr >
@@ -230,9 +203,8 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
230
203
< label >
231
204
Name: { state . components [ state . canvasFocus . componentId - 1 ] . name }
232
205
</ label >
233
- { /* CARET - HANGING TABLE STATE PROPS */ }
234
- < div style = { { border : `${ 3 } px solid green` } } >
235
- < TableStateProps selectHandler = { handlerTableSelect } deleteHandler = { handlerDeleteState } />
206
+ < div style = { { 'background-color' :`#ececea` } } >
207
+ < TableStateProps selectHandler = { handlerRowSelect } deleteHandler = { handlerRowDelete } />
236
208
</ div >
237
209
</ div >
238
210
</ div >
0 commit comments