Skip to content

Commit d472e93

Browse files
Merge pull request #87 from ShlomoPorges/development
added sort to state & type , and sorChildren component
2 parents 574060f + de7ab88 commit d472e93

File tree

7 files changed

+132
-16
lines changed

7 files changed

+132
-16
lines changed

src/actionTypes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export const ADD_PROP = 'ADD_PROP';
2626
export const DELETE_ALL_DATA = 'DELETE_ALL_DATA';
2727
export const CHANGE_IMAGE_PATH = 'CHANGE_IMAGE_PATH';
2828
export const UPDATE_HTML_ATTR = 'UPDATE_HTML_ATTR';
29+
export const UPDATE_CHILDREN_SORT = 'UPDATE_CHILDREN_SORT'

src/actions/components.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
DELETE_ALL_DATA,
2828
CHANGE_IMAGE_PATH,
2929
UPDATE_HTML_ATTR,
30+
UPDATE_CHILDREN_SORT,
3031
} from '../actionTypes/index';
3132

3233
import { loadState } from '../localStorage';
@@ -259,3 +260,10 @@ export const updateHtmlAttr = ({ attr, value }) => dispatch => {
259260
payload: { attr, value },
260261
});
261262
};
263+
264+
export const updateChildrenSort = ({ newChildrenArray }) => (dispatch) => {
265+
dispatch({
266+
type: UPDATE_CHILDREN_SORT,
267+
payload: { newChildrenArray },
268+
});
269+
};

src/components/CodePreview.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class CodePreview extends Component<Props> {
1616
const components : ComponentsInt = this.props.components;
1717

1818
return (
19+
<Fragment>
20+
<SortChildren/>
1921
<div
2022
style={{
2123
width: '500px',
@@ -37,6 +39,7 @@ class CodePreview extends Component<Props> {
3739
})}
3840
</pre>
3941
</div>
42+
</Fragment>
4043
);
4144
}
4245
}

src/components/SortChildren.jsx

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,124 @@
11

22
import React, { Component} from "react";
33
import { connect } from "react-redux";
4+
import { updateChildrenSort } from "../actions/components";
45

56
const mapStateToProps = store => ({
67
focusComponent: store.workspace.focusComponent,
78
});
89

10+
const mapDispatchToProps = dispatch => ({
11+
updateChildrenSort: ({ newChildrenArray }) => dispatch(updateChildrenSort({ newChildrenArray }))
12+
});
913

1014
class SortChildren extends Component {
11-
// constructor(props) {
12-
// super(props);
13-
state = {test: 'myTestValue'};
14-
//}
15+
constructor(props) {
16+
super(props);
17+
18+
this.state = {
19+
localChildrenArray: this.setLocalArray()
20+
//localChildrenArray: this.props.focusComponent.childrenArray,
21+
//shortArray: this.setLocalArray()
22+
};
23+
24+
}
1525

26+
setLocalArray = () =>{
27+
const localArray = this.props.focusComponent.childrenArray.map((child,idx) => {
28+
return {Childid: child.childId, childSort: child.childSort}
29+
} )
30+
return localArray ;
31+
}
32+
33+
onDragStart = (e,idx) => {
34+
console.log('dragging index: ',idx)
35+
this.draggedIndex = idx;
36+
e.dataTransfer.effectAllowed = "move";
37+
e.dataTransfer.setData("text/html", e.target.parentNode);
38+
e.dataTransfer.setDragImage(e.target.parentNode, 20, 20);
39+
};
1640

41+
42+
onDragOver = (draggedOverIndex) => {
43+
//const draggedOverIndex = idx;
44+
45+
// if the item is dragged over itself, ignore
46+
if (this.draggedIndex === draggedOverIndex) {
47+
return;
48+
}
49+
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)
54+
55+
let localChildrenArray = [...this.state.localChildrenArray]
56+
//console.log('localChildrenArray BEFORE removing ',JSON.stringify(localChildrenArray))
57+
58+
59+
localChildrenArray.splice(this.draggedIndex,1)
60+
//console.log('localChildrenArray after removing ',JSON.stringify(localChildrenArray))
61+
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 });
66+
67+
this.props.updateChildrenSort({newChildrenArray: localChildrenArray});
68+
69+
};
70+
1771
render() {
72+
const ulStyle = {
73+
margin: 0,
74+
padding: 0,
75+
listStyle: "none",
76+
}
77+
78+
const liStyle = {
79+
backgroundColor: "#383838",
80+
padding: "10px 20px",
81+
position: "relative",
82+
display: "flex",
83+
alignItems: "flex-start",
84+
lineHeight: 1,
85+
cursor: "move",
86+
}
1887
const children = this.props.focusComponent.childrenArray;
19-
const List = children.map( (child,idx) => {
20-
console.log(`mappping...... ${idx} ${child.componentName + child.childId}`)
88+
const List = children
89+
.sort( (a,b) => a.childSort - b.childSort )
90+
.map( (child,idx) => {
91+
//console.log(`mappping...... ${idx} ${child.componentName + child.childId} childSort ${child.childSort}`)
2192
return (
2293
<li
23-
Childid={child.Childid}
94+
style={liStyle}
95+
id={child.Childid}
2496
key={idx}
25-
draggable='true'
26-
// onDragEnd={this.dragEnd.bind(this)}
27-
// onDragStart={this.dragStart.bind(this)}>{item}
2897
>
29-
{child.componentName + child.childId}
98+
<div className="drag" draggable
99+
onDragStart={e => this.onDragStart(e,idx) }
100+
onDragOver ={e => this.onDragOver(idx)}
101+
>
102+
{child.componentName + child.childId}
103+
</div>
104+
30105
</li>
31106
)})
32107
console.log('children')
33108
console.log(children)
34109
console.log('List')
35110
console.log(List)
36111
return (
37-
<div>
38-
<p>Childrens List</p>
39-
{/* <List/> */}
112+
<div style={{
113+
position: 'relative',
114+
float: 'right',
115+
marginTop: '20px',
116+
marginRIght: '20px',
117+
}} >
118+
<h3>Childrens List</h3>
119+
<ul style={ulStyle}>
120+
{List}
121+
</ul>
40122
</div>
41123
)
42124

@@ -45,5 +127,6 @@ render() {
45127
}
46128

47129
export default connect(
48-
mapStateToProps
130+
mapStateToProps,
131+
mapDispatchToProps
49132
)(SortChildren);

src/reducers/componentReducer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ADD_PROP,
2222
DELETE_PROP,
2323
UPDATE_HTML_ATTR,
24+
UPDATE_CHILDREN_SORT,
2425
} from '../actionTypes';
2526

2627
import {
@@ -41,6 +42,7 @@ import {
4142
addProp,
4243
deleteProp,
4344
updateHtmlAttr,
45+
updateChildrenSort,
4446
} from '../utils/componentReducer.util.ts';
4547
import cloneDeep from '../utils/cloneDeep.ts';
4648

@@ -147,6 +149,8 @@ const componentReducer = (state = initialApplicationState, action) => {
147149
return deleteProp(state, action.payload);
148150
case UPDATE_HTML_ATTR:
149151
return updateHtmlAttr(state, action.payload);
152+
case UPDATE_CHILDREN_SORT:
153+
return updateChildrenSort(state, action.payload);
150154
default:
151155
return state;
152156
}

src/utils/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface Position {
1515

1616
export interface ChildInt {
1717
childId: number;
18+
childSort: number;
1819
childType: string;
1920
childComponentId: number;
2021
componentName: string;
@@ -31,7 +32,6 @@ export interface ComponentInt {
3132
stateful: boolean;
3233
title: string;
3334
color: string;
34-
// draggable: boolean;
3535
props: PropInt[];
3636
nextPropId: number;
3737
position: Position;

src/utils/componentReducer.util.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export const addChild = (
157157

158158
const newChild: ChildInt = {
159159
childId: view.nextChildId,
160+
childSort: view.nextChildId,
160161
childType,
161162
childComponentId: childType === "COMP" ? parentComponent.id : null, // only relevant fot children of type COMPONENT
162163
componentName: strippedTitle,
@@ -610,3 +611,19 @@ export const updateHtmlAttr = (state: ApplicationStateInt, { attr, value }: {att
610611
focusChild: modifiedChild
611612
};
612613
};
614+
615+
616+
export const updateChildrenSort = (state: ApplicationStateInt, { newChildrenArray }: {newChildrenArray: ChildrenInt }) => {
617+
console.log('hello from updateChildrenSort. newChildrenArray: ',newChildrenArray )
618+
// the new Array has the same data exactly. the array index is the new sort...
619+
// const modifiedChldrenArray = cloneDeep(state.focusComponent.childrenArray)
620+
// modifiedChldrenArray.forEach( (child: ChildInt, idx:number, arr:any ) => {
621+
// console.log(`chidl id:${child.childId} currSort:${child.childSort}`)
622+
// const newSort = newChildrenArray.findIndex( (newChild:ChildInt) => newChild.childId === child.childId) + 1 ;
623+
// console.log(`new sort: ${newSort}`)
624+
// })
625+
//console.log('modifiedCHildArrrrrr',modifiedChldrenArray)
626+
return {
627+
...state,
628+
};
629+
};

0 commit comments

Comments
 (0)