Skip to content

Commit ddc4caf

Browse files
committed
Resolved bugs for 1) components on the canvas not receiving key props and 2) the app crashing when clearing the workspace
1 parent d754e4c commit ddc4caf

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

app/src/components/main/DirectChildComponent.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
5757
boxShadow:
5858
state.canvasFocus.childId === childId ? '1px 1px 3px rgb(11,212,112)' : ''
5959
};
60+
6061
const combinedStyle = combineStyles(
6162
combineStyles(
6263
combineStyles(globalDefaultStyle, referencedComponent.style),
@@ -109,7 +110,13 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
109110
const HTMLDefaultPlacholder = HTMLType.placeHolderShort;
110111
const combinedStyle = combineStyles(HTMLDefaultStyle, child.style);
111112
return (
112-
<React.Fragment>
113+
<React.Fragment
114+
key={
115+
'indChildFrag' +
116+
child.childId.toString() +
117+
child.typeId.toString()
118+
}
119+
>
113120
{child.children.length === 0 ? (
114121
<IndirectChild
115122
style={combinedStyle}
@@ -161,7 +168,7 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
161168
return (
162169
<div
163170
onClick={onClickHandler}
164-
key={'childComp' + childId}
171+
// key={'childComp' + childId}
165172
style={combinedStyle}
166173
ref={drag}
167174
>

app/src/helperFunctions/renderChildren.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const renderChildren = (children: ChildElement[]) => {
2121
type={type}
2222
typeId={typeId}
2323
style={style}
24-
key={childId.toString()}
24+
key={'DirChild' + childId.toString()}
2525
/>
2626
);
2727
}
@@ -33,7 +33,7 @@ const renderChildren = (children: ChildElement[]) => {
3333
type={type}
3434
typeId={typeId}
3535
style={style}
36-
key={childId.toString()}
36+
key={'DirChild' + childId.toString()}
3737
/>
3838
);
3939
}
@@ -46,7 +46,7 @@ const renderChildren = (children: ChildElement[]) => {
4646
typeId={typeId}
4747
style={style}
4848
children={children}
49-
key={childId.toString()}
49+
key={'DirChild' + childId.toString()}
5050
/>
5151
);
5252
}
@@ -60,7 +60,7 @@ const renderChildren = (children: ChildElement[]) => {
6060
typeId={typeId}
6161
style={style}
6262
children={children}
63-
key={childId.toString()}
63+
key={'DirChild' + childId.toString()}
6464
/>
6565
);
6666
}

app/src/reducers/componentReducer.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '../interfaces/Interfaces';
77
import initialState from '../context/initialState';
88
import generateCode from '../helperFunctions/generateCode';
9+
import cloneDeep from '../helperFunctions/cloneDeep';
910

1011
const reducer = (state: State, action: Action) => {
1112
// if the project type is set as Next.js, next component code should be generated
@@ -133,7 +134,7 @@ const reducer = (state: State, action: Action) => {
133134
type,
134135
typeId,
135136
childId
136-
}: { type: string; typeId: number; childId: any} = action.payload;
137+
}: { type: string; typeId: number; childId: any } = action.payload;
137138
// the parent of the new child is whichever component that is currently focused on
138139
const parentComponentId: number = state.canvasFocus.componentId;
139140

@@ -286,7 +287,14 @@ const reducer = (state: State, action: Action) => {
286287
}
287288

288289
case 'SET INITIAL STATE': {
289-
return { ...action.payload };
290+
// set the canvas focus to be the first component
291+
const canvasFocus = {
292+
...action.payload.canvasFocus,
293+
componentId: 1,
294+
childId: null
295+
};
296+
297+
return { ...action.payload, canvasFocus };
290298
}
291299
case 'SET PROJECT NAME': {
292300
return {
@@ -314,9 +322,32 @@ const reducer = (state: State, action: Action) => {
314322

315323
return { ...state, components, projectType };
316324
}
317-
// Reset state of a given project back to its initial state
325+
// Reset all component data back to their initial state but maintain the user's project name and log-in status
318326
case 'RESET STATE': {
319-
return { ...initialState };
327+
const nextChildId = 1;
328+
const rootComponents = [1];
329+
const nextComponentId = 2;
330+
const canvasFocus = {
331+
...state.canvasFocus,
332+
componentId: 1,
333+
childId: null
334+
};
335+
const rootComponent = {
336+
...state.components[0],
337+
code: '',
338+
children: [],
339+
style: {}
340+
};
341+
const components = [rootComponent];
342+
return {
343+
...state,
344+
nextChildId,
345+
rootComponents,
346+
nextComponentId,
347+
components,
348+
canvasFocus
349+
};
350+
// return { ...initialState };
320351
}
321352

322353
case 'UPDATE PROJECT NAME': {

0 commit comments

Comments
 (0)