Skip to content

Commit b56f039

Browse files
authored
Merge pull request #7 from oslabs-beta/benPropDelete
Fully functional delete props and passed in props
2 parents 7dc1160 + a948cae commit b56f039

File tree

10 files changed

+289
-109
lines changed

10 files changed

+289
-109
lines changed

.package-lock.json.icloud

-169 Bytes
Binary file not shown.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,25 @@ const StatePropsPanel = ({ isThemeLight, data}): JSX.Element => {
113113
setPropNum(prev => prev + 1);
114114
const newState = {
115115
// 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
116-
id: `${currentComponent.name}-${propNum}`,
116+
id: `${currentComponent.name}-${inputKey}`,
117117
key: inputKey,
118118
value: typeConversion(inputValue, inputType),
119119
type: inputType,
120120
};
121121

122+
const setNewState = {
123+
// 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
124+
id: `${currentComponent.name}-set${inputKey.slice(0,1).toUpperCase()}${inputKey.slice(1)}`,
125+
key: `set${inputKey.slice(0,1).toUpperCase()}${inputKey.slice(1)}`,
126+
value: '',
127+
type: 'func',
128+
};
129+
130+
131+
122132
dispatch({
123133
type: 'ADD STATE',
124-
payload: {newState: newState}
134+
payload: {newState: newState, setNewState: setNewState}
125135
});
126136
setRows1([...rows1, newState])
127137
resetError();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const TableParentProps = props => {
7373
// send a dispatch to rerender the table
7474
dispatch({
7575
type: 'ADD PASSEDINPROPS',
76-
payload: { passedInProps: parentComponentProps, rowId: rowId }
76+
payload: { passedInProps: parentComponentProps, rowId: rowId, parentComponent: parentComponent }
7777
});
7878
};
7979

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const TablePassedInProps = props => {
5656
<Button
5757
style={{ width: `${3}px`, color: 'black'}}
5858
onClick={() => {
59-
deletePassedInProps(params.row, params.id);
59+
deletePassedInProps(params.id);
6060
}}
6161
>
6262
<ClearIcon style={{ width: `${15}px` }} />
@@ -66,13 +66,13 @@ const TablePassedInProps = props => {
6666
}
6767
}
6868
];
69-
const deletePassedInProps = (parentComponentProps, rowId) => {
69+
const deletePassedInProps = (rowId) => {
7070
// get the current focused component
7171
// remove the state that the button is clicked
7272
// send a dispatch to rerender the table
7373
dispatch({
7474
type: 'DELETE PASSEDINPROPS',
75-
payload: { passedInProps: parentComponentProps, rowId: rowId }
75+
payload: { rowId: rowId }
7676
});
7777
};
7878

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,25 @@ const TableStateProps = props => {
6868
// send a dispatch to rerender the table
6969
const currentId = state.canvasFocus.componentId;
7070
const currentComponent = state.components[currentId - 1];
71-
const filtered = currentComponent.stateProps.filter(
72-
element => element.id !== selectedId
73-
);
71+
// returns an array of the remaining props after deleting selected prop
72+
const filtered = currentComponent.stateProps.slice();
73+
let otherId;
74+
for (let i = 0; i < filtered.length; i++) {
75+
let curr = filtered[i];
76+
// filtered.push(curr);
77+
if (curr.id === selectedId) {
78+
if (i %2 ===0) {
79+
otherId = filtered[i + 1];
80+
filtered.splice(i, 2);
81+
} else {
82+
otherId = filtered[i - 1];
83+
filtered.splice(i - 1, 2);
84+
}
85+
}
86+
}
7487
dispatch({
7588
type: 'DELETE STATE',
76-
payload: { stateProps: filtered, rowId: selectedId }
89+
payload: { stateProps: filtered, rowId: selectedId, otherId: otherId.id }
7790
});
7891
};
7992

app/src/components/StateManagement/DisplayTab/Tree.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
2020
// we save data to see if it changed
2121
const previouslyRenderedData = usePrevious(data);
2222
// function to filter out separators to prevent render on tree chart
23-
2423
const removeHTMLElements = (arr: object[]) => {
2524
for(let i = 0; i < arr.length; i++) {
2625
if(arr[i] === undefined) continue;

app/src/components/bottom/CodePreview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const CodePreview: React.FC<{
5858
type: 'CODE_PREVIEW_INPUT',
5959
payload: currentComponent.code
6060
});
61-
}, [state.components]);
61+
}, [currentComponent]);
6262

6363
/**
6464
* Handler thats listens to changes in code editor

app/src/helperFunctions/generateCode.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const generateUnformattedCode = (
5252
// returns an array of objects which may include components, html elements, and/or route links
5353
const getEnrichedChildren = (currentComponent: Component | ChildElement) => {
5454
// declare an array of enriched children
55-
const enrichedChildren = currentComponent.children.map((elem: any) => {
55+
const enrichedChildren = currentComponent.children?.map((elem: any) => {
5656
//enrichedChildren is iterating through the children array
5757
const child = { ...elem };
5858
// check if child is a component
@@ -122,8 +122,21 @@ const generateUnformattedCode = (
122122
//LEGACY PD: CAN ADD PROPS HERE AS JSX ATTRIBUTE
123123
const elementTagDetails = (childElement: object) => {
124124
let customizationDetails = '';
125+
let passedInPropsString = '';
126+
if (childElement.type === 'Component') {
127+
let childComponent;
128+
for (let i = 0; i < components.length; i++) {
129+
if (childElement.name === components[i].name) {
130+
childComponent = components[i];
131+
}
132+
}
133+
childComponent.passedInProps.forEach(prop => {passedInPropsString += `${prop.key} = {${prop.key}} `
134+
})
135+
}
136+
137+
125138
if (childElement.childId && childElement.tag !== 'Route') //legacypd
126-
customizationDetails += ' ' + `id="${+childElement.childId}"` + ' props = {props}';
139+
customizationDetails += ' ' + `id = "${+childElement.childId}" ` + `${passedInPropsString}`;
127140
if (childElement.attributes && childElement.attributes.cssClasses) {
128141
customizationDetails +=
129142
' ' + `className="${childElement.attributes.cssClasses}"`;
@@ -244,7 +257,7 @@ const generateUnformattedCode = (
244257
return `${enrichedChildren
245258
.map((child: any) => {
246259
if (child.type === 'Component') {
247-
return `<${child.name} ${elementTagDetails(child)} />`;
260+
return `<${child.name} ${elementTagDetails(child)}/>`;
248261
} else if (child.type === 'HTML Element') {
249262
return elementGenerator(child, level);
250263
}
@@ -301,38 +314,38 @@ const generateUnformattedCode = (
301314
};
302315

303316
// // Generate import --- FROM PREVIOUS ITERATION BEFORE 12.0, NOT WORKING -> CONSIDER DELETING
304-
let importContext = '';
305-
if (currComponent.useContext) {
306-
for (const providerId of Object.keys(currComponent.useContext)) {
307-
const providerComponent = components[parseInt(providerId) - 1];
308-
importContext += `import ${providerComponent.name}Context from './${providerComponent.name}.tsx'\n \t\t`;
309-
}
310-
}
311-
if (currComponent.useContext) {
312-
for (const providerId of Object.keys(currComponent.useContext)) {
313-
const statesFromProvider =
314-
currComponent.useContext[parseInt(providerId)].statesFromProvider; //{1: {Set, compLink, compText}, 2 : {}...}
315-
const providerComponent = components[parseInt(providerId) - 1];
316-
providers +=
317-
'const ' +
318-
providerComponent.name.toLowerCase() +
319-
'Context = useContext(' +
320-
providerComponent.name +
321-
'Context);\n \t\t';
322-
for (let i = 0; i < providerComponent.stateProps.length; i++) {
323-
if (statesFromProvider.has(providerComponent.stateProps[i].id)) {
324-
context +=
325-
'const ' +
326-
providerComponent.stateProps[i].key +
327-
' = ' +
328-
providerComponent.name.toLowerCase() +
329-
'Context.' +
330-
providerComponent.stateProps[i].key +
331-
'; \n \t\t';
332-
}
333-
}
334-
}
335-
}
317+
// let importContext = '';
318+
// if (currComponent.useContext) {
319+
// for (const providerId of Object.keys(currComponent.useContext)) {
320+
// const providerComponent = components[parseInt(providerId) - 1];
321+
// importContext += `import ${providerComponent.name}Context from './${providerComponent.name}.tsx'\n \t\t`;
322+
// }
323+
// }
324+
// if (currComponent.useContext) {
325+
// for (const providerId of Object.keys(currComponent.useContext)) {
326+
// const statesFromProvider =
327+
// currComponent.useContext[parseInt(providerId)].statesFromProvider; //{1: {Set, compLink, compText}, 2 : {}...}
328+
// const providerComponent = components[parseInt(providerId) - 1];
329+
// providers +=
330+
// 'const ' +
331+
// providerComponent.name.toLowerCase() +
332+
// 'Context = useContext(' +
333+
// providerComponent.name +
334+
// 'Context);\n \t\t';
335+
// for (let i = 0; i < providerComponent.stateProps.length; i++) {
336+
// if (statesFromProvider.has(providerComponent.stateProps[i].id)) {
337+
// context +=
338+
// 'const ' +
339+
// providerComponent.stateProps[i].key +
340+
// ' = ' +
341+
// providerComponent.name.toLowerCase() +
342+
// 'Context.' +
343+
// providerComponent.stateProps[i].key +
344+
// '; \n \t\t';
345+
// }
346+
// }
347+
// }
348+
// }
336349
// create final component code. component code differs between classic react, next.js, gatsby.js
337350
// classic react code
338351
if (projectType === 'Classic React') {
@@ -362,15 +375,15 @@ const generateUnformattedCode = (
362375

363376
if (currComponent.name === 'App') {
364377
allContext.reverse().forEach((el, i) => {
365-
let tabs = `\t\t`;
366-
if (i === allContext.length - 1) {
367-
tabs = `\t\t\t`;
368-
}
369-
result = `${tabs.repeat(allContext.length - i)}<${
370-
el.name
371-
}Provider>\n ${result}\n ${tabs.repeat(allContext.length - i)}</${
372-
el.name
373-
}Provider>`;
378+
let tabs = `\t\t`;
379+
if (i === allContext.length - 1) {
380+
tabs = `\t\t\t`;
381+
}
382+
result = `${tabs.repeat(allContext.length - i)}<${
383+
el.name
384+
}Provider>\n ${result}\n ${tabs.repeat(allContext.length - i)}</${
385+
el.name
386+
}Provider>`;
374387
});
375388
}
376389
return result;
@@ -411,9 +424,7 @@ ${createContextImport()}
411424
${importsMapped}
412425
${`const ${currComponent.name} = (props) => {`}
413426
${createUseContextHook()}
414-
${` const [value, setValue] = useState("");${writeStateProps(
415-
currComponent.useStateCodes
416-
)}`}
427+
${`${writeStateProps(currComponent.useStateCodes)}`}
417428
418429
return(
419430
<>
@@ -436,7 +447,6 @@ export default ${currComponent.name}
436447
437448
const ${currComponent.name[0].toUpperCase() +
438449
currComponent.name.slice(1)} = (props): JSX.Element => {
439-
const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
440450
return (
441451
<>
442452
${
@@ -462,7 +472,6 @@ export default ${currComponent.name}
462472
import { StaticQuery, graphql } from 'gatsby';
463473
${links ? `import { Link } from 'gatsby'` : ``}
464474
const ${currComponent.name} = (props: any): JSX.Element => {
465-
const[value, setValue] = useState<any | undefined>("INITIAL VALUE");
466475
return (
467476
<>
468477
${

0 commit comments

Comments
 (0)