Skip to content

Commit 4d00632

Browse files
committed
Merge branch 'duplicateStateName' into ahsan-ben-branch
2 parents 02bd382 + b788cbe commit 4d00632

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
4444
const [parentName, setParentName] = useState('No Parents');
4545
const [parentComponent, setParentComponent] = useState({});
4646
const [rows1, setRows1] = useState(currentComponent.stateProps);
47+
const [propNum, setPropNum] = useState(1);
48+
4749
// convert value to correct type based on user input
4850
const typeConversion = (value, type) => {
4951
switch (type) {
@@ -76,6 +78,14 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
7678
let currKey;
7779
const submitNewState = (e) => {
7880
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+
7989
const statesArray = currentComponent.stateProps;
8090
//loop though array, access each obj at key property
8191
let keyToInt = parseInt(inputKey[0]);
@@ -84,10 +94,28 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
8494
setErrorMsg('Key name can not start with int.');
8595
return;
8696
}
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);
87115
const newState = {
88116
// check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
89117
// id: statesArray.length > 0 ? statesArray[statesArray.length-1].id + 1 : 1,
90-
id: inputKey,
118+
id: `${currentComponent.name}-${propNum}`,
91119
key: inputKey,
92120
value: typeConversion(inputValue, inputType),
93121
type: inputType,
@@ -165,6 +193,7 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
165193
value={inputKey}
166194
error={errorStatus}
167195
onChange={(e) => setInputKey(e.target.value)}
196+
helperText={errorStatus ? errorMsg : ''}
168197
className={isThemeLight ? `${classes.rootLight} ${classes.inputTextLight}` : `${classes.rootDark} ${classes.inputTextDark}`}
169198
/>
170199
<TextField

app/src/reducers/componentReducer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,15 @@ const reducer = (state: State, action: Action) => {
776776
// need to change this to match the id from the tree
777777
state.canvasFocus.componentId
778778
);
779+
// do a check if prop already exists in passed in props
780+
for (let i = 0; i < currComponent.passedInProps.length; i++) {
781+
let curr = currComponent.passedInProps[i];
782+
console.log('inside for loop curr', curr)
783+
console.log('inside for loop action.payload.passedInProps', action.payload.passedInProps)
784+
if (curr.id === action.payload.passedInProps.id) {
785+
return { ...state, components};
786+
}
787+
}
779788
currComponent.passedInProps.push(action.payload.passedInProps);
780789
console.log('in reducer', currComponent)
781790
//currComponent.useStateCodes = updateUseStateCodes(currComponent);

0 commit comments

Comments
 (0)