Skip to content

Commit 83d9d15

Browse files
authored
Merge pull request #14 from oslabs-beta/finalCut
Final cut
2 parents 69e301c + e098f7c commit 83d9d15

File tree

2 files changed

+141
-15
lines changed

2 files changed

+141
-15
lines changed

app/src/components/main/Canvas.tsx

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, useState, useEffect } from 'react';
22
import { useDrop, DropTargetMonitor } from 'react-dnd';
33
import { ItemTypes } from '../../constants/ItemTypes';
44
import StateContext from '../../context/context';
@@ -7,9 +7,58 @@ import { combineStyles } from '../../helperFunctions/combineStyles';
77
import renderChildren from '../../helperFunctions/renderChildren';
88
// Caret start
99
import Arrow from './Arrow';
10+
import { getRowsStateFromCache } from '@mui/x-data-grid/hooks/features/rows/gridRowsUtils';
1011

1112
function Canvas(props): JSX.Element {
1213
const [state, dispatch] = useContext(StateContext);
14+
const [newComp, setNewComp] = useState(false);
15+
const [copiedChildrenArr, setCopiedChildrenArr] = useState([]);
16+
const [copiedComp, setCopiedComp] = useState({});
17+
18+
useEffect(()=> {
19+
if (newComp) {
20+
//find updated comp
21+
const copy = state.components.find(comp => comp.name === copiedComp.name)
22+
// make a array of copied children from the copied component
23+
if (copy.children.length){
24+
const masterArr = [];
25+
const children = copy.children;
26+
function deepChildCopy(children, parentId) {
27+
for (let i = 0; i < children.length; i++) {
28+
const child = children[i];
29+
let id = (parentId) ? parentId : null;
30+
if (child.typeId < 1000){
31+
masterArr.push({
32+
type: "HTML Element",
33+
typeId: child.typeId,
34+
childId: id
35+
})
36+
if (child.children.length) {
37+
deepChildCopy(child.children, child.childId);
38+
}
39+
}
40+
}
41+
}
42+
deepChildCopy(children, null);
43+
setCopiedChildrenArr(masterArr);
44+
}
45+
46+
const components = state.components
47+
48+
// find the ID of the newly created component
49+
const newId = components[components.length -1]['id']
50+
dispatch({
51+
type: 'ADD CHILD',
52+
payload: {
53+
type: "Component",
54+
typeId: newId,
55+
childId: null
56+
}
57+
});
58+
}
59+
setNewComp(false)
60+
}, [newComp])
61+
1362
// Caret start
1463
Arrow.deleteLines();
1564
// find the current component to render on the canvas
@@ -49,7 +98,7 @@ function Canvas(props): JSX.Element {
4998
return;
5099
}
51100
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
52-
if (item.newInstance) {
101+
if (item.newInstance && item.instanceType !== "Component") {
53102
dispatch({
54103
type: 'ADD CHILD',
55104
payload: {
@@ -58,12 +107,73 @@ function Canvas(props): JSX.Element {
58107
childId: null
59108
}
60109
});
110+
} else if (item.newInstance && item.instanceType === "Component") {
111+
let hasDiffParent = false;
112+
const components = state.components;
113+
let newChildName = '';
114+
// loop over componenets array
115+
for (let i = 0; i < components.length; i++) {
116+
const comp = components[i];
117+
//loop over each componenets child
118+
for (let j = 0; j < comp.children.length; j++) {
119+
const child = comp.children[j];
120+
if (child.name === 'seperator') continue;
121+
// check if the item.instanceTypeId matches and child ID
122+
if (item.instanceTypeId === child.typeId) {
123+
// check if the name of the parent matches the canvas focus name
124+
// comp is the parent component
125+
// currentComponent is the canvas.focus component
126+
if (comp.name === currentComponent.name) {
127+
i = components.length;
128+
break;
129+
} else {
130+
// if false
131+
setCopiedComp(child);
132+
hasDiffParent = true;
133+
newChildName = child.name;
134+
i = components.length;
135+
break;
136+
}
137+
}
138+
}
139+
}
140+
if (!hasDiffParent) {
141+
dispatch({
142+
type: 'ADD CHILD',
143+
payload: {
144+
type: item.instanceType,
145+
typeId: item.instanceTypeId,
146+
childId: null
147+
}
148+
});
149+
} else {
150+
151+
// able to duplicate a component in dev only does not work for prod
152+
// create a new component
153+
154+
// let name = prompt("Component already has a parent. \nDo you want to create a new component and import its elements?", "Enter component name here");
155+
// while (components.some(comp => comp.name === name)) {
156+
// name = prompt(`${name} component already exists. \nPlease pick a new name.`);
157+
// }
158+
// if (name) {
159+
// dispatch({
160+
// type: 'ADD COMPONENT',
161+
// payload: { componentName: name, root: false }
162+
// });
163+
164+
// setNewComp(true);
165+
// setNewComp(!newComp)
166+
// }
167+
168+
}
169+
61170
}
62171
},
63172
collect: monitor => ({
64173
isOver: !!monitor.isOver()
65174
})
66175
});
176+
67177
// Styling for Canvas
68178
const defaultCanvasStyle = {
69179
width: '100%',

app/src/reducers/componentReducer.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ const reducer = (state: State, action: Action) => {
221221
const rootComponents = [...state.rootComponents];
222222
if (action.payload.root) rootComponents.push(newComponent.id);
223223
// updates the focus to the new component, which redirects to the new blank canvas of said new component
224-
const canvasFocus = {
225-
...state.canvasFocus,
226-
componentId: newComponent.id,
227-
childId: null
228-
};
224+
225+
// change canvas focus to just created component
226+
// const canvasFocus = {
227+
// ...state.canvasFocus,
228+
// componentId: newComponent.id,
229+
// childId: null
230+
// };
229231
const nextComponentId = state.nextComponentId + 1;
230232
newComponent.code = generateCode(
231233
components,
@@ -239,22 +241,28 @@ const reducer = (state: State, action: Action) => {
239241
components,
240242
rootComponents,
241243
nextComponentId,
242-
canvasFocus
244+
// canvasFocus
243245
};
244246
}
245247
// Add child to a given root component
246248
case 'ADD CHILD': {
249+
let parentComponentId: number;
247250
const {
248251
type,
249252
typeId,
250253
childId
251254
}: { type: string; typeId: number; childId: any } = action.payload;
252-
const parentComponentId: number = state.canvasFocus.componentId;
255+
if (action.payload.copyId) {
256+
parentComponentId = action.payload.copyId;
257+
} else {
258+
parentComponentId = state.canvasFocus.componentId;
259+
}
260+
253261
const components = [...state.components];
254-
// find component (an object) that we're adding a child to
262+
255263
const parentComponent = findComponent(components, parentComponentId);
256-
let componentName = '';
257-
let componentChildren = [];
264+
let componentName: string = '';
265+
let componentChildren: Object[] = [];
258266
if (type === 'Component') {
259267
components.forEach(comp => {
260268
if (comp.id === typeId) {
@@ -268,6 +276,7 @@ const reducer = (state: State, action: Action) => {
268276
if (childTypeExists('Component', parentComponentId, originalComponent))
269277
return state;
270278
}
279+
271280
let newName = state.HTMLTypes.reduce((name, el) => {
272281
if (typeId === el.id) {
273282
name = type === 'Component' ? componentName : el.tag;
@@ -316,8 +325,14 @@ const reducer = (state: State, action: Action) => {
316325
// if there is a childId (childId here references the direct parent of the new child) find that child and a new child to its children array
317326
else {
318327
directParent = findChild(parentComponent, childId);
319-
directParent.children.push(topSeparator);
320-
directParent.children.push(newChild);
328+
//disable nesting a component inside a HTML element
329+
if (directParent.type === "HTML Element" && type === "HTML Element") {
330+
directParent.children.push(topSeparator);
331+
directParent.children.push(newChild);
332+
} else {
333+
return { ...state };
334+
}
335+
321336
}
322337
const canvasFocus = {
323338
...state.canvasFocus,
@@ -913,7 +928,7 @@ const reducer = (state: State, action: Action) => {
913928
state.HTMLTypes
914929
);
915930
}
916-
931+
917932
deletePassedInPropsChildren(parent);
918933
deletePassedInProps(currComponent);
919934

@@ -1023,6 +1038,7 @@ const reducer = (state: State, action: Action) => {
10231038
});
10241039
return { ...state, components};
10251040
}
1041+
10261042
default:
10271043
return state;
10281044
}

0 commit comments

Comments
 (0)