1- import React , { useRef } from 'react' ;
2- import { ChildElement , HTMLType } from '../../interfaces/Interfaces' ;
1+ import React , { useRef } from 'react' ;
2+ import { ChildElement , HTMLType , DragItem } from '../../interfaces/Interfaces' ;
33import { useDrag , useDrop , DropTargetMonitor } from 'react-dnd' ;
44import { ItemTypes } from '../../constants/ItemTypes' ;
55import { combineStyles } from '../../helperFunctions/combineStyles' ;
66import globalDefaultStyle from '../../public/styles/globalDefaultStyles' ;
77import renderChildren from '../../helperFunctions/renderChildren' ;
8- import validateNewParent from '../../helperFunctions/changePositionValidation'
9- import componentNest from '../../helperFunctions/componentNestValidation'
8+ import validateNewParent from '../../helperFunctions/changePositionValidation' ;
9+ import componentNest from '../../helperFunctions/componentNestValidation' ;
1010import { useDispatch , useSelector } from 'react-redux' ;
1111import { RootState } from '../../redux/store' ;
12- import { changeFocus , changePosition , addChild } from '../../redux/reducers/slice/appStateSlice' ;
13-
14-
12+ import {
13+ changeFocus ,
14+ changePosition ,
15+ addChild
16+ } from '../../redux/reducers/slice/appStateSlice' ;
1517
1618function DirectChildHTMLNestable ( {
1719 childId,
1820 type,
1921 typeId,
2022 style,
2123 children
22- } : ChildElement ) {
23- const { state, contextParam } = useSelector ( ( store :RootState ) => ( {
24+ } : ChildElement ) : JSX . Element {
25+ const { state, contextParam } = useSelector ( ( store : RootState ) => ( {
2426 state : store . appState ,
25- contextParam : store . contextSlice ,
27+ contextParam : store . contextSlice
2628 } ) ) ;
2729 const dispatch = useDispatch ( ) ;
2830 const ref = useRef ( null ) ;
@@ -55,28 +57,43 @@ function DirectChildHTMLNestable({
5557 const [ { isOver } , drop ] = useDrop ( {
5658 accept : ItemTypes . INSTANCE ,
5759 // triggered on drop
58- drop : ( item : any , monitor : DropTargetMonitor ) => {
60+ drop : ( item : DragItem , monitor : DropTargetMonitor ) => {
5961 const didDrop = monitor . didDrop ( ) ;
6062 if ( didDrop ) {
6163 return ;
6264 }
6365 // updates state with new instance
6466 // if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
6567 if ( item . newInstance ) {
66- if ( ( item . instanceType === 'Component' && componentNest ( state . components [ item . instanceTypeId - 1 ] . children , childId ) ) || item . instanceType !== 'Component' ) {
67- dispatch ( addChild ( {
68- type : item . instanceType ,
69- typeId : item . instanceTypeId ,
70- childId : childId ,
71- contextParam : contextParam
72- } ) )
68+ if (
69+ ( item . instanceType === 'Component' &&
70+ componentNest (
71+ state . components [ item . instanceTypeId - 1 ] . children ,
72+ childId
73+ ) ) ||
74+ item . instanceType !== 'Component'
75+ ) {
76+ dispatch (
77+ addChild ( {
78+ type : item . instanceType ,
79+ typeId : item . instanceTypeId ,
80+ childId : childId ,
81+ contextParam : contextParam
82+ } )
83+ ) ;
7384 }
7485 }
7586 // if item is not a new instance, change position of element dragged inside separator so that separator is new parent (until replacement)
7687 else {
7788 // check to see if the selected child is trying to nest within itself
7889 if ( validateNewParent ( state , item . childId , childId ) === true ) {
79- dispatch ( changePosition ( { currentChildId : item . childId , newParentChildId : childId , contextParam : contextParam } ) )
90+ dispatch (
91+ changePosition ( {
92+ currentChildId : item . childId ,
93+ newParentChildId : childId ,
94+ contextParam : contextParam
95+ } )
96+ ) ;
8097 }
8198 }
8299 } ,
@@ -89,8 +106,7 @@ function DirectChildHTMLNestable({
89106 } ) ;
90107
91108 const changeFocusFunction = ( componentId : number , childId : number | null ) => {
92- dispatch ( changeFocus ( { componentId, childId} ) ) ;
93-
109+ dispatch ( changeFocus ( { componentId, childId } ) ) ;
94110 } ;
95111
96112 // onClickHandler is responsible for changing the focused component and child component
@@ -104,10 +120,12 @@ function DirectChildHTMLNestable({
104120 const defaultNestableStyle = { ...globalDefaultStyle } ;
105121 const separatorStyle = {
106122 padding : '2px 10px' ,
107- margin : '1px 10px' ,
123+ margin : '1px 10px'
108124 } ;
109125
110- defaultNestableStyle [ 'backgroundColor' ] = isOver ? '#cee2f5' : 'rgba(0, 0, 255, 0.0)' ;
126+ defaultNestableStyle [ 'backgroundColor' ] = isOver
127+ ? '#cee2f5'
128+ : 'rgba(0, 0, 255, 0.0)' ;
111129
112130 const combinedStyle = combineStyles (
113131 combineStyles ( combineStyles ( defaultNestableStyle , HTMLType . style ) , style ) ,
0 commit comments