Skip to content

Commit e9cfa8b

Browse files
merging with child sort panel
2 parents b7e4e56 + 29b23c4 commit e9cfa8b

File tree

5 files changed

+95
-57
lines changed

5 files changed

+95
-57
lines changed

src/actions/components.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,9 @@ export const loadInitData = () => dispatch => {
4040
dispatch({
4141
type: LOAD_INIT_DATA,
4242
payload: {
43-
<<<<<<< HEAD
44-
data: data ? data.workspace : {},
45-
},
46-
}),
47-
=======
4843
data: data ? data.workspace : {}
4944
}
5045
})
51-
>>>>>>> fcd32cb... finished ts changes to util files
5246
);
5347
};
5448

@@ -83,14 +77,10 @@ export const deleteChild = ({}) => dispatch => {
8377
dispatch({ type: DELETE_CHILD, payload: {} });
8478
};
8579

86-
<<<<<<< HEAD
87-
export const deleteComponent = ({ componentId, stateComponents }) => dispatch => {
88-
=======
8980
export const deleteComponent = ({
9081
componentId,
9182
stateComponents
9283
}) => dispatch => {
93-
>>>>>>> fcd32cb... finished ts changes to util files
9484
// find all places where the "to be delted" is a child and do what u gotta do
9585
stateComponents.forEach(parent => {
9686
parent.childrenArray
@@ -325,9 +315,16 @@ export const updateHtmlAttr = ({ attr, value }) => dispatch => {
325315
});
326316
};
327317

318+
<<<<<<< HEAD
328319
export const updateChildrenSort = ({ newChildrenArray }) => dispatch => {
329320
dispatch({
330321
type: UPDATE_CHILDREN_SORT,
331322
payload: { newChildrenArray }
323+
=======
324+
export const updateChildrenSort = ({ newSortValues }) => (dispatch) => {
325+
dispatch({
326+
type: UPDATE_CHILDREN_SORT,
327+
payload: { newSortValues },
328+
>>>>>>> 29b23c438a735dda0a220e39c9795d2ca252eb6d
332329
});
333330
};

src/components/SortChildren.jsx

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,88 @@
11

2+
"use strict" ;
3+
24
import React, { Component} from "react";
35
import { connect } from "react-redux";
46
import { updateChildrenSort } from "../actions/components";
7+
import { width } from "window-size";
8+
import cloneDeep from "../utils/cloneDeep";
59

610
const mapStateToProps = store => ({
711
focusComponent: store.workspace.focusComponent,
812
});
913

1014
const mapDispatchToProps = dispatch => ({
11-
updateChildrenSort: ({ newChildrenArray }) => dispatch(updateChildrenSort({ newChildrenArray }))
15+
updateChildrenSort: ({ newSortValues }) => dispatch(updateChildrenSort({ newSortValues }))
1216
});
1317

1418
class SortChildren extends Component {
1519
constructor(props) {
1620
super(props);
1721

1822
this.state = {
19-
localChildrenArray: this.setLocalArray()
20-
//localChildrenArray: this.props.focusComponent.childrenArray,
21-
//shortArray: this.setLocalArray()
22-
};
23-
24-
}
25-
23+
draggedIndex: null,
24+
draggedOverIndex: null
25+
};
26+
} // end constrcutor
27+
2628
setLocalArray = () =>{
2729
const localArray = this.props.focusComponent.childrenArray.map((child,idx) => {
28-
return {Childid: child.childId, childSort: child.childSort}
30+
return {childId: child.childId, childSort: child.childSort}
2931
} )
3032
return localArray ;
3133
}
3234

35+
3336
onDragStart = (e,idx) => {
34-
console.log('dragging index: ',idx)
35-
this.draggedIndex = idx;
37+
38+
this.setState({draggedIndex:idx})
39+
3640
e.dataTransfer.effectAllowed = "move";
3741
e.dataTransfer.setData("text/html", e.target.parentNode);
3842
e.dataTransfer.setDragImage(e.target.parentNode, 20, 20);
3943
};
4044

4145

42-
onDragOver = (draggedOverIndex) => {
43-
//const draggedOverIndex = idx;
46+
onDragOver = (idx) => {
47+
48+
this.setState({draggedOverIndex:idx})
49+
// console.log(`onDragOver idx=${idx} this.state.draggedOverIndex:${this.state.draggedOverIndex}`)
50+
};
4451

45-
// if the item is dragged over itself, ignore
46-
if (this.draggedIndex === draggedOverIndex) {
52+
onDragEnd(e) {
53+
console.log(`dragEnd this
54+
state.draggedIndex: ${this.state.draggedIndex}
55+
this.state.draggedOverIndex: ${this.state.draggedOverIndex}`
56+
)
57+
//const {draggedIndex, draggedOverIndex } = this.state;
58+
if (this.state.draggedIndex === this.state.draggedOverIndex
59+
// || !this.state.draggedIndex || this.state.draggedOverIndex
60+
) {
4761
return;
4862
}
4963

50-
//console.log(`OnDrageOver: draggedOverIndex: ${draggedOverIndex} this.draggedIndex:${this.draggedIndex}`)
51-
// filter out the currently dragged item
52-
let draggedChild = this.state.localChildrenArray[this.draggedIndex] ;
53-
//console.log(`dragged item:`,draggedChild)
64+
let currentSortValues = this.setLocalArray();
65+
// console.log(`currentSortValues`,JSON.stringify((currentSortValues)))
5466

55-
let localChildrenArray = [...this.state.localChildrenArray]
56-
//console.log('localChildrenArray BEFORE removing ',JSON.stringify(localChildrenArray))
67+
// remove the dragged Item and save it, we will use add it back in a moment.
68+
const draggedBaby = currentSortValues[this.state.draggedIndex] ;
69+
currentSortValues.splice(this.state.draggedIndex,1) ;
70+
71+
// put back the dragge item after the dragged Over
72+
currentSortValues.splice(this.state.draggedOverIndex, 0, draggedBaby)
73+
//console.log(`currentSortValues after reAdding the dragged baby `,JSON.stringify(currentSortValues))
5774

75+
currentSortValues = currentSortValues.map((child,idx) => {
76+
return {childId:child.childId, childSort: idx+1}
77+
})
5878

59-
localChildrenArray.splice(this.draggedIndex,1)
60-
//console.log('localChildrenArray after removing ',JSON.stringify(localChildrenArray))
79+
console.log(`currentSortValues after updating the sort `,JSON.stringify(currentSortValues))
6180

62-
//add the dragged item after the dragged over item
63-
localChildrenArray.splice(draggedOverIndex, 0, draggedChild);
64-
//console.log('localChildrenArray final: ',JSON.stringify(localChildrenArray))
65-
this.setState({localChildrenArray });
81+
this.props.updateChildrenSort({newSortValues: currentSortValues});
6682

67-
this.props.updateChildrenSort({newChildrenArray: localChildrenArray});
68-
69-
};
83+
this.setState({draggedIndex: 0, draggedOverIndex: 0 })
84+
85+
}
7086

7187
render() {
7288
const ulStyle = {
@@ -84,46 +100,66 @@ render() {
84100
lineHeight: 1,
85101
cursor: "move",
86102
}
87-
const children = this.props.focusComponent.childrenArray;
88-
const List = children
103+
//const children = this.props.focusComponent.childrenArray;
104+
// const List = children
105+
const List = cloneDeep(this.props.focusComponent.childrenArray)
89106
.sort( (a,b) => a.childSort - b.childSort )
90107
.map( (child,idx) => {
91-
//console.log(`mappping...... ${idx} ${child.componentName + child.childId} childSort ${child.childSort}`)
92108
return (
93109
<li
94110
style={liStyle}
95-
id={child.Childid}
111+
id={child.childId}
96112
key={idx}
97113
>
98114
<div className="drag" draggable
99115
onDragStart={e => this.onDragStart(e,idx) }
100116
onDragOver ={e => this.onDragOver(idx)}
117+
onDragEnd ={e => this.onDragEnd() }
101118
>
102119
{child.componentName + child.childId}
103120
</div>
104121

105122
</li>
106123
)})
107-
console.log('children')
108-
console.log(children)
109-
console.log('List')
110-
console.log(List)
111124
return (
112-
<div style={{
125+
<div
126+
style={{
127+
minWidth: '200px',
113128
position: 'relative',
114-
float: 'right',
129+
float: 'left',
115130
marginTop: '20px',
116131
marginRIght: '20px',
117-
}} >
132+
}}
133+
>
118134
<h3>Childrens List</h3>
119135
<ul style={ulStyle}>
120-
{List}
136+
137+
{cloneDeep(this.props.focusComponent.childrenArray)
138+
.sort( (a,b) => a.childSort - b.childSort )
139+
.map( (child,idx) => {
140+
return (
141+
<li
142+
style={liStyle}
143+
id={child.childId}
144+
key={idx}
145+
>
146+
<div className="drag" draggable
147+
onDragStart={e => this.onDragStart(e,idx) }
148+
onDragOver ={e => this.onDragOver(idx)}
149+
onDragEnd ={e => this.onDragEnd() }
150+
>
151+
{child.componentName + child.childId}
152+
</div>
153+
154+
</li>
155+
)})}
156+
157+
158+
{/* {List} */}
121159
</ul>
122160
</div>
123-
)
124-
125-
}
126-
161+
)
162+
}
127163
}
128164

129165
export default connect(

src/utils/componentReducer.util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,5 +548,7 @@ export const updateChildrenSort = (
548548
// console.log('modifiedCHildArrrrrr',modifiedChldrenArray)
549549
return {
550550
...state,
551+
components: modifiedComponentsArray,
552+
focusComponent: modifiedComponent,
551553
};
552554
};

src/utils/componentRender.util.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import cloneDeep from './cloneDeep';
2+
13
const componentRender = (component, data) => {
24
const { stateful, id, position, childrenArray, title, props } = component;
35

@@ -122,7 +124,8 @@ const componentRender = (component, data) => {
122124
123125
return (
124126
<div>
125-
${childrenArray
127+
${cloneDeep(childrenArray)
128+
.sort((a, b) => a.childSort - b.childSort)
126129
.map(child => `<${componentNameGenerator(child)} ${propDrillTextGenerator(child)}/>`)
127130
.join('\n')}
128131
</div>

0 commit comments

Comments
 (0)