@@ -9,6 +9,7 @@ import initialState from '../context/initialState';
9
9
import generateCode from '../helperFunctions/generateCode' ;
10
10
import cloneDeep from '../helperFunctions/cloneDeep' ;
11
11
import { isValueObject } from 'immutable' ;
12
+ import Canvas from '../components/main/Canvas' ;
12
13
13
14
const reducer = ( state : State , action : Action ) => {
14
15
// if the project type is set as Next.js, next component code should be generated
@@ -90,23 +91,28 @@ const reducer = (state: State, action: Action) => {
90
91
91
92
const updateIds = ( components : Component [ ] ) => {
92
93
// component IDs should be array index + 1
93
- console . log ( 'component' , components ) ;
94
94
components . forEach ( ( comp , i ) => ( comp . id = i + 1 ) ) ;
95
95
96
- // putting components' name and id into an obj
96
+ // put components' names and ids into an obj
97
97
const obj = { } ;
98
98
components . forEach ( el => {
99
99
obj [ el . name ] = el . id ;
100
100
} ) ;
101
- // update all id and typeid to match one another
102
- const updateAllIds = comp => {
101
+ // update all ids and typeIds to match one another
102
+ const updateAllIds = ( comp : Component [ ] | ChildElement [ ] ) => {
103
+ // for each of the components, if it has children, iterate through that children array
103
104
comp . forEach ( el => {
104
105
if ( el . children . length > 0 ) {
105
106
for ( let i = 0 ; i < el . children . length ; i ++ ) {
107
+ // update each child's childId
106
108
el . children [ i ] . childId = i + 1 ;
109
+ // if the child's name and id exists in the object
107
110
if ( obj [ el . children [ i ] . name ] ) {
111
+ // set the child's typeId to be the value in the object of the child's name key
108
112
el . children [ i ] . typeId = obj [ el . children [ i ] . name ] ;
109
113
}
114
+ // recursively call the updateAllIds function on the child's children array if
115
+ // the child's children array is greater than 0
110
116
if ( el . children [ i ] . children . length > 0 ) {
111
117
updateAllIds ( el . children [ i ] . children ) ;
112
118
}
@@ -137,37 +143,45 @@ const reducer = (state: State, action: Action) => {
137
143
138
144
const updateRoots = ( components : Component [ ] ) => {
139
145
const roots = [ ] ;
146
+ // for each of the components in the passed in array of components, if the child component
147
+ // is a page, push its id into the roots array
140
148
components . forEach ( comp => {
141
149
if ( comp . isPage ) roots . push ( comp . id ) ;
142
150
} ) ;
143
151
return roots ;
144
152
} ;
145
153
146
154
const deleteById = ( id : number ) : Component [ ] => {
155
+ // name of the component we want to delete
147
156
const name : string = state . components [ id - 1 ] . name ;
148
- // console.log('name: ', name);
149
157
150
- const checkChildren = child => {
158
+ const checkChildren = ( child : Component [ ] | ChildElement [ ] ) => {
159
+ // for each of the components in the passed in components array, if the child
160
+ // component has a children array, iterate through the array of children
151
161
child . forEach ( el => {
152
162
if ( el . children . length ) {
153
163
const arr = [ ] ;
154
164
for ( let i = 0 ; i < el . children . length ; i ++ ) {
165
+ // check to see if the name variable doesn't match the name of the child
155
166
if ( el . children [ i ] . name !== name ) {
167
+ // if so, push into the new array the child component
156
168
arr . push ( el . children [ i ] ) ;
157
169
}
158
170
}
171
+ // set the children array to be the new array
159
172
el . children = arr ;
173
+ // recursively call the checkChildren function with the updated children array
160
174
checkChildren ( el . children ) ;
161
175
}
162
176
} ) ;
163
177
} ;
178
+ // creating a copy of the components array
164
179
const copyComp = [ ...state . components ] ;
165
- console . log ( 'before check children' , copyComp ) ;
180
+
166
181
if ( copyComp . length ) {
167
- // for each item in the array, check to see if the children array is not empty
168
182
checkChildren ( copyComp ) ;
169
183
}
170
- console . log ( 'after check children' , copyComp ) ;
184
+
171
185
const filteredArr = [ ...copyComp ] . filter ( comp => comp . id != id ) ;
172
186
return updateIds ( filteredArr ) ;
173
187
} ;
@@ -181,7 +195,6 @@ const reducer = (state: State, action: Action) => {
181
195
182
196
const deleteComponentFromPages = ( components , name ) => {
183
197
const searchNestedComps = childComponents => {
184
- console . log ( 'child components' , childComponents ) ;
185
198
// if (childComponents.length === 0) return console.log('empty children array');
186
199
// childComponents.forEach((comp, i, arr) => {
187
200
// console.log('each individual comp', comp);
@@ -191,10 +204,8 @@ const reducer = (state: State, action: Action) => {
191
204
// });
192
205
} ;
193
206
components . forEach ( comp => {
194
- console . log ( 'current comp' , comp ) ;
195
207
searchNestedComps ( comp . children ) ;
196
208
} ) ;
197
- console . log ( 'comp' , components ) ;
198
209
} ;
199
210
200
211
switch ( action . type ) {
@@ -228,7 +239,6 @@ const reducer = (state: State, action: Action) => {
228
239
} ;
229
240
230
241
const nextComponentId = state . nextComponentId + 1 ;
231
- console . log ( 'add components' , components ) ;
232
242
return {
233
243
...state ,
234
244
components,
@@ -428,45 +438,31 @@ const reducer = (state: State, action: Action) => {
428
438
429
439
case 'DELETE PAGE' : {
430
440
const id : number = state . canvasFocus . componentId ;
431
-
432
441
const components : Component [ ] = deleteById ( id ) ;
433
-
434
442
// rebuild rootComponents with correct page IDs
435
443
const rootComponents = updateRoots ( components ) ;
436
-
437
444
const canvasFocus = { componentId : 1 , childId : null } ;
438
445
return { ...state , rootComponents, components, canvasFocus } ;
439
446
}
440
447
case 'DELETE REUSABLE COMPONENT' : {
441
448
const id : number = state . canvasFocus . componentId ;
442
-
443
449
// updated list of components after deleting a component
444
450
const components : Component [ ] = deleteById ( id ) ;
451
+ const rootComponents : number [ ] = updateRoots ( components ) ;
445
452
446
- const rootComponents = updateRoots ( components ) ;
447
-
448
- // console.log('canvas', state);
449
- // const component = findComponent(
450
- // state.components,
451
- // state.canvasFocus.componentId
452
- // );
453
- // console.log('component: ', component);
454
- // const { directParent, childIndexValue } = findParent(
455
- // component,
456
- // state.canvasFocus.componentId
457
- // );
458
- // console.log('childIndexValue: ', childIndexValue);
459
- // console.log('directparent', directParent);
460
- // const child = { ...directParent.children[childIndexValue] };
461
- // directParent.children.splice(childIndexValue, 1);
462
-
463
- // component.code = generateCode(
464
- // [...state.components],
465
- // id,
466
- // [...state.rootComponents],
467
- // state.projectType,
468
- // state.HTMLTypes
469
- // );
453
+ // iterate over the length of the components array
454
+ for ( let i = 0 ; i < components . length ; i ++ ) {
455
+ // for each components' code, run the generateCode function to
456
+ // update the code preview on the app
457
+ components [ i ] . code = generateCode (
458
+ components ,
459
+ components [ i ] . id ,
460
+ rootComponents ,
461
+ state . projectType ,
462
+ state . HTMLTypes
463
+ ) ;
464
+ }
465
+ console . log ( 'components' , components ) ;
470
466
471
467
const canvasFocus = { componentId : 1 , childId : null } ;
472
468
0 commit comments