Skip to content

Commit 7d3fc79

Browse files
committed
added better error handling of context manager for array and objects
1 parent 46f1aa8 commit 7d3fc79

File tree

2 files changed

+97
-26
lines changed

2 files changed

+97
-26
lines changed

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

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
2828
const [inputValue, setInputValue] = useState('');
2929
const [inputType, setInputType] = useState('');
3030
const [errorStatus, setErrorStatus] = useState(false);
31+
const [inputTypeError, setInputTypeError] = useState('');
32+
const [newVal, setNewVal] = useState('test');
3133
const [errorMsg, setErrorMsg] = useState('');
3234
const currentId = state.canvasFocus.componentId;
3335
const currentComponent = state.components[currentId - 1];
@@ -42,27 +44,79 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
4244
const typeConversion = (value: string, type: string) => {
4345
switch (type) {
4446
case 'string':
45-
return String(value);
47+
{
48+
setInputTypeError('');
49+
return String(value);
50+
}
4651
case 'number':
47-
return Number(value);
52+
{
53+
setInputTypeError('');
54+
return Number(value);
55+
}
4856
case 'boolean':
49-
return value === 'true';
57+
{
58+
setInputTypeError('');
59+
return value === 'true';
60+
}
5061
case 'array':
51-
return JSON.parse(value);
52-
case 'object':
53-
return JSON.parse(value);
62+
try{
63+
let retVal = JSON.parse(value);
64+
if(Array.isArray(retVal)){
65+
console.log('is this an array still', retVal)
66+
setInputTypeError('');
67+
return retVal
68+
}else{
69+
70+
throw new Error('Input was not an array!');
71+
72+
}
73+
}
74+
catch{
75+
76+
setInputTypeError(type)
77+
return null;
78+
}
79+
case 'object':{
80+
try{
81+
let retVal = JSON.parse(value);
82+
83+
if(typeof retVal === 'object' && !Array.isArray(retVal)){
84+
setInputTypeError('');
85+
return retVal
86+
}else{
87+
throw new Error('Input was not an object (excluding Arrays)!');
88+
}
89+
}
90+
catch{
91+
92+
setInputTypeError(type)
93+
return null;
94+
}
95+
}
5496
default:
55-
return value;
97+
{
98+
setInputTypeError('');
99+
return value;
100+
}
56101
}
57102
};
58103

104+
105+
59106
// clears the input key, value, and type on Form
60107
const clearForm = () => {
61108
setInputKey('');
62109
setInputValue('');
63110
setInputType('');
64111
};
65112

113+
114+
useEffect(()=>{
115+
console.log("inputValue changed", inputValue)
116+
setNewVal(typeConversion(inputValue, inputType));
117+
console.log("current newVal", newVal)
118+
}, [inputType, inputValue] )
119+
console.log("current outside newVal", newVal)
66120
// submit new stateProps entries to state context
67121
const submitNewState = (e) => {
68122
e.preventDefault();
@@ -91,17 +145,21 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
91145
setErrorMsg('Key name already in use.');
92146
return;
93147
}
148+
else{
149+
setErrorStatus(false);
150+
setErrorMsg('');
151+
}
94152
}
95153
}
96-
setPropNum((prev) => prev + 1);
154+
setPropNum((prev) => prev + 1);
97155
const newState = {
98156
// id name of state will be the parent component name concated with propNum. it will start at 1 and increase by 1 for each new state added
99157
id: `${currentComponent.name}-${inputKey}`,
100158
key: inputKey,
101-
value: typeConversion(inputValue, inputType),
159+
value: newVal,
102160
type: inputType
103161
};
104-
162+
105163
const setNewState = {
106164
// id name of state will be the parent component name concated with propNum. it will start at 1 and increase by 1 for each new state added
107165
id: `${currentComponent.name}-set${inputKey
@@ -111,16 +169,19 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
111169
value: '',
112170
type: 'func'
113171
};
114-
dispatch(
115-
addState({
116-
newState: newState,
117-
setNewState: setNewState,
118-
contextParam: contextParam
119-
})
120-
);
121-
setRows1([...rows1, newState]);
122-
setErrorStatus(false);
123-
clearForm();
172+
if(!inputTypeError){
173+
dispatch(
174+
addState({
175+
newState: newState,
176+
setNewState: setNewState,
177+
contextParam: contextParam
178+
})
179+
);
180+
setRows1([...rows1, newState]);
181+
setErrorStatus(false);
182+
clearForm();
183+
184+
}
124185
};
125186

126187
// find table row using its id and if it exists, populate form with its details
@@ -134,7 +195,8 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
134195
if (exists) {
135196
setInputKey(table.row.key);
136197
setInputType(table.row.type);
137-
setInputValue(table.row.value);
198+
console.log("tablerowvalue", table.row.value);
199+
setInputValue(table.row.value ? JSON.stringify(table.row.value) : '');
138200
} else clearForm();
139201
};
140202
//use effect to populate parent props table on load and every time canvas focus changes
@@ -216,7 +278,7 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
216278
? `${classes.formControl} ${classes.lightThemeFontColor}`
217279
: `${classes.formControl} ${classes.darkThemeFontColor}`
218280
}
219-
error={errorStatus}
281+
error={inputTypeError != '' || errorStatus }
220282
>
221283
<InputLabel
222284
id="select-required-label"
@@ -233,7 +295,7 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
233295
? `${classes.selectEmpty} ${classes.rootUnderlineLight} ${classes.inputTextLight}`
234296
: `${classes.selectEmpty} ${classes.rootUnderlineDark} ${classes.inputTextDark}`
235297
}
236-
value={inputType}
298+
value={inputType === 'func' ? '' : inputType}
237299
onChange={(event) => setInputType(event.target.value)}
238300
MenuProps={{ disablePortal: true }}
239301
style={
@@ -321,7 +383,10 @@ const StatePropsPanel = ({ isThemeLight, data }): JSX.Element => {
321383
: classes.darkThemeFontColor
322384
}
323385
>
324-
Required
386+
{
387+
inputTypeError === 'object' ? 'JSON object form: {"key": value}' :
388+
inputTypeError === 'array' ? 'Array form: [value]' : 'Required'
389+
}
325390
</FormHelperText>
326391
</FormControl>
327392
<br />

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ const TableStateProps = props => {
4242
field: 'value',
4343
headerName: 'Initial Value',
4444
width: 100,
45-
editable: true
45+
editable: true,
46+
valueGetter: (param) => { //to display the actual object or array instead of [object Object], leave undefined if it is setter function
47+
if(param.row.type === 'func')
48+
return;
49+
return JSON.stringify(param.row.value)
50+
}
4651
},
4752
{
4853
field: 'type',
@@ -96,7 +101,8 @@ const TableStateProps = props => {
96101

97102
// rows to show are either from current component or from a given provider
98103
let rows = [];
99-
currentComponent.stateProps?.forEach((prop) => rows.push(prop));
104+
currentComponent.stateProps?.forEach((prop) =>{ console.log('prop', prop); rows.push(prop)});
105+
console.log("what is in rows", rows)
100106

101107

102108
return (

0 commit comments

Comments
 (0)