Skip to content

Commit af227d8

Browse files
merged with Shlomo delete functionality
2 parents d2446e1 + 496b82e commit af227d8

File tree

5 files changed

+196
-161
lines changed

5 files changed

+196
-161
lines changed

src/actions/components.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,29 @@ export const addChild = ({ title }) => (dispatch) => {
6868
dispatch({ type: ADD_CHILD, payload: { title } });
6969
};
7070

71-
export const deleteChild = ({ title }) => (dispatch) => {
72-
dispatch({ type: DELETE_CHILD, payload: { title } });
71+
export const deleteChild = ({}) => (dispatch) => {
72+
// with no payload, it will delete focusd child
73+
dispatch({ type: DELETE_CHILD, payload: {} });
7374
};
7475

75-
export const deleteComponent = ({ index, id, parentIds = [] }) => (dispatch) => {
76-
if (parentIds.length) {
77-
// Delete Component from its parent if it has a parent.
78-
dispatch(updateChildren({ parentIds, childId: id, childIndex: index }));
79-
}
80-
// Reassign Component's children to its parent if it has one or make them orphans
81-
dispatch(parentReassignment({ index, id, parentIds }));
76+
export const deleteComponent = ({ componentId, stateComponents }) => (dispatch) => {
77+
console.log('Hello from component.js delete component.componentId= ', componentId);
78+
79+
// find all places where the "to be delted" is a child and do what u gotta do
80+
stateComponents.forEach((parent) => {
81+
parent.childrenArray.filter(child => child.childComponentId == componentId).forEach((child) => {
82+
// console.log(`Should delete ${child.childId} from component id:${parent.id} ${parent.title}`)
83+
dispatch({
84+
type: DELETE_CHILD,
85+
payload: { parentId: parent.id, childId: child.childId, calledFromDeleteComponent: true },
86+
});
87+
});
88+
});
8289

83-
dispatch({ type: DELETE_COMPONENT, payload: { index, id } });
84-
dispatch({ type: SET_SELECTABLE_PARENTS });
90+
// change focus to APp
91+
dispatch({ type: CHANGE_FOCUS_COMPONENT, payload: { title: 'App' } });
92+
// after taking care of the children delete the component
93+
dispatch({ type: DELETE_COMPONENT, payload: { componentId } });
8594
};
8695

8796
export const updateComponent = ({

src/containers/LeftContainer.jsx

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,55 @@
1-
import React, { Component } from "react";
2-
import { connect } from "react-redux";
3-
import { compose } from "redux";
1+
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
import { compose } from 'redux';
44
// import PropTypes from 'prop-types';
5-
import FormControl from "@material-ui/core/FormControl";
6-
import TextField from "@material-ui/core/TextField";
7-
import Button from "@material-ui/core/Button";
8-
import AddIcon from "@material-ui/icons/Add";
9-
import Grid from "@material-ui/core/Grid";
10-
import { withStyles } from "@material-ui/core/styles";
11-
import LeftColExpansionPanel from "../components/LeftColExpansionPanel.jsx";
5+
import FormControl from '@material-ui/core/FormControl';
6+
import TextField from '@material-ui/core/TextField';
7+
import Button from '@material-ui/core/Button';
8+
import AddIcon from '@material-ui/icons/Add';
9+
import Grid from '@material-ui/core/Grid';
10+
import { withStyles } from '@material-ui/core/styles';
11+
import LeftColExpansionPanel from '../components/LeftColExpansionPanel.jsx';
1212
// import createModal from '../utils/createModal.util';
13-
import * as actions from "../actions/components";
13+
import * as actions from '../actions/components';
1414

1515
const mapDispatchToProps = dispatch => ({
1616
addComponent: ({ title }) => dispatch(actions.addComponent({ title })),
1717
updateComponent: ({
18-
id,
19-
index,
20-
newParentId = null,
21-
color = null,
22-
stateful = null
23-
}) =>
24-
dispatch(
25-
actions.updateComponent({
26-
id,
27-
index,
28-
newParentId,
29-
color,
30-
stateful
31-
})
32-
),
18+
id, index, newParentId = null, color = null, stateful = null,
19+
}) => dispatch(
20+
actions.updateComponent({
21+
id,
22+
index,
23+
newParentId,
24+
color,
25+
stateful,
26+
}),
27+
),
3328
// deleteComponent: ({ index, id, parentIds }) => dispatch(actions.deleteComponent({ index, id, parentIds })),
3429
// moveToBottom: componentId => dispatch(actions.moveToBottom(componentId)),
3530
// moveToTop: componentId => dispatch(actions.moveToTop(componentId)),
3631
// openExpansionPanel: component => dispatch(actions.openExpansionPanel(component)),
3732
// deleteAllData: () => dispatch(actions.deleteAllData()),
3833
addChild: ({ title }) => dispatch(actions.addChild({ title })),
39-
changeFocusComponent: ({ title }) =>
40-
dispatch(actions.changeFocusComponent({ title })),
41-
changeFocusChild: ({ title, childId }) =>
42-
dispatch(actions.changeFocusChild({ title, childId }))
34+
changeFocusComponent: ({ title }) => dispatch(actions.changeFocusComponent({ title })),
35+
changeFocusChild: ({ title, childId }) => dispatch(actions.changeFocusChild({ title, childId })),
4336
});
4437

4538
class LeftContainer extends Component {
4639
state = {
47-
componentName: ""
40+
componentName: '',
4841
};
4942

50-
handleChange = event => {
43+
handleChange = (event) => {
5144
this.setState({
52-
[event.target.name]: event.target.value
45+
[event.target.name]: event.target.value,
5346
});
5447
};
5548

5649
handleAddComponent = () => {
5750
this.props.addComponent({ title: this.state.componentName });
5851
this.setState({
59-
componentName: ""
52+
componentName: '',
6053
});
6154
};
6255

@@ -71,11 +64,11 @@ class LeftContainer extends Component {
7164
addChild,
7265
changeFocusComponent,
7366
changeFocusChild,
74-
selectableChildren
67+
selectableChildren,
7568
} = this.props;
7669
const { componentName } = this.state;
7770

78-
console.log(components);
71+
// console.log(components);
7972

8073
const componentsExpansionPanel = components
8174
.sort((a, b) => parseInt(b.id) - parseInt(a.id)) // sort by id value of comp
@@ -105,8 +98,8 @@ class LeftContainer extends Component {
10598
margin="normal"
10699
autoFocus
107100
onChange={this.handleChange}
108-
onKeyPress={ev => {
109-
if (ev.key === "Enter") {
101+
onKeyPress={(ev) => {
102+
if (ev.key === 'Enter') {
110103
// Do code here
111104
this.handleAddComponent();
112105
ev.preventDefault();
@@ -116,10 +109,10 @@ class LeftContainer extends Component {
116109
name="componentName"
117110
className={classes.light}
118111
InputProps={{
119-
className: classes.input
112+
className: classes.input,
120113
}}
121114
InputLabelProps={{
122-
className: classes.input
115+
className: classes.input,
123116
}}
124117
/>
125118
</Grid>
@@ -147,8 +140,8 @@ export default compose(
147140
withStyles(styles),
148141
connect(
149142
null,
150-
mapDispatchToProps
151-
)
143+
mapDispatchToProps,
144+
),
152145
)(LeftContainer);
153146

154147
// LeftContainer.propTypes = {
@@ -168,40 +161,40 @@ export default compose(
168161
function styles() {
169162
return {
170163
cssLabel: {
171-
color: "white",
164+
color: 'white',
172165

173-
"&$cssFocused": {
174-
color: "green"
175-
}
166+
'&$cssFocused': {
167+
color: 'green',
168+
},
176169
},
177170
cssFocused: {},
178171
input: {
179-
color: "#fff",
180-
opacity: "0.7",
181-
marginBottom: "10px"
172+
color: '#fff',
173+
opacity: '0.7',
174+
marginBottom: '10px',
182175
},
183176
underline: {
184-
color: "white",
185-
"&::before": {
186-
color: "white"
187-
}
177+
color: 'white',
178+
'&::before': {
179+
color: 'white',
180+
},
188181
},
189182
button: {
190-
color: "#fff",
183+
color: '#fff',
191184

192-
"&:disabled": {
193-
color: "grey"
194-
}
185+
'&:disabled': {
186+
color: 'grey',
187+
},
195188
},
196189
clearButton: {
197-
top: "96%",
198-
position: "sticky!important",
199-
zIndex: "1",
190+
top: '96%',
191+
position: 'sticky!important',
192+
zIndex: '1',
200193

201-
"&:disabled": {
202-
color: "grey",
203-
backgroundColor: "#424242"
204-
}
205-
}
194+
'&:disabled': {
195+
color: 'grey',
196+
backgroundColor: '#424242',
197+
},
198+
},
206199
};
207200
}

src/containers/MainContainer.jsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
handleTransform,
1414
changeFocusChild,
1515
deleteChild,
16+
deleteComponent,
1617
} from '../actions/components';
1718
import KonvaStage from '../components/KonvaStage.jsx';
1819
// import MainContainerHeader from '../components/MainContainerHeader.jsx';
@@ -35,13 +36,15 @@ const mapDispatchToProps = dispatch => ({
3536
toggleComponentDragging: status => dispatch(toggleDragging(status)),
3637
openPanel: component => dispatch(openExpansionPanel(component)),
3738
changeFocusChild: ({ title, childId }) => dispatch(changeFocusChild({ title, childId })),
38-
deleteChild: ({ childId }) => dispatch(deleteChild({ childId })),
39+
deleteChild: ({}) => dispatch(deleteChild({})), // if u send no prms, function will delete focus child.
40+
deleteComponent: ({ componentId, stateComponents }) => dispatch(deleteComponent({ componentId, stateComponents })),
3941
});
4042

4143
const mapStateToProps = store => ({
4244
totalComponents: store.workspace.totalComponents,
4345
focusComponent: store.workspace.focusComponent,
4446
focusChild: store.workspace.focusChild,
47+
stateComponents: store.workspace.components,
4548
});
4649

4750
class MainContainer extends Component {
@@ -83,6 +86,8 @@ class MainContainer extends Component {
8386
focusChild,
8487
changeFocusChild,
8588
deleteChild,
89+
deleteComponent,
90+
stateComponents,
8691
} = this.props;
8792
const {
8893
increaseHeight,
@@ -96,6 +101,14 @@ class MainContainer extends Component {
96101
} = this;
97102
const cursor = this.state.draggable ? 'move' : 'default';
98103

104+
// show a string of all direct parents. SO the user can gaze at it.
105+
const directParents = !focusComponent.id
106+
? 'Waiting for a focused component'
107+
: stateComponents
108+
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId == focusComponent.id))
109+
.map(comp => comp.title)
110+
.join(',');
111+
99112
return (
100113
<MuiThemeProvider theme={theme}>
101114
<div className="main-container" style={{ cursor }}>
@@ -114,7 +127,14 @@ class MainContainer extends Component {
114127
deleteChild={deleteChild}
115128
/>
116129
</div>
117-
<button onClick={deleteChild}>`delete focused child`</button>
130+
131+
<p>{directParents ? `Used in: ${directParents}` : 'Not used in any other component'}</p>
132+
<button onClick={deleteChild}>delete focused child</button>
133+
<button
134+
onClick={() => deleteComponent({ componentId: focusComponent.id, stateComponents })}
135+
>
136+
delete focused components
137+
</button>
118138
</div>
119139
</MuiThemeProvider>
120140
);

src/reducers/componentReducer.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
DELETE_ALL_DATA,
2525
CHANGE_IMAGE_PATH,
2626
ADD_PROP,
27-
DELETE_PROP
28-
} from "../actionTypes";
27+
DELETE_PROP,
28+
} from '../actionTypes';
2929

3030
import {
3131
addComponent,
@@ -48,8 +48,8 @@ import {
4848
openExpansionPanel,
4949
changeImagePath,
5050
addProp,
51-
deleteProp
52-
} from "../utils/componentReducer.util";
51+
deleteProp,
52+
} from '../utils/componentReducer.util';
5353

5454
// interface Child {
5555
// childId: number;
@@ -66,11 +66,11 @@ import {
6666
// }
6767

6868
const appComponent = {
69-
id: "1",
69+
id: '1',
7070
stateful: false,
71-
title: "App",
71+
title: 'App',
7272
parentIds: [],
73-
color: "#FF6D00",
73+
color: '#FF6D00',
7474
draggable: true,
7575
childrenIds: [],
7676
selectableParents: [],
@@ -81,12 +81,12 @@ const appComponent = {
8181
x: 110,
8282
y: 120,
8383
width: 50,
84-
height: 50
84+
height: 50,
8585
},
8686

8787
childrenArray: [],
8888
nextChildId: 1,
89-
focusChild: null
89+
focusChild: null,
9090
};
9191

9292
const initialApplicationState = {
@@ -105,13 +105,13 @@ const initialApplicationState = {
105105
x: 110,
106106
y: 120,
107107
width: 50,
108-
height: 50
108+
height: 50,
109109
},
110-
draggable: true
110+
draggable: true,
111111
},
112112
components: [appComponent],
113-
appDir: "",
114-
loading: false
113+
appDir: '',
114+
loading: false,
115115
};
116116

117117
const componentReducer = (state = initialApplicationState, action) => {
@@ -121,9 +121,9 @@ const componentReducer = (state = initialApplicationState, action) => {
121121
...state,
122122
...action.payload.data,
123123
loading: false,
124-
appDir: "",
124+
appDir: '',
125125
successOpen: false,
126-
errorOpen: false
126+
errorOpen: false,
127127
};
128128
case ADD_COMPONENT:
129129
return addComponent(state, action.payload);

0 commit comments

Comments
 (0)