Skip to content

Commit 7f84fe6

Browse files
committed
Delete functionality for reusable components and pages
2 parents be891e0 + c3caf96 commit 7f84fe6

File tree

8 files changed

+106
-15
lines changed

8 files changed

+106
-15
lines changed

app/electron/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,5 @@ ipcMain.on('github', event => {
396396
}
397397
});
398398
});
399+
400+
//module.exports = dialog;

app/src/components/left/ComponentPanel.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const ComponentPanel = (): JSX.Element => {
8282
};
8383

8484
const handleNameSubmit = () => {
85-
let letters = /[a-z]/;
85+
let letters = /[a-zA-Z]/;
8686
if (!compName.charAt(0).match(letters)) {
8787
triggerError('letters');
8888
return;
@@ -106,6 +106,12 @@ const ComponentPanel = (): JSX.Element => {
106106
return state.canvasFocus.componentId === targetId ? true : false;
107107
};
108108

109+
const deleteReusableComponent = (id) => {
110+
// reducer to modify state.components
111+
// make sure the component is not a root
112+
//
113+
}
114+
109115
return (
110116
<div className={classes.panelWrapper}>
111117
{/* Add a new component*/}
@@ -155,30 +161,35 @@ const ComponentPanel = (): JSX.Element => {
155161
<Grid container direction="row" justify="center" alignItems="center">
156162
{state.components
157163
.filter(comp => state.rootComponents.includes(comp.id))
158-
.map(comp => (
164+
.map(comp => {
165+
//console.log('root comp', comp.name)
166+
return (
159167
<ComponentPanelItem
160168
isFocus={isFocus(comp.id)}
161169
key={`comp-${comp.id}`}
162170
name={comp.name}
163171
id={comp.id}
164172
root={true}
165173
/>
166-
))}
174+
)})}
167175
</Grid>
168176
{/* Display all reusable components */}
169177
<h4>Reusable components</h4>
170178
<Grid container direction="row" justify="center" alignItems="center">
171179
{state.components
172180
.filter(comp => !state.rootComponents.includes(comp.id))
173-
.map(comp => (
181+
.map(comp => {
182+
//console.log('all root comps', state.rootComponents);
183+
//console.log('all reusable comps', state.components);
184+
return (
174185
<ComponentPanelItem
175186
isFocus={isFocus(comp.id)}
176187
key={`comp-${comp.id}`}
177188
name={comp.name}
178189
id={comp.id}
179190
root={false}
180191
/>
181-
))}
192+
)})}
182193
</Grid>
183194
{/* Display navigation components - (only applies to next.js which has routing built in) */}
184195
{state.projectType === 'Next.js' ? (

app/src/components/main/Canvas.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function Canvas() {
3030
const [{ isOver }, drop] = useDrop({
3131
accept: ItemTypes.INSTANCE,
3232
drop: (item: DragItem, monitor: DropTargetMonitor) => {
33+
//console.log(isOver);
3334
const didDrop = monitor.didDrop(); // returns false for direct drop target
3435
if (didDrop) {
3536
return;
@@ -48,6 +49,7 @@ function Canvas() {
4849
}
4950
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
5051
else {
52+
console.log('dispatch change position')
5153
dispatch({
5254
type: 'CHANGE POSITION',
5355
payload: {
@@ -73,6 +75,8 @@ function Canvas() {
7375
// Combine the default styles of the canvas with the custom styles set by the user for that component
7476
// The render children function renders all direct children of a given component
7577
// Direct children are draggable/clickable
78+
//console.log('curr comp =>', currentComponent);
79+
//console.log('curr comp style', currentComponent.style);
7680
const canvasStyle = combineStyles(defaultCanvasStyle, currentComponent.style);
7781
return (
7882
<div ref={drop} style={canvasStyle} onClick={onClickHandler}>

app/src/components/main/DirectChildComponent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
7272
const renderIndirectChildren = (
7373
referencedComponent: Component | ChildElement
7474
) => {
75+
console.log('referenced comp => ', referencedComponent);
7576
// iterate through all children of referenced
7677
return referencedComponent.children.map(child => {
7778
if (child.type === 'Component') {

app/src/containers/RightContainer.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const RightContainer = (props): JSX.Element => {
2525
const [compHeight, setCompHeight] = useState('');
2626

2727
const resetFields = () => {
28+
//console.log(configTarget);
29+
//console.log(configTarget.children);
2830
const style = configTarget.child
2931
? configTarget.child.style
3032
: configTarget.style;
@@ -165,6 +167,17 @@ const RightContainer = (props): JSX.Element => {
165167
dispatch({ type: 'DELETE PAGE', payload: { id }});
166168
}
167169

170+
const handleDeleteReusableComponent = () => {
171+
dispatch({ type: 'DELETE REUSABLE COMPONENT', payload: {} });
172+
}
173+
174+
const isReusable = (configTarget): boolean => {
175+
return state.components
176+
.filter(comp => !state.rootComponents
177+
.includes(comp.id))
178+
.some(el => el.id == configTarget.id);
179+
}
180+
168181
return (
169182
<div className="column right ">
170183
<div className="rightPanelWrapper">
@@ -366,12 +379,22 @@ const RightContainer = (props): JSX.Element => {
366379
</Button>
367380
</div>
368381
) : (isPage(configTarget) ? (
369-
<div className={classes.buttonRow}>
382+
<div className={classes.buttonRow}>
370383
<Button
371384
color="secondary"
372385
className={classes.button}
373386
onClick={handlePageDelete(configTarget.id)}
374-
>
387+
>
388+
DELETE PAGE
389+
</Button>
390+
</div>
391+
) : isReusable(configTarget) ? (
392+
<div className={classes.buttonRow}>
393+
<Button
394+
color="secondary"
395+
className={classes.button}
396+
onClick={handleDeleteReusableComponent}
397+
>
375398
DELETE PAGE
376399
</Button>
377400
</div>

app/src/helperFunctions/generateCode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ const generateCode = (
245245
rootComponents: number[],
246246
projectType: string
247247
) => {
248+
// console.log('components', components);
249+
// console.log('componentId', componentId);
250+
// console.log('rootComponents', rootComponents);
251+
// console.log('projectType', projectType);
248252
const code = generateUnformattedCode(
249253
components,
250254
componentId,

app/src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
const PrivateRoute = ({ component: Component, ...rest }) => (
1818
<Route
1919
{...rest}
20-
render={props =>
21-
Cookies.get('ssid') || window.localStorage.getItem('ssid') ? (
20+
render={props => {
21+
//console.log(props);
22+
return Cookies.get('ssid') || window.localStorage.getItem('ssid') ? (
2223
<Component {...props} />
23-
) : (
24-
<Redirect to="/login" />
25-
)
24+
) : (
25+
<Redirect to="/login" />
26+
)
27+
}
2628
}
2729
/>
2830
);

app/src/reducers/componentReducer.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const reducer = (state: State, action: Action) => {
2424
// We're going to keep track of the nodes we need to search through with an Array
2525
// Initialize this array with the top level node
2626
const nodeArr: (Component | ChildElement)[] = [component];
27-
27+
nodeArr.forEach(node => console.log('node', node));
28+
//console.log('nodeArr', nodeArr);
2829
// iterate through each node in the array as long as there are elements in the array
2930
while (nodeArr.length > 0) {
3031
// shift off the first value and assign to an element
@@ -87,8 +88,25 @@ const reducer = (state: State, action: Action) => {
8788
return;
8889
};
8990

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+
console.log('comp =>', comp);
97+
comp.children.forEach(child => {
98+
if (child.type === 'Component' && child.typeId === id) {
99+
isChild = true;
100+
console.log('parent name =>', comp.name);
101+
console.log('parent id =>', comp.name);
102+
}
103+
});
104+
});
105+
return isChild;
106+
}
107+
90108
const updateIds = (components: Array<Object>) => {
91-
components.forEach((comp, i) => comp.id = i + 1)
109+
components.forEach((comp, i) => comp.id = i + 1);
92110
}
93111

94112
switch (action.type) {
@@ -273,13 +291,15 @@ const reducer = (state: State, action: Action) => {
273291
components,
274292
state.canvasFocus.componentId
275293
);
276-
294+
console.log('curr comp', component);
277295
// find the moved element's former parent
278296
// delete the element from it's former parent's children array
279297
const { directParent, childIndexValue } = findParent(
280298
component,
281299
state.canvasFocus.childId
282300
);
301+
console.log('direct parent', directParent);
302+
console.log('child index', childIndexValue);
283303
const child = { ...directParent.children[childIndexValue] };
284304
directParent.children.splice(childIndexValue, 1);
285305
component.code = generateCode(
@@ -304,6 +324,30 @@ const reducer = (state: State, action: Action) => {
304324
const canvasFocus = { componentId: 1, childId: null }
305325
return {...state, components, canvasFocus}
306326
}
327+
case 'DELETE REUSABLE COMPONENT' : {
328+
// TODO: bug when deleting element inside page
329+
// can't edit component name
330+
// happens sometimes. not sure exactly when
331+
332+
const id: number = state.canvasFocus.componentId;
333+
// check if component is a child element of a page
334+
if(isChildOfPage(id)) {
335+
// TODO: include name of parent in alert
336+
// TODO: change canvas focus to parent
337+
//dialog.showErrorBox('error','Reusable components inside of a page must be deleted from the page');
338+
alert('Reusable components inside of a page must be deleted from the page');
339+
//const canvasFocus:Object = { componentId: id, childId: null };
340+
return { ...state }
341+
}
342+
// filter out components that don't match id
343+
const components: Array<Object> = [...state.components].filter(comp => comp.id != id);
344+
345+
updateIds(components);
346+
347+
// TODO: temporary fix. should point to id directly
348+
const canvasFocus = { componentId: 1, childId: null };
349+
return {...state, components, canvasFocus};
350+
}
307351

308352
case 'SET INITIAL STATE': {
309353
// set the canvas focus to be the first component

0 commit comments

Comments
 (0)