Skip to content

Commit 34d938c

Browse files
committed
updating state input error handling so no duplicates or blanks allowed
1 parent df1495c commit 34d938c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

app/src/components/StateManagement/CreateTab/components/StatePropsPanel.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
7575
let currKey;
7676
const submitNewState = (e) => {
7777
e.preventDefault();
78+
79+
// don't allow them to submit state without all fields
80+
if (!inputKey|| !inputType || !inputValue) {
81+
setErrorStatus(true);
82+
setErrorMsg('All fields are required');
83+
return;
84+
}
85+
7886
const statesArray = currentComponent.stateProps;
7987
//loop though array, access each obj at key property
8088
let keyToInt = parseInt(inputKey[0]);
@@ -83,6 +91,24 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
8391
setErrorMsg('Key name can not start with int.');
8492
return;
8593
}
94+
95+
// carly LegacyPD
96+
// check here to see if state has already been created with the submitted key
97+
// iterate through all the state keys in all of the components in the app
98+
// outer for loop: iterating through all of the components in the app
99+
for (let i = 0; i < state.components.length; i++) {
100+
// inner for loop iterating through the stateProps array for each component
101+
for (let j = 0; j < state.components[i].stateProps.length; j++) {
102+
103+
// if find piece of state with the same key as inputKey, create an error
104+
if (inputKey === state.components[i].stateProps[j]["key"]) {
105+
setErrorStatus(true);
106+
setErrorMsg('Key name already in use.');
107+
return;
108+
}
109+
}
110+
}
111+
86112
const newState = {
87113
// check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
88114
id: statesArray.length > 0 ? statesArray[statesArray.length-1].id + 1 : 1,
@@ -161,6 +187,7 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
161187
value={inputKey}
162188
error={errorStatus}
163189
onChange={(e) => setInputKey(e.target.value)}
190+
helperText={errorStatus ? errorMsg : ''}
164191
className={isThemeLight ? `${classes.rootLight} ${classes.inputTextLight}` : `${classes.rootDark} ${classes.inputTextDark}`}
165192
/>
166193
<TextField

0 commit comments

Comments
 (0)