@@ -32,7 +32,9 @@ export const addComponent = (state, { title }) => {
32
32
. replace ( / [ - _ \s 0 - 9 \W ] + / gi, '' ) ;
33
33
34
34
if ( state . components . find ( comp => comp . title === strippedTitle ) ) {
35
- // alert the user that duplicate component names are not allowed
35
+ window . alert (
36
+ `a component with the name: "${ strippedTitle } " already exists.\n Please think of another name.` ,
37
+ ) ;
36
38
return {
37
39
...state ,
38
40
} ;
@@ -68,18 +70,17 @@ export const addComponent = (state, { title }) => {
68
70
// get the focus component (aka the component were adding the child to)
69
71
70
72
export const addChild = ( state , { title } ) => {
71
- const strippedTitle = title
72
- . replace ( / [ a - z ] + / gi, word => word [ 0 ] . toUpperCase ( ) + word . slice ( 1 ) )
73
- . replace ( / [ - _ \s 0 - 9 \W ] + / gi, '' ) ;
73
+ const strippedTitle = title ;
74
+ // .replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
75
+ // .replace(/[-_\s0-9\W]+/gi, "" );
74
76
75
- // view represents the component that this child will live (and be rendered) in
77
+ // view represents the curretn FOCUSED COMPONENT - this is the component where the child is being added to
78
+ // we only add childrent (or do any action) to the focused omconent
76
79
const view = state . components . find ( comp => comp . title === state . focusComponent . title ) ;
77
80
78
81
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
79
82
const parentComponent = state . components . find ( comp => comp . title === title ) ;
80
83
81
- console . log ( 'view from addChild: ' , view ) ;
82
-
83
84
const newChild = {
84
85
childId : view . nextChildId . toString ( ) ,
85
86
childComponentId : parentComponent . id ,
@@ -117,6 +118,48 @@ export const addChild = (state, { title }) => {
117
118
} ;
118
119
} ;
119
120
121
+ export const deleteChild = ( state , { } ) => {
122
+ // The child to delete is the FOCUSED child
123
+ // THE parent is the FOCUSED component
124
+
125
+ if ( ! state . focusComponent . id ) return window . alert ( 'Cannot delete Child if Focused component id = ZERO' ) ;
126
+ if ( ! state . focusChild . childId ) return window . alert ( 'Cannot delete Child if Focused Child id = ZERO' ) ;
127
+ // I'm not sure what the "focused component" is (a reference, a copy or ?
128
+ // So, let's get a reference to the focused component ..
129
+ const currFocusedComponent = state . components . find ( c => c . id == state . focusComponent . id ) ;
130
+
131
+ // copy the Children Array of the foxused component
132
+ const copyOfChildrenArray = [ ...currFocusedComponent . childrenArray ] ;
133
+ // delete the SELECTED CHILD from the copied array
134
+ const indexToDelete = copyOfChildrenArray . findIndex (
135
+ elem => elem . childId == state . focusChild . childId ,
136
+ ) ;
137
+
138
+ if ( indexToDelete < 0 ) return window . alert ( 'DeleteChild speaking here. The chils u r trying to delete was not found' ) ;
139
+
140
+ copyOfChildrenArray . splice ( indexToDelete , 1 ) ;
141
+
142
+ // Make a copy of the focused component - this is the one we will modify
143
+ const copyOfFocusedComponent = {
144
+ ...currFocusedComponent ,
145
+ childrenArray : copyOfChildrenArray ,
146
+ } ;
147
+
148
+ // copy the entire compoenents array from state
149
+ const modifiedComponentArray = [
150
+ ...state . components . filter ( c => c . id !== currFocusedComponent . id ) , // all elements besides the one just changed
151
+ copyOfFocusedComponent ,
152
+ ] ;
153
+
154
+ // RETURN - update state...
155
+ return {
156
+ ...state ,
157
+ components : modifiedComponentArray ,
158
+ focusComponent : copyOfFocusedComponent , // problem! the delete works on State.components, but not on FOcus component.. starnge..
159
+ focusChild : { } , // reset to blank.
160
+ } ;
161
+ } ;
162
+
120
163
export const handleTransform = ( state , {
121
164
componentId, childId, x, y, width, height,
122
165
} ) => {
0 commit comments