Skip to content

Commit 54d94f2

Browse files
authored
Merge pull request #87 from React-Proto/revert-76-feat/multiple_parents
Revert "Allow component to have multiple parents."
2 parents 886b460 + 8fa2205 commit 54d94f2

13 files changed

+127
-170
lines changed

src/actionTypes/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export const LOAD_INIT_DATA = 'LOAD_INIT_DATA';
22
export const ADD_COMPONENT = 'ADD_COMPONENT';
33
export const UPDATE_COMPONENT = 'UPDATE_COMPONENT';
44
export const DELETE_COMPONENT = 'DELETE_COMPONENT';
5-
export const UPDATE_CHILDREN = 'UPDATE_CHILDREN';
5+
export const ADD_NEW_CHILD = 'ADD_NEW_CHILD';
6+
export const DELETE_CHILD = 'DELETE_CHILD';
67
export const REASSIGN_PARENT = 'REASSIGN_PARENT';
78
export const SET_SELECTABLE_PARENTS = 'SET_SELECTABLE_PARENTS';
89
export const EXPORT_FILES = 'EXPORT_FILES';

src/actions/components.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
ADD_COMPONENT,
44
UPDATE_COMPONENT,
55
DELETE_COMPONENT,
6-
UPDATE_CHILDREN,
6+
ADD_NEW_CHILD,
7+
DELETE_CHILD,
78
REASSIGN_PARENT,
89
SET_SELECTABLE_PARENTS,
910
EXPORT_FILES,
@@ -39,21 +40,30 @@ export const loadInitData = () => (dispatch) => {
3940
}));
4041
};
4142

42-
export const updateChildren = (({
43-
parentIds, childIndex, childId,
43+
export const addNewChild = (({
44+
id, childIndex, childId,
4445
}) => ({
45-
type: UPDATE_CHILDREN,
46+
type: ADD_NEW_CHILD,
4647
payload: {
47-
parentIds, childIndex, childId,
48+
id, childIndex, childId,
4849
},
4950
}));
5051

51-
export const parentReassignment = (({ index, id, parentIds }) => ({
52+
export const deleteChild = (({
53+
parent, childIndex, childId,
54+
}) => ({
55+
type: DELETE_CHILD,
56+
payload: {
57+
parent, childIndex, childId,
58+
},
59+
}));
60+
61+
export const parentReassignment = (({ index, id, parent }) => ({
5262
type: REASSIGN_PARENT,
5363
payload: {
5464
index,
5565
id,
56-
parentIds,
66+
parent,
5767
},
5868
}));
5969

@@ -62,20 +72,19 @@ export const addComponent = ({ title }) => (dispatch) => {
6272
dispatch({ type: SET_SELECTABLE_PARENTS });
6373
};
6474

65-
export const deleteComponent = ({ index, id, parentIds = [] }) => (dispatch) => {
66-
if (parentIds.length) {
67-
// Delete Component from its parent if it has a parent.
68-
dispatch(updateChildren({ parentIds, childId: id, childIndex: index }));
75+
export const deleteComponent = ({ index, id, parent }) => (dispatch) => {
76+
// Delete Component from its parent if it has a parent.
77+
if (parent && parent.id) {
78+
dispatch(deleteChild({ parent, childId: id, childIndex: index }));
6979
}
7080
// Reassign Component's children to its parent if it has one or make them orphans
71-
dispatch(parentReassignment({ index, id, parentIds }));
72-
81+
dispatch(parentReassignment({ index, id, parent }));
7382
dispatch({ type: DELETE_COMPONENT, payload: { index, id } });
7483
dispatch({ type: SET_SELECTABLE_PARENTS });
7584
};
7685

7786
export const updateComponent = ({
78-
id, index, newParentId = null, color = null, stateful = null,
87+
id, index, parent = null, newParentId = null, color = null, stateful = null,
7988
}) => (dispatch) => {
8089
dispatch({
8190
type: UPDATE_COMPONENT,
@@ -84,8 +93,12 @@ export const updateComponent = ({
8493
},
8594
});
8695

87-
if (newParentId) {
88-
dispatch(updateChildren({ parentIds: [newParentId], childId: id, childIndex: index }));
96+
if (newParentId && newParentId !== 'null') {
97+
dispatch(addNewChild({ id: newParentId, childId: id, childIndex: index }));
98+
}
99+
100+
if (parent && parent.id) {
101+
dispatch(deleteChild({ parent, index, childId: id }));
89102
}
90103

91104
dispatch({ type: SET_SELECTABLE_PARENTS });

src/components/LeftColExpansionPanel.jsx

+28-60
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import ExpansionPanelDetails from '@material-ui/core/ExpansionPanelDetails';
77
import ExpansionPanelActions from '@material-ui/core/ExpansionPanelActions';
88
import Typography from '@material-ui/core/Typography';
99
import Input from '@material-ui/core/Input';
10-
import MenuItem from '@material-ui/core/MenuItem';
11-
import RemoveCircleOutlineIcon from '@material-ui/icons/RemoveCircleOutline';
12-
import ListItemText from '@material-ui/core/ListItemText';
1310
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
1411
import Switch from '@material-ui/core/Switch';
15-
import Chip from '@material-ui/core/Chip';
1612
import IconButton from '@material-ui/core/IconButton';
1713
import DeleteIcon from '@material-ui/icons/Delete';
1814
import FlipToBackIcon from '@material-ui/icons/FlipToBack';
@@ -32,13 +28,6 @@ const styles = theme => ({
3228
fontSize: theme.typography.pxToRem(15),
3329
fontWeight: theme.typography.fontWeightRegular,
3430
},
35-
chips: {
36-
display: 'flex',
37-
flexWrap: 'wrap',
38-
},
39-
chip: {
40-
margin: theme.spacing.unit / 4,
41-
},
4231
panel: {
4332
backgroundColor: '#333333',
4433
},
@@ -71,15 +60,6 @@ const styles = theme => ({
7160
group: {
7261
margin: `${theme.spacing.unit}px 0`,
7362
},
74-
icon: {
75-
fontSize: '20px',
76-
color: '#000',
77-
transition: 'all .2s ease',
78-
79-
'&:hover': {
80-
color: 'red',
81-
},
82-
},
8363
});
8464

8565
const LeftColExpansionPanel = (props) => {
@@ -99,24 +79,23 @@ const LeftColExpansionPanel = (props) => {
9979
id,
10080
stateful,
10181
color,
102-
parents,
103-
parentIds,
82+
parent,
10483
selectableParents,
10584
} = component;
10685

107-
const handleParentChange = (event, parentId = null) => {
108-
let newParentId = parentId;
109-
if (event) {
110-
const selectedParents = event.target.value;
111-
newParentId = selectedParents[selectedParents.length - 1].id;
112-
}
113-
114-
return updateComponent({
115-
index,
116-
id,
117-
newParentId,
118-
});
119-
};
86+
const parentOptions = [
87+
<option value='null' key=''>
88+
None
89+
</option>,
90+
...selectableParents.map(
91+
selectableParent => <option
92+
value={selectableParent.id}
93+
key={selectableParent.id}
94+
>
95+
{selectableParent.title}
96+
</option>,
97+
),
98+
];
12099

121100
return (
122101
<div className={classes.root}>
@@ -151,35 +130,24 @@ const LeftColExpansionPanel = (props) => {
151130
/>
152131
</div>
153132
<div className={classes.column}>
154-
<InputLabel className={classes.label} htmlFor='parentSelect'>selectedParents</InputLabel>
133+
<InputLabel className={classes.label} htmlFor='parentSelect'>Parent</InputLabel>
155134
<Select
156135
className={classes.light}
157-
multiple
158-
value={parents}
136+
native
137+
value={parent.id}
159138
id='parentSelect'
160139
name='parentName'
161-
disabled={selectableParents.length < 1}
162-
onChange={handleParentChange}
163-
input={<Input id='parentSelect' />}
164-
renderValue={selectedP => (
165-
<div className={classes.chips}>
166-
{selectedP.map(parent => (
167-
<Chip
168-
key={parent.id}
169-
label={parent.title}
170-
className={classes.chip}
171-
onDelete={() => handleParentChange(null, parent.id)}
172-
deleteIcon={<RemoveCircleOutlineIcon className={classes.icon} />}
173-
/>
174-
))}
175-
</div>
176-
)}
140+
onChange={(event) => {
141+
const newParentId = event.target.value;
142+
updateComponent({
143+
newParentId,
144+
index,
145+
id,
146+
parent,
147+
});
148+
}}
177149
>
178-
{selectableParents.map(parentObj => (
179-
<MenuItem key={parentObj.id} value={parentObj}>
180-
<ListItemText primary={parentObj.title} />
181-
</MenuItem>
182-
))}
150+
{parentOptions}
183151
</Select>
184152
</div>
185153
</ExpansionPanelDetails>
@@ -205,7 +173,7 @@ const LeftColExpansionPanel = (props) => {
205173
className={classes.button}
206174
onClick={() => {
207175
deleteComponent({
208-
index, id, parentIds,
176+
index, id, parent,
209177
});
210178
}}
211179
aria-label='Delete'>

src/components/SortableComponent.jsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import 'react-sortable-tree/style.css';
55

66
const SortableComponent = (props) => {
77
const rootComponents = props.components.filter(
8-
comp => comp.parentIds.length === 0,
8+
comp => comp.parentId.length === 0,
99
).reverse();
10-
1110
return (
1211
<div className="sortable-tree">
1312
<SortableTree
1413
style={{ backgroundColor: 'rgb(37, 37, 38)' }}
1514
treeData={rootComponents}
1615
canDrag={false}
17-
onChange={() => {}}
16+
onChange={treeData => this.setState({ treeData })}
1817
/>
1918
</div>
2019
);
@@ -23,5 +22,5 @@ const SortableComponent = (props) => {
2322
export default SortableComponent;
2423

2524
SortableComponent.propTypes = {
26-
components: PropTypes.array.isRequired,
25+
components: PropTypes.array,
2726
};

src/containers/AppContainer.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import LinearProgress from '@material-ui/core/LinearProgress';
66
import LeftContainer from './LeftContainer.jsx';
77
import MainContainer from './MainContainer.jsx';
88
import RightContainer from './RightContainer.jsx';
9-
import convertIdsToObjs from '../utils/convertIdsToObjs.util';
9+
import convertIdToObjs from '../utils/convertIdsToObjs.util';
1010
import theme from '../components/theme';
1111
import { loadInitData } from '../actions/components';
1212

@@ -51,7 +51,7 @@ class AppContainer extends Component {
5151
loading,
5252
} = this.props;
5353
const { width, rightColumnOpen } = this.state;
54-
const updatedComponents = convertIdsToObjs(components);
54+
const updatedComponents = convertIdToObjs(components);
5555

5656
return (
5757
<MuiThemeProvider theme={theme}>

src/containers/LeftContainer.jsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ const mapDispatchToProps = dispatch => ({
1616
addComponent: ({ title }) => dispatch(actions.addComponent({ title })),
1717
updateComponent:
1818
({
19-
id, index, newParentId = null, color = null, stateful = null,
19+
id, index, parent = null, newParentId = null, color = null, stateful = null,
2020
}) => dispatch(actions.updateComponent({
21-
id, index, newParentId, color, stateful,
21+
id, index, parent, newParentId, color, stateful,
2222
})),
2323
deleteComponent: ({
24-
index, id, parentIds,
25-
}) => dispatch(actions.deleteComponent({ index, id, parentIds })),
24+
index, id, parent,
25+
}) => dispatch(actions.deleteComponent({ index, id, parent })),
2626
moveToBottom: componentId => dispatch(actions.moveToBottom(componentId)),
2727
moveToTop: componentId => dispatch(actions.moveToTop(componentId)),
2828
openExpansionPanel: component => dispatch(actions.openExpansionPanel(component)),
2929
deleteAllData: () => dispatch(actions.deleteAllData()),
3030
});
3131

32-
const styles = () => ({
32+
const styles = theme => ({
3333
cssLabel: {
3434
color: 'white',
3535

src/containers/MainContainer.jsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,10 @@ class MainContainer extends Component {
221221
showGenerateAppModal,
222222
setImage,
223223
} = this;
224-
const cursor = this.state.draggable ? 'move' : 'default';
225224

226225
return (
227226
<MuiThemeProvider theme={theme}>
228-
<div
229-
className="main-container"
230-
style={{ cursor }}>
227+
<div className="main-container">
231228
<MainContainerHeader
232229
image={image}
233230
increaseHeight={increaseHeight}

src/containers/RightContainer.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ RightContainer.propTypes = {
7777
deleteProp: PropTypes.func.isRequired,
7878
addProp: PropTypes.func.isRequired,
7979
width: PropTypes.number.isRequired,
80-
rightColumnOpen: PropTypes.bool.isRequired,
8180
};
8281

8382

src/localStorage.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import localforage from 'localforage';
22

3-
export const saveState = state => localforage.setItem('state-v1.0.1', state);
4-
export const loadState = () => localforage.getItem('state-v1.0.1');
3+
export const saveState = state => localforage.setItem('state', state);
4+
5+
export const loadState = () => localforage.getItem('state');

src/reducers/componentReducer.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
ADD_COMPONENT,
44
UPDATE_COMPONENT,
55
DELETE_COMPONENT,
6-
UPDATE_CHILDREN,
6+
ADD_NEW_CHILD,
7+
DELETE_CHILD,
78
REASSIGN_PARENT,
89
SET_SELECTABLE_PARENTS,
910
EXPORT_FILES,
@@ -27,7 +28,8 @@ import {
2728
addComponent,
2829
updateComponent,
2930
deleteComponent,
30-
updateChildren,
31+
addChild,
32+
deleteChild,
3133
reassignParent,
3234
setSelectableP,
3335
exportFilesSuccess,
@@ -72,8 +74,10 @@ const componentReducer = (state = initialApplicationState, action) => {
7274
return updateComponent(state, action.payload);
7375
case DELETE_COMPONENT:
7476
return deleteComponent(state, action.payload);
75-
case UPDATE_CHILDREN:
76-
return updateChildren(state, action.payload);
77+
case ADD_NEW_CHILD:
78+
return addChild(state, action.payload);
79+
case DELETE_CHILD:
80+
return deleteChild(state, action.payload);
7781
case REASSIGN_PARENT:
7882
return reassignParent(state, action.payload);
7983
case SET_SELECTABLE_PARENTS:

0 commit comments

Comments
 (0)