Skip to content

Commit 629e217

Browse files
Currently working on bug of deleting the first state
and added error handler for invalid input for state Co-authored-by: Ron Fu
1 parent e11019c commit 629e217

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

app/src/components/right/StatePropsPanel.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
3636
const [inputKey, setInputKey] = useState("");
3737
const [inputValue, setInputValue] = useState("");
3838
const [inputType, setInputType] = useState("");
39-
39+
const [errorStatus, setErrorStatus] = useState(false);
40+
const [errorMsg, setErrorMsg] = useState('');
4041
// get currentComponent by using currently focused component's id
4142
const currentId = state.canvasFocus.componentId;
4243
const currentComponent = state.components[currentId - 1];
@@ -61,11 +62,25 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
6162
setInputValue("");
6263
setInputType("");
6364
};
65+
//reset error warning
66+
const resetError = () => {
67+
setErrorStatus(false);
68+
};
6469

6570
// submit new stateProps entries to state context
71+
let currKey;
6672
const submitNewState = (e) => {
6773
e.preventDefault();
6874
const statesArray = currentComponent.stateProps;
75+
//loop though array, access each obj at key property
76+
let keyToInt = parseInt(inputKey[0]);
77+
if(!isNaN(keyToInt)) {
78+
setErrorStatus(true);
79+
setErrorMsg('Key name can not start with int.');
80+
return;
81+
}
82+
83+
//return alert('key can not start with number');
6984
const newState = {
7085
// check if array is not empty => true find last elem in array. get id and increment by 1 || else 1
7186
id: statesArray.length > 0 ? statesArray[statesArray.length-1].id + 1 : 1,
@@ -78,6 +93,7 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
7893
type: 'ADD STATE',
7994
payload: {newState: newState}
8095
});
96+
resetError();
8197
clearForm();
8298
};
8399

@@ -107,6 +123,7 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
107123
label="key:"
108124
variant="outlined"
109125
value={inputKey}
126+
error={errorStatus}
110127
onChange={(e) => setInputKey(e.target.value)}
111128
className={isThemeLight ? `${classes.rootLight} ${classes.inputTextLight}` : `${classes.rootDark} ${classes.inputTextDark}`}
112129
/>

app/src/components/right/TableStateProps.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const deleteState = (selectedId) => {
7171
const filtered = currentComponent.stateProps.filter(element => element.id !== selectedId);
7272
dispatch({
7373
type: 'DELETE STATE',
74-
payload: {stateProps: filtered}
74+
payload: {stateProps: filtered, rowId: selectedId}
7575
});
7676
}
7777

app/src/containers/CustomizationPanel.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,31 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
212212
const providerComponent = state.components[componentProviderId - 1];
213213
const providerStates = providerComponent.stateProps;
214214
const newInput = providerStates[statePropsId - 1].value;
215+
216+
// states providerComponent
217+
// adding new state into provideComponent
218+
// currComponent = canvas focus component
219+
// currComponent = state.components[componentProviderId - 1]; {1 : {Set, compLink, compText}}
220+
// Our set holds the state row ids of that component
221+
215222

216-
console.log("state-components", JSON.parse(JSON.stringify(state.components)));
217-
console.log("current-component", JSON.parse(JSON.stringify(currentComponent)));
223+
// console.log("state-components", JSON.parse(JSON.stringify(state.components)));
224+
// console.log("current-component", JSON.parse(JSON.stringify(currentComponent)));
218225

219226
if (attributeName === 'compText') {
220227
let newContextObj = {...currentComponent.useContext};
221228
if(!newContextObj) {
222-
console.log("New Context Obj created");
223229
newContextObj = {};
224230
}
225231

226232
if (!newContextObj[componentProviderId]) {
227-
console.log("Provider Id added to our new context obj ", componentProviderId);
228233
newContextObj[componentProviderId] = {statesFromProvider : new Set()};
229234
}
230235

231236
newContextObj[componentProviderId].compText = statePropsId;
232237
newContextObj[componentProviderId].statesFromProvider.add(statePropsId);
233238
setCompText(newInput);
234-
// setUseContextObj(newContextObj);
235-
console.log("newContextObj to be dispatched ", newContextObj);
239+
// setUseContextObj(newContextObj);
236240

237241
dispatch({
238242
type: 'UPDATE USE CONTEXT',

app/src/helperFunctions/generateCode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ const generateUnformattedCode = (
254254
// check for context
255255
if (currComponent.useContext) {
256256
for (const providerId of Object.keys(currComponent.useContext)) {
257-
const statesFromProvider = currComponent.useContext[parseInt(providerId)].statesFromProvider; //currently just from App
257+
const statesFromProvider = currComponent.useContext[parseInt(providerId)].statesFromProvider; //{1: {Set, compLink, compText}, 2 : {}...}
258258
const providerComponent = components[parseInt(providerId) - 1];
259259
providers += 'const ' + providerComponent.name.toLowerCase() + 'Context = useContext(' + providerComponent.name + 'Context);\n';
260-
260+
console.log('statesFromProviders: ', statesFromProvider);
261+
console.log('providerComponent: ', providerComponent);
261262
for (const stateId of statesFromProvider.values()) {
262263
context +=
263264
'const ' +

app/src/reducers/componentReducer.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,23 @@ const reducer = (state: State, action: Action) => {
798798
);
799799

800800
currComponent.stateProps = action.payload.stateProps;
801-
802801
currComponent.useStateCodes = updateUseStateCodes(currComponent);
803802

803+
components.forEach((component) => {
804+
if(component.useContext && component.useContext[state.canvasFocus.componentId ]) {
805+
// console.log("component that takes state from curr", JSON.parse(JSON.stringify(component)));
806+
component.useContext[state.canvasFocus.componentId].statesFromProvider.delete(action.payload.rowId);
807+
// console.log("component that has deleted state", JSON.parse(JSON.stringify(component)));
808+
component.code = generateCode(
809+
components,
810+
component.id,
811+
[...state.rootComponents],
812+
state.projectType,
813+
state.HTMLTypes
814+
);
815+
}
816+
});
817+
804818
currComponent.code = generateCode(
805819
components,
806820
state.canvasFocus.componentId,

0 commit comments

Comments
 (0)