Skip to content

Commit 9b0dc2b

Browse files
merged with Shlomo delete button
2 parents 60cb036 + c31cec3 commit 9b0dc2b

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

src/actionTypes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const LOAD_INIT_DATA = "LOAD_INIT_DATA";
22
export const ADD_COMPONENT = "ADD_COMPONENT";
33
export const ADD_CHILD = "ADD_CHILD";
4+
export const DELETE_CHILD = "DELETE_CHILD";
45
export const UPDATE_COMPONENT = "UPDATE_COMPONENT";
56
export const DELETE_COMPONENT = "DELETE_COMPONENT";
67
export const CHANGE_FOCUS_COMPONENT = "CHANGE_FOCUS_COMPONENT";

src/actions/components.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
LOAD_INIT_DATA,
33
ADD_COMPONENT,
44
ADD_CHILD,
5+
DELETE_CHILD,
56
UPDATE_COMPONENT,
67
DELETE_COMPONENT,
78
CHANGE_FOCUS_COMPONENT,
@@ -63,12 +64,14 @@ export const parentReassignment = ({ index, id, parentIds }) => ({
6364

6465
export const addComponent = ({ title }) => dispatch => {
6566
dispatch({ type: ADD_COMPONENT, payload: { title } });
66-
// dispatch({ type: SET_SELECTABLE_PARENTS });
6767
};
6868

6969
export const addChild = ({ title }) => dispatch => {
7070
dispatch({ type: ADD_CHILD, payload: { title } });
71-
// dispatch({ type: SET_SELECTABLE_PARENTS });
71+
};
72+
73+
export const deleteChild = ({ title }) => dispatch => {
74+
dispatch({ type: DELETE_CHILD, payload: { title } });
7275
};
7376

7477
export const deleteComponent = ({ index, id, parentIds = [] }) => dispatch => {

src/containers/MainContainer.jsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
openExpansionPanel,
1313
handleTransform,
1414
changeFocusChild,
15+
deleteChild,
1516
} from '../actions/components';
1617
import KonvaStage from '../components/KonvaStage.jsx';
1718
// import MainContainerHeader from '../components/MainContainerHeader.jsx';
@@ -34,6 +35,7 @@ const mapDispatchToProps = dispatch => ({
3435
toggleComponentDragging: status => dispatch(toggleDragging(status)),
3536
openPanel: component => dispatch(openExpansionPanel(component)),
3637
changeFocusChild: ({ title, childId }) => dispatch(changeFocusChild({ title, childId })),
38+
deleteChild: ({ childId }) => dispatch(deleteChild({ childId })),
3739
});
3840

3941
const mapStateToProps = store => ({
@@ -58,20 +60,6 @@ class MainContainer extends Component {
5860

5961
componentDidMount() {}
6062

61-
// increaseHeight = () => {
62-
// this.setState({
63-
// scaleX: this.state.scaleX * 1.5,
64-
// scaleY: this.state.scaleY * 1.5,
65-
// });
66-
// };
67-
68-
// decreaseHeight = () => {
69-
// this.setState({
70-
// scaleX: this.state.scaleX * 0.75,
71-
// scaleY: this.state.scaleY * 0.75,
72-
// });
73-
// };
74-
7563
toggleDrag = () => {
7664
this.props.toggleComponentDragging(this.state.draggable);
7765
this.setState({
@@ -94,6 +82,7 @@ class MainContainer extends Component {
9482
focusComponent,
9583
focusChild,
9684
changeFocusChild,
85+
deleteChild,
9786
} = this.props;
9887
const {
9988
increaseHeight,
@@ -122,8 +111,10 @@ class MainContainer extends Component {
122111
focusComponent={focusComponent}
123112
focusChild={focusChild}
124113
changeFocusChild={changeFocusChild}
114+
deleteChild={deleteChild}
125115
/>
126116
</div>
117+
<button onClick={deleteChild}>`delete focused child`</button>
127118
</div>
128119
</MuiThemeProvider>
129120
);

src/reducers/componentReducer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
LOAD_INIT_DATA,
33
ADD_COMPONENT,
44
ADD_CHILD,
5+
DELETE_CHILD,
56
UPDATE_COMPONENT,
67
DELETE_COMPONENT,
78
CHANGE_FOCUS_COMPONENT,
@@ -29,6 +30,7 @@ import {
2930
import {
3031
addComponent,
3132
addChild,
33+
deleteChild,
3234
updateComponent,
3335
deleteComponent,
3436
changeFocusComponent,
@@ -127,6 +129,8 @@ const componentReducer = (state = initialApplicationState, action) => {
127129
return addComponent(state, action.payload);
128130
case ADD_CHILD:
129131
return addChild(state, action.payload);
132+
case DELETE_CHILD:
133+
return deleteChild(state, action.payload);
130134
case UPDATE_COMPONENT:
131135
return updateComponent(state, action.payload);
132136
case DELETE_COMPONENT:

src/utils/componentReducer.util.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export const addComponent = (state, { title }) => {
3232
.replace(/[-_\s0-9\W]+/gi, '');
3333

3434
if (state.components.find(comp => comp.title === strippedTitle)) {
35-
// alert the user that duplicate component names are not allowed
35+
window.alert(
36+
`a component with the name: "${strippedTitle}" already exists.\n Please think of another name.`,
37+
);
3638
return {
3739
...state,
3840
};
@@ -68,18 +70,17 @@ export const addComponent = (state, { title }) => {
6870
// get the focus component (aka the component were adding the child to)
6971

7072
export const addChild = (state, { title }) => {
71-
const strippedTitle = title
72-
.replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
73-
.replace(/[-_\s0-9\W]+/gi, '');
73+
const strippedTitle = title;
74+
// .replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
75+
// .replace(/[-_\s0-9\W]+/gi, "");
7476

75-
// view represents the component that this child will live (and be rendered) in
77+
// view represents the curretn FOCUSED COMPONENT - this is the component where the child is being added to
78+
// we only add childrent (or do any action) to the focused omconent
7679
const view = state.components.find(comp => comp.title === state.focusComponent.title);
7780

7881
// parentComponent is the component this child is generated from (ex. instance of Box has comp of Box)
7982
const parentComponent = state.components.find(comp => comp.title === title);
8083

81-
console.log('view from addChild: ', view);
82-
8384
const newChild = {
8485
childId: view.nextChildId.toString(),
8586
childComponentId: parentComponent.id,
@@ -117,6 +118,48 @@ export const addChild = (state, { title }) => {
117118
};
118119
};
119120

121+
export const deleteChild = (state, {}) => {
122+
// The child to delete is the FOCUSED child
123+
// THE parent is the FOCUSED component
124+
125+
if (!state.focusComponent.id) return window.alert('Cannot delete Child if Focused component id = ZERO');
126+
if (!state.focusChild.childId) return window.alert('Cannot delete Child if Focused Child id = ZERO');
127+
// I'm not sure what the "focused component" is (a reference, a copy or ?
128+
// So, let's get a reference to the focused component ..
129+
const currFocusedComponent = state.components.find(c => c.id == state.focusComponent.id);
130+
131+
// copy the Children Array of the foxused component
132+
const copyOfChildrenArray = [...currFocusedComponent.childrenArray];
133+
// delete the SELECTED CHILD from the copied array
134+
const indexToDelete = copyOfChildrenArray.findIndex(
135+
elem => elem.childId == state.focusChild.childId,
136+
);
137+
138+
if (indexToDelete < 0) return window.alert('DeleteChild speaking here. The chils u r trying to delete was not found');
139+
140+
copyOfChildrenArray.splice(indexToDelete, 1);
141+
142+
// Make a copy of the focused component - this is the one we will modify
143+
const copyOfFocusedComponent = {
144+
...currFocusedComponent,
145+
childrenArray: copyOfChildrenArray,
146+
};
147+
148+
// copy the entire compoenents array from state
149+
const modifiedComponentArray = [
150+
...state.components.filter(c => c.id !== currFocusedComponent.id), // all elements besides the one just changed
151+
copyOfFocusedComponent,
152+
];
153+
154+
// RETURN - update state...
155+
return {
156+
...state,
157+
components: modifiedComponentArray,
158+
focusComponent: copyOfFocusedComponent, // problem! the delete works on State.components, but not on FOcus component.. starnge..
159+
focusChild: {}, // reset to blank.
160+
};
161+
};
162+
120163
export const handleTransform = (state, {
121164
componentId, childId, x, y, width, height,
122165
}) => {

0 commit comments

Comments
 (0)