@@ -26,7 +26,6 @@ const reducer = (state: State, action: Action) => {
26
26
// We're going to keep track of the nodes we need to search through with an Array
27
27
// Initialize this array with the top level node
28
28
const nodeArr : ( Component | ChildElement ) [ ] = [ component ] ;
29
- nodeArr . forEach ( node => console . log ( 'node' , node ) ) ;
30
29
// iterate through each node in the array as long as there are elements in the array
31
30
while ( nodeArr . length > 0 ) {
32
31
// shift off the first value and assign to an element
@@ -93,6 +92,14 @@ const reducer = (state: State, action: Action) => {
93
92
components . forEach ( ( comp , i ) => ( comp . id = i + 1 ) ) ;
94
93
} ;
95
94
95
+ const updateRoots = ( components : Component [ ] ) => {
96
+ const roots = [ ] ;
97
+ components . forEach ( comp => {
98
+ if ( comp . isPage ) roots . push ( comp . id ) ;
99
+ } ) ;
100
+ return roots ;
101
+ }
102
+
96
103
const deleteById = ( id : number ) : Component [ ] =>
97
104
[ ...state . components ] . filter ( comp => comp . id != id ) ;
98
105
@@ -110,6 +117,9 @@ const reducer = (state: State, action: Action) => {
110
117
action . payload . componentName === ''
111
118
)
112
119
return state ;
120
+
121
+ const components = [ ...state . components ] ;
122
+
113
123
const newComponent = {
114
124
id : state . components . length + 1 ,
115
125
name : action . payload . componentName ,
@@ -120,7 +130,6 @@ const reducer = (state: State, action: Action) => {
120
130
isPage : action . payload . root
121
131
} ;
122
132
123
- const components = [ ...state . components ] ;
124
133
components . push ( newComponent ) ;
125
134
126
135
const rootComponents = [ ...state . rootComponents ] ;
@@ -134,7 +143,6 @@ const reducer = (state: State, action: Action) => {
134
143
} ;
135
144
136
145
const nextComponentId = state . nextComponentId + 1 ;
137
- console . log ( 'new comps' , components ) ;
138
146
return {
139
147
...state ,
140
148
components,
@@ -190,10 +198,9 @@ const reducer = (state: State, action: Action) => {
190
198
}
191
199
} )
192
200
}
193
-
194
201
const newChild : ChildElement = {
195
202
type,
196
- typeId,
203
+ typeId : components . length + 1 ,
197
204
name : newName ,
198
205
childId : state . nextChildId ,
199
206
style : { } ,
@@ -327,43 +334,20 @@ const reducer = (state: State, action: Action) => {
327
334
state . projectType ,
328
335
state . HTMLTypes
329
336
) ;
337
+
330
338
const canvasFocus = { ...state . canvasFocus , childId : null } ;
331
339
return { ...state , components, canvasFocus } ;
332
340
}
333
341
334
342
case 'DELETE PAGE' : {
335
343
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
344
357
345
const components : Component [ ] = deleteById ( id ) ;
358
346
updateIds ( components ) ;
359
347
360
348
// rebuild rootComponents with correct page IDs
361
- const rootComponents : number [ ] = [ ] ;
362
- components . forEach ( comp => {
363
- if ( comp . isPage ) rootComponents . push ( comp . id ) ;
364
- } ) ;
365
- //console.log('curr', currentComponents);
366
- console . log ( 'comps' , components ) ;
349
+ const rootComponents = updateRoots ( components ) ;
350
+
367
351
const canvasFocus = { componentId : 1 , childId : null }
368
352
return { ...state , rootComponents, components, canvasFocus}
369
353
}
@@ -372,7 +356,9 @@ const reducer = (state: State, action: Action) => {
372
356
const components : Component [ ] = deleteById ( id ) ;
373
357
updateIds ( components ) ;
374
358
const canvasFocus = { componentId : 1 , childId : null } ;
375
- return { ...state , components, canvasFocus, nextComponentId : id } ;
359
+
360
+ const rootComponents = updateRoots ( components ) ;
361
+ return { ...state , rootComponents, components, canvasFocus, nextComponentId : id } ;
376
362
}
377
363
378
364
case 'SET INITIAL STATE' : {
@@ -439,7 +425,6 @@ const reducer = (state: State, action: Action) => {
439
425
components,
440
426
canvasFocus
441
427
} ;
442
- // return { ...initialState };
443
428
}
444
429
445
430
case 'UPDATE PROJECT NAME' : {
@@ -451,9 +436,7 @@ const reducer = (state: State, action: Action) => {
451
436
}
452
437
453
438
case 'OPEN PROJECT' : {
454
- console . log ( "BEFORE FUNCTION: " , action . payload . HTMLTypes ) ;
455
439
convertToJSX ( action . payload . HTMLTypes ) ;
456
- console . log ( "AFTER FUNCTION: " , action . payload . HTMLTypes ) ;
457
440
return {
458
441
...action . payload
459
442
} ;
0 commit comments