Skip to content

Commit fe0fc8c

Browse files
committed
fixed recursive call in handleSeparator function with deletion, fixed reducer to handle separators for more than one page
1 parent 6b82f1c commit fe0fc8c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

app/src/helperFunctions/manageSeparators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ manageSeparators.handleSeparators = (arr, str) => {
1111
if (str === 'delete' && arr.length === 1) {
1212
arr.splice(0, 1);
1313
}
14+
1415
for (let index = 0; index < arr.length; index++) {
1516
if (arr[index].name === 'separator' && arr[index + 1].name === 'separator') {
1617
arr.splice(index, 1); // removes extra separator from array
@@ -36,7 +37,7 @@ manageSeparators.handleSeparators = (arr, str) => {
3637
// check is length is > 0 or it is a nested element
3738
if (arr[index].children.length) {
3839
// recursive call if children array
39-
manageSeparators.handleSeparators(arr[index].children);
40+
(str === 'delete') ? manageSeparators.handleSeparators(arr[index].children, str) : manageSeparators.handleSeparators(arr[index].children);
4041
}
4142
}
4243
return manageSeparators.nextTopSeparatorId;

app/src/reducers/componentReducer.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ const reducer = (state: State, action: Action) => {
202202
return state;
203203

204204
const components = [...state.components];
205+
205206
const newComponent = {
206207
id: state.components.length + 1,
207208
name: action.payload.componentName,
@@ -224,6 +225,7 @@ const reducer = (state: State, action: Action) => {
224225
};
225226

226227
const nextComponentId = state.nextComponentId + 1;
228+
console.log('add component', components)
227229
return {
228230
...state,
229231
components,
@@ -242,7 +244,7 @@ const reducer = (state: State, action: Action) => {
242244

243245
const parentComponentId: number = state.canvasFocus.componentId;
244246
const components = [...state.components];
245-
247+
console.log('add child', components)
246248

247249
// find component (an object) that we're adding a child to
248250
const parentComponent = findComponent(components, parentComponentId);
@@ -327,11 +329,13 @@ const reducer = (state: State, action: Action) => {
327329
};
328330
const nextChildId = state.nextChildId + 1;
329331
let nextTopSeparatorId = state.nextTopSeparatorId + 1;
330-
331-
let addChildArray = components[0].children;
332+
console.log('canvasFocus', canvasFocus)
333+
console.log('components',components)
334+
// let addChildArray = components[0].children;
335+
let addChildArray = components[canvasFocus.componentId-1].children
332336
addChildArray = manageSeparators.mergeSeparator(addChildArray, 1);
333337
if (directParent && directParent.name === 'separator') nextTopSeparatorId = manageSeparators.handleSeparators(addChildArray, 'add');
334-
components[0].children = addChildArray;
338+
components[canvasFocus.componentId-1].children = addChildArray;
335339

336340
return { ...state, components, nextChildId, canvasFocus, nextTopSeparatorId };
337341
}
@@ -380,8 +384,8 @@ const reducer = (state: State, action: Action) => {
380384

381385
let nextTopSeparatorId = state.nextTopSeparatorId;
382386

383-
components[0].children = manageSeparators.mergeSeparator(components[0].children, 0);
384-
nextTopSeparatorId = manageSeparators.handleSeparators(components[0].children, 'change position')
387+
components[state.canvasFocus.componentId-1].children = manageSeparators.mergeSeparator(components[state.canvasFocus.componentId-1].children, 0);
388+
nextTopSeparatorId = manageSeparators.handleSeparators(components[state.canvasFocus.componentId-1].children, 'change position')
385389
return { ...state, components, nextTopSeparatorId };
386390
}
387391
// Change the focus component and child
@@ -448,8 +452,9 @@ const reducer = (state: State, action: Action) => {
448452
);
449453

450454

451-
let nextTopSeparatorId = manageSeparators.handleSeparators(components[0].children, 'delete')
452455
const canvasFocus = { ...state.canvasFocus, childId: null };
456+
console.log('before invoking handleSep')
457+
let nextTopSeparatorId = manageSeparators.handleSeparators(components[canvasFocus.componentId-1].children, 'delete')
453458
return { ...state, components, canvasFocus, nextTopSeparatorId };
454459
}
455460

@@ -538,7 +543,6 @@ const reducer = (state: State, action: Action) => {
538543
case 'RESET STATE': {
539544
const nextChildId = 1;
540545
const nextTopSeparatorId = 1000;
541-
const nextBottomSeparatorId = 5000;
542546
const rootComponents = [1];
543547
const nextComponentId = 2;
544548
const canvasFocus = {
@@ -557,7 +561,6 @@ const reducer = (state: State, action: Action) => {
557561
...state,
558562
nextChildId,
559563
nextTopSeparatorId,
560-
nextBottomSeparatorId,
561564
rootComponents,
562565
nextComponentId,
563566
components,

0 commit comments

Comments
 (0)