Skip to content

Commit c0497e9

Browse files
author
john lim
committed
Merge branch 'main' of https://github.com/oslabs-beta/ReacType into master
2 parents f371f4c + ee9a426 commit c0497e9

File tree

5 files changed

+106
-67
lines changed

5 files changed

+106
-67
lines changed

app/src/components/main/Canvas.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const findNestedChild = (curr, components) => {
1717

1818
function Canvas() {
1919
const [state, dispatch] = useContext(StateContext);
20-
console.log('components', state.components);
2120
// find the current component to render on the canvas
2221
const currentComponent: Component = state.components.find(
2322
(elem: Component) => elem.id === state.canvasFocus.componentId

app/src/containers/RightContainer.tsx

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import DialogContentText from '@material-ui/core/DialogContentText';
2020
import DialogTitle from '@material-ui/core/DialogTitle';
2121
import ErrorMessages from '../constants/ErrorMessages';
2222
import { styleContext } from './AppContainer';
23+
import List from '@material-ui/core/List';
24+
import ListItem from '@material-ui/core/ListItem';
25+
import ListItemText from '@material-ui/core/ListItemText';
26+
import createModal from '../components/right/createModal';
2327

2428
// need to pass in props to use the useHistory feature of react router
2529
const RightContainer = (): JSX.Element => {
@@ -36,6 +40,7 @@ const RightContainer = (): JSX.Element => {
3640
const [deleteIndexError, setDeleteIndexError] = useState(false);
3741
const [deleteComponentError, setDeleteComponentError] = useState(false);
3842
const { style } = useContext(styleContext);
43+
const [modal, setModal] = useState(null);
3944

4045
const resetFields = () => {
4146
const style = configTarget.child
@@ -156,22 +161,6 @@ const RightContainer = (): JSX.Element => {
156161

157162
// id of target we want to check
158163
const { id } = configTarget;
159-
// put in logic here to check for all nested children
160-
// const checkAllChildren = comps => {
161-
// comps.forEach(el => {
162-
// if (el.children.length > 0) {
163-
// checkAllChildren(el.children);
164-
// }
165-
// if (el.type === 'Component' && el.typeId === id) {
166-
// isChild = true;
167-
// }
168-
// });
169-
// };
170-
// const copyComp = [...state.components];
171-
// checkAllChildren(copyComp);
172-
// return isChild;
173-
174-
// was here previously
175164
state.components.forEach(comp => {
176165
comp.children.forEach(child => {
177166
if (child.type === 'Component' && child.typeId === id) {
@@ -232,12 +221,9 @@ const RightContainer = (): JSX.Element => {
232221
: dispatch({ type: 'DELETE PAGE', payload: { id } });
233222
};
234223

235-
const handleDeleteReusableComponent = () => {
236-
// isChildOfPage()
237-
// ? handleDialogError('component')
238-
// :
239-
dispatch({ type: 'DELETE REUSABLE COMPONENT', payload: {} });
240-
};
224+
// const handleDeleteReusableComponent = () => {
225+
// dispatch({ type: 'DELETE REUSABLE COMPONENT', payload: {} });
226+
// };
241227

242228
const isReusable = (configTarget): boolean => {
243229
return state.components
@@ -256,6 +242,64 @@ const RightContainer = (): JSX.Element => {
256242
setDeleteLinkedPageError(false);
257243
};
258244

245+
// closes out the open modal
246+
const closeModal = (): void => setModal('');
247+
248+
// creates modal that asks if user wants to clear all components
249+
// if user clears their components, then their components are removed from state and the modal is closed
250+
const clearComps = (): void => {
251+
// Reset state for project to initial state
252+
const handleDeleteReusableComponent = (): void => {
253+
closeModal();
254+
dispatch({ type: 'DELETE REUSABLE COMPONENT', payload: {} });
255+
};
256+
257+
// set modal options
258+
const children = (
259+
<List className="export-preference">
260+
<ListItem
261+
key={'delete'}
262+
button
263+
onClick={handleDeleteReusableComponent}
264+
style={{
265+
border: '1px solid #3f51b5',
266+
marginBottom: '2%',
267+
marginTop: '5%'
268+
}}
269+
>
270+
<ListItemText primary={'Yes'} style={{ textAlign: 'center' }} />
271+
</ListItem>
272+
<ListItem
273+
key={'not delete'}
274+
button
275+
onClick={closeModal}
276+
style={{
277+
border: '1px solid #3f51b5',
278+
marginBottom: '2%',
279+
marginTop: '5%'
280+
}}
281+
>
282+
<ListItemText primary={'No'} style={{ textAlign: 'center' }} />
283+
</ListItem>
284+
</List>
285+
);
286+
287+
// create modal
288+
setModal(
289+
createModal({
290+
closeModal,
291+
children,
292+
message:
293+
'Deleting this component will delete all instances of this component within the application. Do you still wish to proceed?',
294+
primBtnLabel: null,
295+
primBtnAction: null,
296+
secBtnAction: null,
297+
secBtnLabel: null,
298+
open: true
299+
})
300+
);
301+
};
302+
259303
return (
260304
<div className="column right" style={style}>
261305
<div className="rightPanelWrapper">
@@ -471,7 +515,7 @@ const RightContainer = (): JSX.Element => {
471515
<Button
472516
color="secondary"
473517
className={classes.button}
474-
onClick={handleDeleteReusableComponent}
518+
onClick={clearComps}
475519
>
476520
DELETE REUSABLE COMPONENT
477521
</Button>
@@ -524,6 +568,7 @@ const RightContainer = (): JSX.Element => {
524568
</Button>
525569
</DialogActions>
526570
</Dialog> */}
571+
{modal}
527572
</div>
528573
);
529574
};

app/src/context/HTMLTypes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const HTMLTypes: HTMLType[] = [
1515
tag: 'div',
1616
name: 'Div',
1717
style: {},
18-
placeHolderShort: '',
18+
placeHolderShort: 'div',
1919
placeHolderLong: '',
2020
icon: HeaderIcon
2121
},

app/src/helperFunctions/generateCode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const generateUnformattedCode = (
3232
const getEnrichedChildren = (currentComponent: Component | ChildElement) => {
3333
const enrichedChildren = currentComponent.children.map((elem: any) => {
3434
const child = { ...elem };
35-
console.log('child', child);
3635
if (child.type === 'Component') {
3736
const referencedComponent = components.find(
3837
elem => elem.id === child.typeId

app/src/reducers/componentReducer.ts

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import initialState from '../context/initialState';
99
import generateCode from '../helperFunctions/generateCode';
1010
import cloneDeep from '../helperFunctions/cloneDeep';
1111
import { isValueObject } from 'immutable';
12+
import Canvas from '../components/main/Canvas';
1213

1314
const reducer = (state: State, action: Action) => {
1415
// if the project type is set as Next.js, next component code should be generated
@@ -90,23 +91,28 @@ const reducer = (state: State, action: Action) => {
9091

9192
const updateIds = (components: Component[]) => {
9293
// component IDs should be array index + 1
93-
console.log('component', components);
9494
components.forEach((comp, i) => (comp.id = i + 1));
9595

96-
// putting components' name and id into an obj
96+
// put components' names and ids into an obj
9797
const obj = {};
9898
components.forEach(el => {
9999
obj[el.name] = el.id;
100100
});
101-
// update all id and typeid to match one another
102-
const updateAllIds = comp => {
101+
// update all ids and typeIds to match one another
102+
const updateAllIds = (comp: Component[] | ChildElement[]) => {
103+
// for each of the components, if it has children, iterate through that children array
103104
comp.forEach(el => {
104105
if (el.children.length > 0) {
105106
for (let i = 0; i < el.children.length; i++) {
107+
// update each child's childId
106108
el.children[i].childId = i + 1;
109+
// if the child's name and id exists in the object
107110
if (obj[el.children[i].name]) {
111+
// set the child's typeId to be the value in the object of the child's name key
108112
el.children[i].typeId = obj[el.children[i].name];
109113
}
114+
// recursively call the updateAllIds function on the child's children array if
115+
// the child's children array is greater than 0
110116
if (el.children[i].children.length > 0) {
111117
updateAllIds(el.children[i].children);
112118
}
@@ -137,37 +143,45 @@ const reducer = (state: State, action: Action) => {
137143

138144
const updateRoots = (components: Component[]) => {
139145
const roots = [];
146+
// for each of the components in the passed in array of components, if the child component
147+
// is a page, push its id into the roots array
140148
components.forEach(comp => {
141149
if (comp.isPage) roots.push(comp.id);
142150
});
143151
return roots;
144152
};
145153

146154
const deleteById = (id: number): Component[] => {
155+
// name of the component we want to delete
147156
const name: string = state.components[id - 1].name;
148-
// console.log('name: ', name);
149157

150-
const checkChildren = child => {
158+
const checkChildren = (child: Component[] | ChildElement[]) => {
159+
// for each of the components in the passed in components array, if the child
160+
// component has a children array, iterate through the array of children
151161
child.forEach(el => {
152162
if (el.children.length) {
153163
const arr = [];
154164
for (let i = 0; i < el.children.length; i++) {
165+
// check to see if the name variable doesn't match the name of the child
155166
if (el.children[i].name !== name) {
167+
// if so, push into the new array the child component
156168
arr.push(el.children[i]);
157169
}
158170
}
171+
// set the children array to be the new array
159172
el.children = arr;
173+
// recursively call the checkChildren function with the updated children array
160174
checkChildren(el.children);
161175
}
162176
});
163177
};
178+
// creating a copy of the components array
164179
const copyComp = [...state.components];
165-
console.log('before check children', copyComp);
180+
166181
if (copyComp.length) {
167-
// for each item in the array, check to see if the children array is not empty
168182
checkChildren(copyComp);
169183
}
170-
console.log('after check children', copyComp);
184+
171185
const filteredArr = [...copyComp].filter(comp => comp.id != id);
172186
return updateIds(filteredArr);
173187
};
@@ -181,7 +195,6 @@ const reducer = (state: State, action: Action) => {
181195

182196
const deleteComponentFromPages = (components, name) => {
183197
const searchNestedComps = childComponents => {
184-
console.log('child components', childComponents);
185198
// if (childComponents.length === 0) return console.log('empty children array');
186199
// childComponents.forEach((comp, i, arr) => {
187200
// console.log('each individual comp', comp);
@@ -191,10 +204,8 @@ const reducer = (state: State, action: Action) => {
191204
// });
192205
};
193206
components.forEach(comp => {
194-
console.log('current comp', comp);
195207
searchNestedComps(comp.children);
196208
});
197-
console.log('comp', components);
198209
};
199210

200211
switch (action.type) {
@@ -228,7 +239,6 @@ const reducer = (state: State, action: Action) => {
228239
};
229240

230241
const nextComponentId = state.nextComponentId + 1;
231-
console.log('add components', components);
232242
return {
233243
...state,
234244
components,
@@ -428,45 +438,31 @@ const reducer = (state: State, action: Action) => {
428438

429439
case 'DELETE PAGE': {
430440
const id: number = state.canvasFocus.componentId;
431-
432441
const components: Component[] = deleteById(id);
433-
434442
// rebuild rootComponents with correct page IDs
435443
const rootComponents = updateRoots(components);
436-
437444
const canvasFocus = { componentId: 1, childId: null };
438445
return { ...state, rootComponents, components, canvasFocus };
439446
}
440447
case 'DELETE REUSABLE COMPONENT': {
441448
const id: number = state.canvasFocus.componentId;
442-
443449
// updated list of components after deleting a component
444450
const components: Component[] = deleteById(id);
451+
const rootComponents: number[] = updateRoots(components);
445452

446-
const rootComponents = updateRoots(components);
447-
448-
// console.log('canvas', state);
449-
// const component = findComponent(
450-
// state.components,
451-
// state.canvasFocus.componentId
452-
// );
453-
// console.log('component: ', component);
454-
// const { directParent, childIndexValue } = findParent(
455-
// component,
456-
// state.canvasFocus.componentId
457-
// );
458-
// console.log('childIndexValue: ', childIndexValue);
459-
// console.log('directparent', directParent);
460-
// const child = { ...directParent.children[childIndexValue] };
461-
// directParent.children.splice(childIndexValue, 1);
462-
463-
// component.code = generateCode(
464-
// [...state.components],
465-
// id,
466-
// [...state.rootComponents],
467-
// state.projectType,
468-
// state.HTMLTypes
469-
// );
453+
// iterate over the length of the components array
454+
for (let i = 0; i < components.length; i++) {
455+
// for each components' code, run the generateCode function to
456+
// update the code preview on the app
457+
components[i].code = generateCode(
458+
components,
459+
components[i].id,
460+
rootComponents,
461+
state.projectType,
462+
state.HTMLTypes
463+
);
464+
}
465+
console.log('components', components);
470466

471467
const canvasFocus = { componentId: 1, childId: null };
472468

0 commit comments

Comments
 (0)