@@ -120,55 +120,39 @@ export const addChild = (state, { title }) => {
120
120
} ;
121
121
} ;
122
122
123
- export const deleteChild = ( state , { } ) => {
124
- // The child to delete is the FOCUSED child
125
- // THE parent is the FOCUSED component
126
-
127
- console . log ( ( `Focused Component (parent) id:${ state . focusComponent . id } title:${ state . focusComponent . title }
128
- Focused Child (the one 2 be deleted) ChildID:${ state . focusChild . childId } ` )
129
- )
130
-
131
- if ( ! state . focusComponent . id ) return window . alert ( `Cannot delete Child if Focused component id = ZERO` )
132
- if ( ! state . focusChild . childId ) return window . alert ( `Cannot delete Child if Focused Child id = ZERO` )
133
- // I'm not sure what the "focused component" is (a reference, a copy or ?
134
- // So, let's get a reference to the focused component ..
135
- const currFocusedComponent = state . components . find ( c => c . id == state . focusComponent . id ) ;
136
- console . log ( 'currFocusedComponent: ' , currFocusedComponent )
137
- // copy the Children Array of the foxused component
138
-
139
- console . log ( 'Wtf ?' )
140
- const copyOfChildrenArray = [ ...currFocusedComponent . childrenArray ]
141
- console . log ( 'copyOfChildrenArray' , copyOfChildrenArray )
142
- console . log ( 'after copy : state.focusChild.childId:' , state . focusChild . childId )
143
- // delete the SELECTED CHILD from the copied array
144
- const indexToDelete = copyOfChildrenArray . findIndex ( elem => elem . childId == state . focusChild . childId )
145
- console . log ( `Index to delete : ${ indexToDelete } ` )
146
-
147
- if ( indexToDelete < 0 ) return window . alert ( `DeleteChild speaking here. The chils u r trying to delete was not found` ) ;
148
-
149
- copyOfChildrenArray . splice ( indexToDelete , 1 )
150
-
151
- //Make a copy of the focused component - this is the one we will modify
152
- const copyOfFocusedComponent = {
153
- ...currFocusedComponent ,
154
- childrenArray : copyOfChildrenArray
155
- }
123
+ export const deleteChild = ( state ,
124
+ { parentId = state . focusComponent . id
125
+ , childId = state . focusChild . childId
126
+ , calledFromDeleteComponent = false
127
+ } ) => {
128
+
129
+ //console.log(`delete child here. state.focusChild.childId = ${state.focusChild.childId} state.focusComponent.id=${state.focusComponent.id}`)
130
+ /**************************************************
131
+ if no parameters are provided we default to delete the FOCUSED CHILD of the FOCUSED COMPONENTS
132
+ however when deleting component we wnt to delete ALL the places where it's used, so we call this function
133
+ Also when calling from DELETE components , we do not touch focusCOmponent.
134
+ *************************************************************************************/
135
+ if ( ! parentId ) { window . alert ( `Cannot delete Child if parent id = ZERO ` ) ; return state }
136
+ if ( ! childId ) { window . alert ( `Cannot delete Child if Child id = ZERO` ) ; return state }
137
+ console . log ( `delete child parentid: ${ parentId } cildId: ${ childId } ` )
138
+ // make a DEEP copy of the parent component (the one thats about to loose a child)
139
+ const parentComponentCopy = JSON . parse ( JSON . stringify ( state . components . find ( c => c . id == parentId ) ) ) ;
140
+
141
+ // delete the CHILD from the copied array
142
+ const indexToDelete = parentComponentCopy . childrenArray . findIndex ( elem => elem . childId == childId )
143
+ if ( indexToDelete < 0 ) return window . alert ( `DeleteChild speaking here. The child u r trying to delete was not found` ) ;
144
+ parentComponentCopy . childrenArray . splice ( indexToDelete , 1 )
156
145
157
- console . log ( `copyOfFocusedComponent:` )
158
- console . log ( copyOfFocusedComponent )
159
- // copy the entire compoenents array from state
160
146
const modifiedComponentArray = [
161
- ...state . components . filter ( c => c . id !== currFocusedComponent . id ) , // all elements besides the one just changed
162
- copyOfFocusedComponent
147
+ ...state . components . filter ( c => c . id !== parentId ) , // all elements besides the one just changed
148
+ parentComponentCopy
163
149
]
164
- console . log ( 'modifiedComponentArray' )
165
- console . log ( modifiedComponentArray )
166
150
167
151
// RETURN - update state...
168
152
return {
169
153
...state ,
170
154
components : modifiedComponentArray ,
171
- focusComponent : copyOfFocusedComponent , // problem! the delete works on State.components, but not on FOcus component.. starnge..
155
+ focusComponent : calledFromDeleteComponent ? state . focusComponent : parentComponentCopy , // when called from delete component we dont need want to touch the focus
172
156
focusChild : { } // reset to blank.
173
157
174
158
}
@@ -235,56 +219,61 @@ export const handleTransform = (
235
219
236
220
// };
237
221
238
- export const updateComponent = (
239
- state ,
240
- { id, newParentId = null , color = null , stateful = null , props = null }
241
- ) => {
242
- let component ;
243
- const components = state . components . map ( comp => {
244
- if ( comp . id === id ) {
245
- component = { ...comp } ;
246
- if ( newParentId ) {
247
- const parentIdsSet = new Set ( component . parentIds ) ;
248
- if ( parentIdsSet . has ( newParentId ) ) {
249
- parentIdsSet . delete ( newParentId ) ;
250
- } else {
251
- parentIdsSet . add ( newParentId ) ;
252
- }
253
- component . parentIds = [ ...parentIdsSet ] ;
254
- }
255
- if ( props ) {
256
- component . props = props ;
257
- component . nextPropId += 1 ;
258
- }
259
- component . color = color || component . color ;
260
- component . stateful = stateful === null ? component . stateful : stateful ;
261
- return component ;
262
- }
263
- return comp ;
264
- } ) ;
222
+ // export const updateComponent = (
223
+ // state,
224
+ // { id, newParentId = null, color = null, stateful = null, props = null }
225
+ // ) => {
226
+ // let component;
227
+ // const components = state.components.map(comp => {
228
+ // if (comp.id === id) {
229
+ // component = { ...comp };
230
+ // if (newParentId) {
231
+ // const parentIdsSet = new Set(component.parentIds);
232
+ // if (parentIdsSet.has(newParentId)) {
233
+ // parentIdsSet.delete(newParentId);
234
+ // } else {
235
+ // parentIdsSet.add(newParentId);
236
+ // }
237
+ // component.parentIds = [...parentIdsSet];
238
+ // }
239
+ // if (props) {
240
+ // component.props = props;
241
+ // component.nextPropId += 1;
242
+ // }
243
+ // component.color = color || component.color;
244
+ // component.stateful = stateful === null ? component.stateful : stateful;
245
+ // return component;
246
+ // }
247
+ // return comp;
248
+ // });
265
249
266
- return {
267
- ...state ,
268
- components,
269
- focusComponent : component
270
- } ;
271
- } ;
250
+ // return {
251
+ // ...state,
252
+ // components,
253
+ // focusComponent: component
254
+ // };
255
+ // };
272
256
273
257
// Delete component with the index for now, but will be adjusted to use id
274
- export const deleteComponent = ( state , { index, id } ) => {
275
- const { focusComponent } = state ;
276
- const components = [
277
- ...state . components . slice ( 0 , index ) ,
278
- ...state . components . slice ( index + 1 )
279
- ] ;
280
258
259
+ export const deleteComponent = ( state , { componentId } ) => {
260
+ // const { focusComponent } = state;
261
+ // const components = [
262
+ // ...state.components.slice(0, index),
263
+ // ...state.components.slice(index + 1)
264
+ // ];
265
+ const indexToDelete = state . components . findIndex ( comp => comp . id == componentId ) ;
266
+ console . log ( 'index to delete: ' , indexToDelete )
267
+
268
+ const componentsCopy = JSON . parse ( JSON . stringify ( state . components ) )
269
+ componentsCopy . splice ( indexToDelete , 1 )
281
270
const totalComponents = state . totalComponents - 1 ;
282
271
272
+ console . log ( `Real delete component action here : id:${ componentId } ` )
283
273
return {
284
274
...state ,
285
275
totalComponents,
286
- components,
287
- focusComponent : focusComponent . id === id ? { } : focusComponent
276
+ components : componentsCopy ,
288
277
} ;
289
278
} ;
290
279
0 commit comments