@@ -44,6 +44,8 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
44
44
const [ parentName , setParentName ] = useState ( 'No Parents' ) ;
45
45
const [ parentComponent , setParentComponent ] = useState ( { } ) ;
46
46
const [ rows1 , setRows1 ] = useState ( currentComponent . stateProps ) ;
47
+ const [ propNum , setPropNum ] = useState ( 1 ) ;
48
+
47
49
// convert value to correct type based on user input
48
50
const typeConversion = ( value , type ) => {
49
51
switch ( type ) {
@@ -76,6 +78,14 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
76
78
let currKey ;
77
79
const submitNewState = ( e ) => {
78
80
e . preventDefault ( ) ;
81
+
82
+ // don't allow them to submit state without all fields
83
+ if ( ! inputKey || ! inputType || ! inputValue ) {
84
+ setErrorStatus ( true ) ;
85
+ setErrorMsg ( 'All fields are required' ) ;
86
+ return ;
87
+ }
88
+
79
89
const statesArray = currentComponent . stateProps ;
80
90
//loop though array, access each obj at key property
81
91
let keyToInt = parseInt ( inputKey [ 0 ] ) ;
@@ -84,10 +94,28 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
84
94
setErrorMsg ( 'Key name can not start with int.' ) ;
85
95
return ;
86
96
}
97
+
98
+ // carly LegacyPD
99
+ // check here to see if state has already been created with the submitted key
100
+ // iterate through all the state keys in all of the components in the app
101
+ // outer for loop: iterating through all of the components in the app
102
+ for ( let i = 0 ; i < state . components . length ; i ++ ) {
103
+ // inner for loop iterating through the stateProps array for each component
104
+ for ( let j = 0 ; j < state . components [ i ] . stateProps . length ; j ++ ) {
105
+
106
+ // if find piece of state with the same key as inputKey, create an error
107
+ if ( inputKey === state . components [ i ] . stateProps [ j ] [ "key" ] ) {
108
+ setErrorStatus ( true ) ;
109
+ setErrorMsg ( 'Key name already in use.' ) ;
110
+ return ;
111
+ }
112
+ }
113
+ }
114
+ setPropNum ( prev => prev + 1 ) ;
87
115
const newState = {
88
116
// check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
89
117
// id: statesArray.length > 0 ? statesArray[statesArray.length-1].id + 1 : 1,
90
- id : inputKey ,
118
+ id : ` ${ currentComponent . name } - ${ propNum } ` ,
91
119
key : inputKey ,
92
120
value : typeConversion ( inputValue , inputType ) ,
93
121
type : inputType ,
@@ -165,6 +193,7 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
165
193
value = { inputKey }
166
194
error = { errorStatus }
167
195
onChange = { ( e ) => setInputKey ( e . target . value ) }
196
+ helperText = { errorStatus ? errorMsg : '' }
168
197
className = { isThemeLight ? `${ classes . rootLight } ${ classes . inputTextLight } ` : `${ classes . rootDark } ${ classes . inputTextDark } ` }
169
198
/>
170
199
< TextField
0 commit comments