Skip to content

Commit 23b2cd1

Browse files
Added imports
Co-authored-by: Ron Fu
1 parent a3e3a0a commit 23b2cd1

File tree

6 files changed

+50
-58
lines changed

6 files changed

+50
-58
lines changed

app/src/components/bottom/UseStateModal.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function UseStateModal({ updateAttributeWithState, attributeToChange, childId })
3636
<div className="useState-stateDisplay">
3737
<TableStateProps
3838
providerId = {componentProviderId}
39+
canDeleteState = {false}
3940
selectHandler={(table) => {
4041
updateAttributeWithState(attributeToChange, componentProviderId, table.row.id);
4142
}}

app/src/components/right/StatePropsPanel.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,6 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
9797
} else clearForm();
9898
};
9999

100-
// find & delete table row using its id
101-
// const handlerRowDelete = (id:any) => {
102-
// // iterate and filter out stateProps with matching row id
103-
// const filtered = currentComponent.stateProps.filter(element => element.id !== id);
104-
// dispatch({
105-
// type: 'DELETE STATE',
106-
// payload: {stateProps: filtered}
107-
// });
108-
// };
109-
110100
return (
111101
<div className={'state-panel'}>
112102
<div>
@@ -184,7 +174,7 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
184174
<h4 className={isThemeLight ? classes.lightThemeFontColor : classes.darkThemeFontColor}>
185175
Current State Name: {state.components[state.canvasFocus.componentId - 1].name}
186176
</h4>
187-
<TableStateProps selectHandler={handlerRowSelect} isThemeLight={isThemeLight} />
177+
<TableStateProps canDeleteState = {true} selectHandler={handlerRowSelect} isThemeLight={isThemeLight} />
188178
</div>
189179
</div>
190180
);

app/src/components/right/TableStateProps.tsx

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ const TableStateProps = (props) => {
3030

3131
// when component gets mounted, sets the gridColumn
3232
useEffect(() => {
33-
setGridColumns(getColumns(props, state, dispatch));
33+
34+
35+
// get the columns and remove delete buttons
36+
// only if table is displayed as modal
37+
const columns = getColumns(props, state, dispatch);
38+
if(props.canDeleteState) {
39+
setGridColumns(columns);
40+
}
41+
else {
42+
setGridColumns(columns.slice(0, columns.length - 1));
43+
}
44+
3445

3546
if (!props.providerId) {
3647
const currentId = state.canvasFocus.componentId;
@@ -90,44 +101,21 @@ const getColumns = (props, state, dispatch) => {
90101
width: 70,
91102
editable: false,
92103
renderCell: function renderCell(params:any) {
93-
const getIdRow = () => {
94-
// const fields = api.getAllColumns().map((c: any) => c.field).filter((c : any) => c !== '__check__' && !!c);
95-
return params.id;
96-
// return params.getValue(fields[0]);
97-
};
98104
return (
99-
<Button style={{width:`${3}px`}}
100-
onClick={() => {
101-
const deleteAttributeWithState = (id) => {
102-
let currentComponent;
103-
if(!props.providerId) {
105+
<Button style={{width:`${3}px`}} onClick={() => {
106+
// get the current focused component
107+
// remove the state that the button is clicked
108+
// send a dispatch to rerender the table
104109
const currentId = state.canvasFocus.componentId;
105-
currentComponent = state.components[currentId - 1];
106-
const filtered = currentComponent.stateProps.filter(element => element.id !== id);
107-
console.log('filtered:', filtered);
110+
const currentComponent = state.components[currentId - 1];
111+
112+
const filtered = currentComponent.stateProps.filter(element => element.id !== params.id);
108113
dispatch({
109114
type: 'DELETE STATE',
110115
payload: {stateProps: filtered}
111116
});
112-
}
113-
else {
114-
currentComponent = state.components[props.providerId - 1];
115-
console.log("row-id: ", id);
116-
console.log("provider-id: ", props.providerId)
117-
console.log("currentProviderComponent: ", currentComponent);
118-
const filtered = currentComponent.stateProps.filter(element => element.id !== id);
119-
console.log('filtered:', filtered);
120-
dispatch({
121-
type: 'DELETE STATE',
122-
payload: {stateProps: filtered, providerId: props.providerId}
123-
});
124-
}
125-
}
126-
127-
deleteAttributeWithState(getIdRow());
128-
}
129-
130-
}>
117+
118+
}}>
131119
<ClearIcon style={{width:`${15}px`}}/>
132120
</Button>
133121
);

app/src/containers/CustomizationPanel.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,28 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
211211
const currentComponent = state.components[componentProviderId - 1];
212212
const currentComponentProps = currentComponent.stateProps;
213213
const newInput = currentComponentProps[statePropsId - 1].value;
214+
console.log("useContext of current component = ", currentComponent);
214215

215216

216217
if (attributeName === 'compText') {
217218
const newContextObj = useContextObj;
218219
if (!newContextObj[componentProviderId]) {
219-
newContextObj[componentProviderId] = {};
220+
newContextObj[componentProviderId] = {statesFromProvider : new Set()};
220221
}
222+
221223
newContextObj[componentProviderId].compText = statePropsId;
224+
newContextObj[componentProviderId].statesFromProvider.add(statePropsId);
222225
setCompText(newInput);
223226
setUseContextObj(newContextObj);
224227
}
225228
if (attributeName === 'compLink') {
226229
const newContextObj = useContextObj;
227230
if (!newContextObj[componentProviderId]) {
228-
newContextObj[componentProviderId] = {};
231+
newContextObj[componentProviderId] = {statesFromProvider : new Set()};
229232
}
230233
newContextObj[componentProviderId].compLink = statePropsId;
231-
234+
newContextObj[componentProviderId].statesFromProvider.add(statePropsId);
235+
232236
setCompLink(newInput);
233237
setUseContextObj(newContextObj);
234238
}
@@ -241,6 +245,10 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
241245

242246
}
243247

248+
useEffect(() => {
249+
console.log(state.components);
250+
}, state);
251+
244252
// dispatch to 'UPDATE CSS' called when save button is clicked,
245253
// passing in style object constructed from all changed input values
246254
const handleSave = (): Object => {

app/src/helperFunctions/generateCode.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,23 @@ const generateUnformattedCode = (
242242

243243
return state;
244244
}
245+
246+
let importContext = '';
247+
//{1: {statesFromProvider: new Set}, 2: ..., 3:...}
248+
if(currComponent.useContext) {
249+
for (const providerId of Object.keys(currComponent.useContext)) {
250+
const providerComponent = components[parseInt(providerId) - 1];
251+
importContext += `import ${providerComponent.name}Context from './${providerComponent.name}.tsx'\n`;
252+
}
253+
}
245254
// check for context
246255
if (currComponent.useContext) {
247-
248256
for (const providerId of Object.keys(currComponent.useContext)) {
249-
const attributesAndStateIds = currComponent.useContext[String(providerId)]; //currently just from App
250-
const providerComponent = components[providerId - 1];
257+
const statesFromProvider = currComponent.useContext[parseInt(providerId)].statesFromProvider; //currently just from App
258+
const providerComponent = components[parseInt(providerId) - 1];
251259
providers += 'const ' + providerComponent.name.toLowerCase() + 'Context = useContext(' + providerComponent.name + 'Context);\n';
252260

253-
for (const stateId of Object.values(attributesAndStateIds)) {
261+
for (const stateId of statesFromProvider.values()) {
254262
context +=
255263
'const ' +
256264
providerComponent.stateProps[stateId - 1].key +
@@ -270,6 +278,7 @@ const generateUnformattedCode = (
270278
${`import React, { useState, createContext, useContext } from 'react';`}
271279
${importReactRouter ? `import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';`: ``}
272280
${importsMapped}
281+
${importContext}
273282
${providers}
274283
${context}
275284
${`const ${currComponent.name} = (props: any): JSX.Element => {`}

app/src/reducers/componentReducer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ const reducer = (state: State, action: Action) => {
468468
state.HTMLTypes
469469
);
470470

471+
471472
return {...state, components }
472473

473474
}
@@ -791,14 +792,9 @@ const reducer = (state: State, action: Action) => {
791792

792793
case 'DELETE STATE' : {
793794
const components = [...state.components];
794-
let currComponent = !action.payload.providerId
795-
? findComponent(
796-
components,
797-
state.canvasFocus.componentId
798-
)
799-
: findComponent(
795+
let currComponent = findComponent(
800796
components,
801-
action.payload.providerId
797+
state.canvasFocus.componentId
802798
);
803799

804800
currComponent.stateProps = action.payload.stateProps;

0 commit comments

Comments
 (0)