Skip to content

Commit c31cec3

Browse files
Merge pull request #32 from ShlomoPorges/development
Added DELETE CHILD (action, reducer, button and more)
2 parents fb1f985 + 43d9f00 commit c31cec3

File tree

7 files changed

+79
-8
lines changed

7 files changed

+79
-8
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/components/KonvaStage.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class KonvaStage extends Component {
5151
focusComponent,
5252
focusChild,
5353
changeFocusChild,
54+
deleteChild,
5455
} = this.props;
5556
const { selectedShapeName } = this.state;
5657

@@ -83,6 +84,7 @@ class KonvaStage extends Component {
8384
title={child.componentName + child.childId}
8485
color={child.color}
8586
handleTransform={handleTransform}
87+
deleteChild={deleteChild}
8688
/>
8789
))}
8890
</Layer>

src/components/Rectangle.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Rectangle extends Component {
5656
focusChild,
5757
focusComponent,
5858
components,
59+
deleteChild
5960
} = this.props;
6061

6162
// the Group is responsible for dragging of all children

src/containers/MainContainer.jsx

Lines changed: 5 additions & 0 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 => ({
@@ -94,6 +96,7 @@ class MainContainer extends Component {
9496
focusComponent,
9597
focusChild,
9698
changeFocusChild,
99+
deleteChild,
97100
} = this.props;
98101
const {
99102
increaseHeight,
@@ -122,8 +125,10 @@ class MainContainer extends Component {
122125
focusComponent={focusComponent}
123126
focusChild={focusChild}
124127
changeFocusChild={changeFocusChild}
128+
deleteChild={deleteChild}
125129
/>
126130
</div>
131+
<button onClick={deleteChild} >`delete focused child`</button>
127132
</div>
128133
</MuiThemeProvider>
129134
);

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: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ 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(`a component with the name: "${strippedTitle}" already exists.\n Please think of another name.`);
3636
return {
3737
...state
3838
};
@@ -68,19 +68,20 @@ export const addComponent = (state, { title }) => {
6868
// get the focus component (aka the component were adding the child to)
6969

7070
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, "");
71+
const strippedTitle = title;
72+
// .replace(/[a-z]+/gi, word => word[0].toUpperCase() + word.slice(1))
73+
// .replace(/[-_\s0-9\W]+/gi, "");
7474

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

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

83-
console.log("view from addChild: ", view);
84+
// console.log("view from addChild: ", view);
8485

8586
const newChild = {
8687
childId: view.nextChildId.toString(),
@@ -119,6 +120,60 @@ export const addChild = (state, { title }) => {
119120
};
120121
};
121122

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

0 commit comments

Comments
 (0)