Skip to content

Commit b32e163

Browse files
committed
Merge branch 'main' of https://github.com/oslabs-beta/ReacType into master
2 parents 88ee26f + ebf00cb commit b32e163

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

app/src/reducers/componentReducer.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const reducer = (state: State, action: Action) => {
2626
// We're going to keep track of the nodes we need to search through with an Array
2727
// Initialize this array with the top level node
2828
const nodeArr: (Component | ChildElement)[] = [component];
29-
nodeArr.forEach(node => console.log('node', node));
3029
// iterate through each node in the array as long as there are elements in the array
3130
while (nodeArr.length > 0) {
3231
// shift off the first value and assign to an element
@@ -93,6 +92,14 @@ const reducer = (state: State, action: Action) => {
9392
components.forEach((comp, i) => (comp.id = i + 1));
9493
};
9594

95+
const updateRoots = (components: Component[]) => {
96+
const roots = [];
97+
components.forEach(comp => {
98+
if (comp.isPage) roots.push(comp.id);
99+
});
100+
return roots;
101+
}
102+
96103
const deleteById = (id: number): Component[] =>
97104
[...state.components].filter(comp => comp.id != id);
98105

@@ -110,6 +117,9 @@ const reducer = (state: State, action: Action) => {
110117
action.payload.componentName === ''
111118
)
112119
return state;
120+
121+
const components = [...state.components];
122+
113123
const newComponent = {
114124
id: state.components.length + 1,
115125
name: action.payload.componentName,
@@ -120,7 +130,6 @@ const reducer = (state: State, action: Action) => {
120130
isPage: action.payload.root
121131
};
122132

123-
const components = [...state.components];
124133
components.push(newComponent);
125134

126135
const rootComponents = [...state.rootComponents];
@@ -134,7 +143,6 @@ const reducer = (state: State, action: Action) => {
134143
};
135144

136145
const nextComponentId = state.nextComponentId + 1;
137-
console.log('new comps', components);
138146
return {
139147
...state,
140148
components,
@@ -190,10 +198,9 @@ const reducer = (state: State, action: Action) => {
190198
}
191199
})
192200
}
193-
194201
const newChild: ChildElement = {
195202
type,
196-
typeId,
203+
typeId: components.length + 1,
197204
name: newName,
198205
childId: state.nextChildId,
199206
style: {},
@@ -327,43 +334,20 @@ const reducer = (state: State, action: Action) => {
327334
state.projectType,
328335
state.HTMLTypes
329336
);
337+
330338
const canvasFocus = { ...state.canvasFocus, childId: null };
331339
return { ...state, components, canvasFocus };
332340
}
333341

334342
case 'DELETE PAGE': {
335343
const id: number = state.canvasFocus.componentId;
336-
// console.log('id', id);
337-
// const pageName = state.components[id-1].name;
338-
// // check if page is linked to in other pages
339-
// console.log(state.components);
340-
// console.log(pageName);
341-
342-
// //const currentComponents = [...state.components];
343-
344-
// const isLinked = (comps) => {
345-
// if (comps.length === 0) return;
346-
// comps.forEach((comp, i, array) => {
347-
// if (comp.type === 'Route Link' && comp.name === pageName) {
348-
// console.log('found link', i, comp);
349-
// array.splice(i, 1);
350-
// // delete the comp
351-
// }
352-
// if (comp.children.length > 0) isLinked(comp.children);
353-
// })
354-
// }
355-
// isLinked(state.components);
356344

357345
const components: Component[] = deleteById(id);
358346
updateIds(components);
359347

360348
// rebuild rootComponents with correct page IDs
361-
const rootComponents: number[] = [];
362-
components.forEach(comp => {
363-
if (comp.isPage) rootComponents.push(comp.id);
364-
});
365-
//console.log('curr', currentComponents);
366-
console.log('comps', components);
349+
const rootComponents = updateRoots(components);
350+
367351
const canvasFocus = { componentId: 1, childId: null }
368352
return {...state, rootComponents, components, canvasFocus}
369353
}
@@ -372,7 +356,9 @@ const reducer = (state: State, action: Action) => {
372356
const components: Component[] = deleteById(id);
373357
updateIds(components);
374358
const canvasFocus = { componentId: 1, childId: null };
375-
return {...state, components, canvasFocus, nextComponentId: id };
359+
360+
const rootComponents = updateRoots(components);
361+
return {...state, rootComponents, components, canvasFocus, nextComponentId: id };
376362
}
377363

378364
case 'SET INITIAL STATE': {
@@ -439,7 +425,6 @@ const reducer = (state: State, action: Action) => {
439425
components,
440426
canvasFocus
441427
};
442-
// return { ...initialState };
443428
}
444429

445430
case 'UPDATE PROJECT NAME': {
@@ -451,9 +436,7 @@ const reducer = (state: State, action: Action) => {
451436
}
452437

453438
case 'OPEN PROJECT': {
454-
console.log("BEFORE FUNCTION: ", action.payload.HTMLTypes);
455439
convertToJSX(action.payload.HTMLTypes);
456-
console.log("AFTER FUNCTION: ", action.payload.HTMLTypes);
457440
return {
458441
...action.payload
459442
};

app/src/tutorial/ComponentTree.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import React from 'react';
22
import { Link, withRouter, RouteComponentProps } from 'react-router-dom';
3+
import AccountTreeIcon from '@material-ui/icons/AccountTree';
34

5+
import { makeStyles } from '@material-ui/core/styles';
6+
import Typography from '@material-ui/core/Typography';
7+
import Container from '@material-ui/core/Container';
48
// import Grid from '@material-ui/core/Grid';
59

610
// import ComponentPanel from '../components/left/ComponentPanel';
711
// import HTMLPanel from '../components/left/HTMLPanel';
812

913
// Left-hand portion of the app, where component options are displayed
10-
14+
const useStyles = makeStyles({});
1115
const ComponentTree: React.FC<RouteComponentProps> = () => {
1216
return (
1317
<div>
1418
ComponentTree
15-
<Link to={`/`}>
16-
<button>Application</button>
17-
</Link>
1819
<Link to={`/tutorial`}>
1920
<button>Tutorial</button>
2021
</Link>

0 commit comments

Comments
 (0)