Skip to content

Commit 745370f

Browse files
authored
Merge pull request #4 from oslabs-beta/ron-william-changes
Ron and William Changes
2 parents 996e25f + 0839e5f commit 745370f

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

app/src/components/main/Canvas.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ function Canvas() {
2424
// onClickHandler is responsible for changing the focused component and child component
2525
function onClickHandler(event) {
2626
event.stopPropagation();
27-
// note: a null value for the child id means that we are focusing on the top-level component rather than any child
28-
console.log(state.canvasFocus);
27+
// note: a null value for the child id means that we are focusing on the top-level component rather than any child
2928
changeFocus(state.canvasFocus.componentId, null);
3029
};
3130

app/src/components/right/StatePropsPanel.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import TableStateProps from "./TableStateProps";
2929

3030

3131
const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
32+
const [state, dispatch] = useContext(StateContext);
3233
const classes = useStyles();
33-
const [state] = useContext(StateContext);
34+
3435

3536
const [inputKey, setInputKey] = useState("");
3637
const [inputValue, setInputValue] = useState("");
@@ -78,6 +79,9 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
7879
currentComponent.stateProps.push(newState);
7980
// reset newStateProp to empty for future new state prop entries
8081
updateUseStateCodes();
82+
dispatch({
83+
type: 'ADD STATE'
84+
});
8185
clearForm();
8286
};
8387

@@ -113,6 +117,7 @@ const StatePropsPanel = ({ isThemeLight }): JSX.Element => {
113117

114118
// find & delete table row using its id
115119
const handlerRowDelete = (id:any) => {
120+
console.log('handlerRowDelete invoked')
116121
// iterate and filter out stateProps with matching row id
117122
currentComponent.stateProps = currentComponent.stateProps.filter(element => element.id !== id);
118123
updateUseStateCodes();

app/src/helperFunctions/generateCode.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const generateUnformattedCode = (
6767
child.children = getEnrichedChildren(child);
6868
}
6969
// when we see a Switch, import React Router
70-
if (referencedHTML.tag === 'Switch') importReactRouter = true;
70+
if (referencedHTML.tag === 'Switch' || referencedHTML.tag === 'Link') importReactRouter = true;
7171
return child;
7272
} else if (child.type === 'Route Link') {
7373
links = true;
@@ -82,26 +82,26 @@ const generateUnformattedCode = (
8282
// Raised formatStyles so that it is declared before it is referenced. It was backwards.
8383
// format styles stored in object to match React inline style format
8484
const formatStyles = (styleObj: any) => {
85-
console.log(styleObj);
86-
8785
if (Object.keys(styleObj).length === 0) return ``;
8886
const formattedStyles = [];
89-
for (let i in styleObj) {
90-
let styleString = i + ': ' + "'" + styleObj[i] + "'";
91-
// if(i === 'style') {
92-
// styleString = i + ':' + JSON.stringify(styleObj[i]);
93-
// }
94-
95-
formattedStyles.push(styleString);
87+
let styleString;
88+
for (let i in styleObj) {
89+
if(i === 'style') {
90+
styleString = i + ':' + JSON.stringify(styleObj[i]);
91+
formattedStyles.push(styleString);
92+
}
9693
}
97-
return ' style={{' + formattedStyles.join(',') + '}}';
94+
return formattedStyles;
9895
};
9996

10097
// function to dynamically add classes, ids, and styles to an element if it exists.
10198
const elementTagDetails = (childElement: object) => {
10299
let customizationDetails = "";
103100
if (childElement.childId && childElement.tag !== 'Route') customizationDetails += (' ' + `id="${+childElement.childId}"`);
104-
if (childElement.attributes && childElement.attributes.cssClasses) customizationDetails += (' ' + `className="${childElement.attributes.cssClasses}"`);
101+
if (childElement.attributes && childElement.attributes.cssClasses) {
102+
console.log(childElement.attributes);
103+
customizationDetails += (' ' + `className="${childElement.attributes.cssClasses}"`);
104+
}
105105
if (childElement.style && Object.keys(childElement.style).length > 0) customizationDetails +=(' ' + formatStyles(childElement));
106106
return customizationDetails;
107107
};
@@ -214,8 +214,6 @@ const generateUnformattedCode = (
214214

215215
// create final component code. component code differs between classic react, next.js, gatsby.js
216216
// classic react code
217-
218-
//import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
219217
if (projectType === 'Classic React') {
220218
return `
221219
${stateful && !classBased ? `import React, {useState} from 'react';` : ''}
@@ -245,17 +243,17 @@ const generateUnformattedCode = (
245243
${classBased ? `render(): JSX.Element {` : ``}
246244
${!importReactRouter ?
247245
`return (
248-
<div className="${currentComponent.name}" style={props.style}>
246+
<div className="${currentComponent.name}" ${formatStyles(currentComponent)}>
249247
${writeNestedElements(enrichedChildren)}
250248
</div>
251249
);` : `return (
252250
<Router>
253-
<div className="${currentComponent.name}" style={props.style}>
251+
<div className="${currentComponent.name}" ${formatStyles(currentComponent)}>
254252
${writeNestedElements(enrichedChildren)}
255253
</div>
256254
</Router>
257255
);`}
258-
${classBased ? `}` : ``}
256+
${`}`}
259257
export default ${currentComponent.name};
260258
`;
261259
}
@@ -300,7 +298,7 @@ const generateUnformattedCode = (
300298
301299
const ${currentComponent.name} = (props: any): JSX.Element => {
302300
303-
const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
301+
const[value, setValue] = useState<any | undefined>("INITIAL VALUE");
304302
305303
return (
306304
<>

app/src/reducers/componentReducer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,31 @@ const reducer = (state: State, action: Action) => {
732732
...state
733733
};
734734
}
735+
case 'ADD STATE': {
736+
// if (!state.canvasFocus.childId) return state;
737+
// find the current component in focus
738+
const components = [...state.components];
739+
const component = findComponent(
740+
components,
741+
state.canvasFocus.componentId
742+
);
743+
744+
console.log("line 744", component);
745+
746+
console.log("line 760", component.code);
747+
748+
component.code = generateCode(
749+
components,
750+
state.canvasFocus.componentId,
751+
[...state.rootComponents],
752+
state.projectType,
753+
state.HTMLTypes
754+
);
755+
756+
console.log('line 770', component.code);
757+
758+
return { ...state, components};
759+
}
735760
default:
736761
return state;
737762
}

0 commit comments

Comments
 (0)