Skip to content

Commit 1782360

Browse files
committed
style is now using context
2 parents b87363b + 3389b47 commit 1782360

File tree

7 files changed

+96
-45
lines changed

7 files changed

+96
-45
lines changed

app/src/components/main/DirectChildComponent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
1818

1919
// find the top-level component corresponding to this instance of the component
2020
// find the current component to render on the canvas
21+
2122
const referencedComponent: Component = state.components.find(
2223
(elem: Component) => elem.id === typeId
2324
);
24-
25+
2526
const [{ isDragging }, drag] = useDrag({
2627
// setting item attributes to be referenced when updating state with new instance of dragged item
2728
item: {
@@ -57,6 +58,8 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
5758
state.canvasFocus.childId === childId ? '1px 1px 3px rgb(11,212,112)' : ''
5859
};
5960

61+
//console.log('reference comp', referencedComponent);
62+
6063
const combinedStyle = combineStyles(
6164
combineStyles(
6265
combineStyles(globalDefaultStyle, referencedComponent.style),
@@ -79,10 +82,12 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
7982
(elem: Component) => elem.id === child.typeId
8083
);
8184
// combine the styles of the child with the reference component but give higher priority to the child's styles
85+
8286
const combinedStyle = combineStyles(
8387
childReferencedComponent.style,
8488
child.style
8589
);
90+
8691
// render an IndirectChild component, and also call renderIndirectChildren recursively to render any of the child Component's children
8792
return (
8893
<IndirectChild

app/src/components/main/RouteLink.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function RouteLink({ childId, type, typeId, style }: ChildElement) {
1111
const ref = useRef(null);
1212

1313
// find the name of the Component corresponding with this link
14+
console.log(state.components);
15+
1416
const routeName: string = state.components.find(
1517
(comp: Component) => comp.id === typeId
1618
).name;

app/src/constants/ErrorMessages.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ export default {
22
deleteIndexTitle: 'You cannot delete index.',
33
deleteIndexMessage: 'You must have at least one page.',
44
deleteComponentTitle: 'You cannot delete this component.',
5-
deleteComponentMessage: 'Reusable components inside of a page must be deleted from the page.'
5+
deleteComponentMessage: 'Reusable components inside of a page must be deleted from the page.',
6+
deleteLinkedPageTitle: 'Delete linked page error.',
7+
deleteLinkedPageMessage: 'Please delete the links to this page before deleting the page.'
8+
69
}

app/src/containers/MainContainer.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import React, { Component } from 'react';
1+
import React, { useContext } from 'react';
22
import BottomPanel from '../components/bottom/BottomPanel';
33
import CanvasContainer from '../components/main/CanvasContainer';
4+
import { styleContext } from './AppContainer';
5+
6+
const MainContainer = () => {
7+
const { style } = useContext(styleContext);
48

5-
const MainContainer = ({ style }) => {
69
return (
710
<div className="main-container" style={style} >
811
<div className="main">
912
<CanvasContainer />
1013
</div>
11-
<BottomPanel style={style} />
14+
<BottomPanel />
1215
</div>
1316
);
1417
};

app/src/containers/RightContainer.tsx

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const RightContainer = (): JSX.Element => {
3232
const [BGColor, setBGColor] = useState('');
3333
const [compWidth, setCompWidth] = useState('');
3434
const [compHeight, setCompHeight] = useState('');
35-
const [dialogError, setDialogError] = useState(false);
35+
const [deleteLinkedPageError, setDeleteLinkedPageError] = useState(false);
3636
const [deleteIndexError, setDeleteIndexError] = useState(false);
3737
const [deleteComponentError, setDeleteComponentError] = useState(false);
3838
const { style } = useContext(styleContext);
@@ -152,8 +152,6 @@ const RightContainer = (): JSX.Element => {
152152
const isIndex = (): boolean => configTarget.id === 1;
153153

154154
const isChildOfPage = (): boolean => {
155-
// TODO: refactor
156-
// TODO: output parent name and id to refocus canvas on parent
157155
let isChild: boolean = false;
158156
const { id } = configTarget;
159157
state.components.forEach(comp => {
@@ -164,7 +162,24 @@ const RightContainer = (): JSX.Element => {
164162
});
165163
});
166164
return isChild;
167-
};
165+
}
166+
167+
const isLinkedTo = (): boolean => {
168+
const { id } = configTarget;
169+
const pageName = state.components[id-1].name;
170+
let isLinked = false;
171+
const searchNestedChildren = (comps) => {
172+
if (comps.length === 0) return;
173+
comps.forEach((comp, i) => {
174+
if (comp.type === 'Route Link' && comp.name === pageName) {
175+
isLinked = true;
176+
}
177+
if (comp.children.length > 0) searchNestedChildren(comp.children);
178+
});
179+
}
180+
searchNestedChildren(state.components);
181+
return isLinked;
182+
}
168183

169184
// dispatch to 'UPDATE CSS' called when save button is clicked,
170185
// passing in style object constructed from all changed input values
@@ -191,13 +206,14 @@ const RightContainer = (): JSX.Element => {
191206
dispatch({ type: 'DELETE CHILD', payload: {} });
192207
};
193208

194-
const handlePageDelete = id => () => {
195-
// TODO: return modal
196-
isIndex()
197-
? handleDialogError('index')
198-
: dispatch({ type: 'DELETE PAGE', payload: { id } });
199-
};
200-
209+
const handlePageDelete = (id) => () => {
210+
// TODO: return modal
211+
if (isLinkedTo()) return setDeleteLinkedPageError(true);
212+
isIndex()
213+
? handleDialogError('index')
214+
: dispatch({ type: 'DELETE PAGE', payload: { id }});
215+
}
216+
201217
const handleDeleteReusableComponent = () => {
202218
isChildOfPage()
203219
? handleDialogError('component')
@@ -218,7 +234,8 @@ const RightContainer = (): JSX.Element => {
218234
const handleCloseDialogError = () => {
219235
setDeleteIndexError(false);
220236
setDeleteComponentError(false);
221-
};
237+
setDeleteLinkedPageError(false);
238+
}
222239

223240
return (
224241
<div className="column right" style={style}>
@@ -445,17 +462,21 @@ const RightContainer = (): JSX.Element => {
445462
<ProjectManager />
446463
</div>
447464
<Dialog
448-
open={deleteIndexError}
465+
open={deleteIndexError || deleteLinkedPageError || deleteComponentError}
449466
onClose={handleCloseDialogError}
450467
aria-labelledby="alert-dialog-title"
451468
aria-describedby="alert-dialog-description"
452469
>
453470
<DialogTitle id="alert-dialog-title">
454-
{ErrorMessages.deleteIndexTitle}
471+
{deleteIndexError ? ErrorMessages.deleteIndexTitle : ''}
472+
{deleteComponentError ? ErrorMessages.deleteComponentTitle : ''}
473+
{deleteLinkedPageError ? ErrorMessages.deleteLinkedPageTitle : ''}
455474
</DialogTitle>
456475
<DialogContent>
457476
<DialogContentText id="alert-dialog-description">
458-
{ErrorMessages.deleteIndexMessage}
477+
{deleteIndexError ? ErrorMessages.deleteIndexMessage : ''}
478+
{deleteComponentError ? ErrorMessages.deleteComponentMessage : ''}
479+
{deleteLinkedPageError ? ErrorMessages.deleteLinkedPageMessage : ''}
459480
</DialogContentText>
460481
</DialogContent>
461482
<DialogActions>
@@ -464,7 +485,7 @@ const RightContainer = (): JSX.Element => {
464485
</Button>
465486
</DialogActions>
466487
</Dialog>
467-
<Dialog
488+
{/* <Dialog
468489
open={deleteComponentError}
469490
onClose={handleCloseDialogError}
470491
aria-labelledby="alert-dialog-title"
@@ -483,7 +504,7 @@ const RightContainer = (): JSX.Element => {
483504
OK
484505
</Button>
485506
</DialogActions>
486-
</Dialog>
507+
</Dialog> */}
487508
</div>
488509
);
489510
};

app/src/helperFunctions/renderChildren.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import RouteLink from '../components/main/RouteLink';
1111
// there are four types of direct children that can be rendered on the screen
1212
const renderChildren = (children: ChildElement[]) => {
1313
return children.map((child: ChildElement, i: number) => {
14+
console.log('child', child);
1415
const { type, typeId, style, childId, children, attributes } = child;
1516
// A DirectChildComponent is an instance of a top level component
1617
// This component will render IndirectChild components (div/components rendered inside a child component)

app/src/reducers/componentReducer.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import initialState from '../context/initialState';
99
import generateCode from '../helperFunctions/generateCode';
1010
import cloneDeep from '../helperFunctions/cloneDeep';
11+
import { isValueObject } from 'immutable';
1112

1213
const reducer = (state: State, action: Action) => {
1314
// if the project type is set as Next.js, next component code should be generated
@@ -88,20 +89,6 @@ const reducer = (state: State, action: Action) => {
8889
return;
8990
};
9091

91-
// const isChildOfPage = (id: number): boolean => {
92-
// // TODO: refactor
93-
// // TODO: output parent name and id to refocus canvas on parent
94-
// let isChild: boolean = false;
95-
// state.components.forEach(comp => {
96-
// comp.children.forEach(child => {
97-
// if (child.type === 'Component' && child.typeId === id) {
98-
// isChild = true;
99-
// }
100-
// });
101-
// });
102-
// return isChild;
103-
// }
104-
10592
const updateIds = (components: Component[]) => {
10693
components.forEach((comp, i) => (comp.id = i + 1));
10794
};
@@ -117,18 +104,14 @@ const reducer = (state: State, action: Action) => {
117104
}
118105

119106
switch (action.type) {
120-
// Add a new component type
121-
// add component to the component array and increment our counter for the componentId
122-
// TODO: parse through name to any white spaces and camel case the name
123107
case 'ADD COMPONENT': {
124-
// if nonString value or empty string is passed in, then don't modify the original state
125108
if (
126109
typeof action.payload.componentName !== 'string' ||
127110
action.payload.componentName === ''
128111
)
129112
return state;
130113
const newComponent = {
131-
id: state.nextComponentId,
114+
id: state.components.length + 1,
132115
name: action.payload.componentName,
133116
nextChildId: 1,
134117
style: {},
@@ -151,6 +134,7 @@ const reducer = (state: State, action: Action) => {
151134
};
152135

153136
const nextComponentId = state.nextComponentId + 1;
137+
console.log('new comps', components);
154138
return {
155139
...state,
156140
components,
@@ -184,19 +168,28 @@ const reducer = (state: State, action: Action) => {
184168
}
185169
});
186170
}
187-
171+
188172
if (type === 'Component') {
189173
const originalComponent = findComponent(state.components, typeId);
190174
if (childTypeExists('Component', parentComponentId, originalComponent))
191-
return state;
175+
return state;
192176
}
193-
177+
194178
let newName = state.HTMLTypes.reduce((name, el) => {
195179
if (typeId === el.id) {
196180
name = (type === 'Component') ? componentName : el.tag;
197181
}
198182
return name;
199183
}, '');
184+
185+
if (type === 'Route Link') {
186+
components.find(comp => {
187+
if (comp.id === typeId) {
188+
newName = comp.name;
189+
return;
190+
}
191+
})
192+
}
200193

201194
const newChild: ChildElement = {
202195
type,
@@ -310,6 +303,7 @@ const reducer = (state: State, action: Action) => {
310303
}
311304
case 'DELETE CHILD': {
312305
// if in-focus instance is a top-level component and not a child, don't delete anything
306+
313307
if (!state.canvasFocus.childId) return state;
314308

315309
// find the current component in focus
@@ -339,6 +333,27 @@ const reducer = (state: State, action: Action) => {
339333

340334
case 'DELETE PAGE': {
341335
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);
356+
342357
const components: Component[] = deleteById(id);
343358
updateIds(components);
344359

@@ -347,7 +362,8 @@ const reducer = (state: State, action: Action) => {
347362
components.forEach(comp => {
348363
if (comp.isPage) rootComponents.push(comp.id);
349364
});
350-
365+
//console.log('curr', currentComponents);
366+
console.log('comps', components);
351367
const canvasFocus = { componentId: 1, childId: null }
352368
return {...state, rootComponents, components, canvasFocus}
353369
}

0 commit comments

Comments
 (0)