1
1
import { createComponent } from '../utils/reducer.util' ;
2
- import { ComponentState } from '../types/types' ;
2
+ import { ComponentState , ChildState } from '../types/types' ;
3
3
import * as types from '../types/actionTypes' ;
4
4
import { loadState } from '../localStorage.js' ;
5
5
import createFiles from '../utils/createFiles.util' ;
@@ -35,18 +35,16 @@ export const changeImagePath = (imageSource: string) => ({
35
35
export const addComponent = ( title : string ) => {
36
36
return ( dispatch , getState ) => {
37
37
// ** grab state from our reducer to see how many components currently exist in our array
38
- const appComponentState = getState ( ) . application . components ;
39
- // ** Conditionally assigning a variable componentId. If any components exist in the array find the last component's id and increment it else initialize at 1.
40
- const componentId = appComponentState . length ? appComponentState [ appComponentState . length - 1 ] . id + 1 : 1 ;
41
- dispatch ( createComponent ( componentId , title , appComponentState ) )
38
+ const app = getState ( ) . application ;
39
+ const componentId = app . nextComponentId ;
40
+ const components = app . components ;
41
+ dispatch ( createComponent ( componentId , title , components ) )
42
42
. then ( ( component : ComponentState ) => {
43
- // ** the dispatch to createComponent will return a promise. The promise will return either the new createdComponent or just the generic state object (which will have an id of 0 by default)
44
- if ( component . id !== 0 ) {
45
- dispatch ( {
46
- type : types . ADD_COMPONENT ,
47
- payload : { component }
48
- } ) ;
49
- }
43
+ // ** the dispatch to createComponent will return a promise. The promise will return either the new createdComponent.
44
+ dispatch ( {
45
+ type : types . ADD_COMPONENT ,
46
+ payload : { component }
47
+ } ) ;
50
48
} )
51
49
. catch ( ( err : Error ) => console . log ( err ) ) ;
52
50
} ;
@@ -62,26 +60,47 @@ export const updateComponent = (id: number, update: {}) => ({
62
60
} ) ;
63
61
64
62
// ** deleteComponent deletes a component from our global state component array
65
- export const deleteComponent = ( id : number ) => ( {
66
- type : types . DELETE_COMPONENT ,
67
- payload : { id }
68
- } ) ;
69
-
70
- export const addChild = ( {
71
- title,
72
- childType,
73
- HTMLInfo,
74
- } : {
75
- title : string ;
76
- childType : string ;
77
- HTMLInfo : object ;
78
- } ) => ( dispatch : any ) => {
79
- dispatch ( { type : types . ADD_CHILD , payload : { title, childType, HTMLInfo } } ) ;
63
+ export const deleteComponent = ( id : number ) => {
64
+ return ( dispatch , getState ) => {
65
+ const appComponents = getState ( ) . application . components ;
66
+ appComponents . forEach ( ( parent : ComponentState ) => {
67
+ parent . children
68
+ . filter ( ( child : ChildState ) => child . childComponentId === id )
69
+ . forEach ( ( child : ChildState ) => {
70
+ dispatch ( {
71
+ type : types . DELETE_CHILD ,
72
+ payload : {
73
+ parentId : parent . id ,
74
+ childId : child . childId ,
75
+ // calledFromDeleteComponent: true,
76
+ } ,
77
+ } ) ;
78
+ } ) ;
79
+ } ) ;
80
+ dispatch ( {
81
+ type : types . DELETE_COMPONENT ,
82
+ payload : { id }
83
+ } ) ;
84
+ }
80
85
} ;
81
86
82
- export const deleteChild = ( { } ) => ( dispatch : any ) => {
83
- // with no payload, it will delete focusd child
84
- dispatch ( { type : types . DELETE_CHILD , payload : { } } ) ;
87
+ export const addChild = ( title : string , childType : string , HTMLInfo ?: { } ) => ( {
88
+ type : types . ADD_CHILD ,
89
+ payload : { title, childType, HTMLInfo }
90
+ } ) ;
91
+
92
+ export const deleteChild = ( id : number ) => {
93
+ return ( dispatch , getState ) => {
94
+ const parent = getState ( ) . application . focusComponent ;
95
+ const child = parent . children . find ( ( currentChild : ChildState ) => id === currentChild . childComponentId ) ;
96
+ dispatch ( {
97
+ type : types . DELETE_CHILD ,
98
+ payload : {
99
+ parentId : parent . id ,
100
+ childId : child . childId ,
101
+ } ,
102
+ } ) ;
103
+ }
85
104
} ;
86
105
87
106
export const changeFocusComponent = ( { title } : { title : string } ) => ( dispatch : any ) => {
0 commit comments