Skip to content

Commit e4ace33

Browse files
author
Mitchel Severe
committed
refactor material ui imports and delete and add child refactor
1 parent 74eae90 commit e4ace33

18 files changed

+423
-403
lines changed

src/App.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React from 'react';
22
import './public/styles/style.css';
33
import AppContainer from './containers/AppContainer';
4-
// import Test from './components/Test';
54

65
const App: React.FC = () => (
76
<div className="app">
87
<AppContainer />
9-
{/* <Test /> */}
108
</div>
119
);
1210

src/actions/actions.ts

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createComponent } from '../utils/reducer.util';
2-
import { ComponentState } from '../types/types';
2+
import { ComponentState, ChildState } from '../types/types';
33
import * as types from '../types/actionTypes';
44
import { loadState } from '../localStorage.js';
55
import createFiles from '../utils/createFiles.util';
@@ -35,18 +35,16 @@ export const changeImagePath = (imageSource: string) => ({
3535
export const addComponent = (title: string) => {
3636
return (dispatch, getState) => {
3737
// ** grab state from our reducer to see how many components currently exist in our array
38-
const appComponentState = getState().application.components;
39-
// ** Conditionally assigning a variable componentId. If any components exist in the array find the last component's id and increment it else initialize at 1.
40-
const componentId = appComponentState.length ? appComponentState[appComponentState.length - 1].id + 1 : 1;
41-
dispatch(createComponent(componentId, title, appComponentState))
38+
const app = getState().application;
39+
const componentId = app.nextComponentId;
40+
const components = app.components;
41+
dispatch(createComponent(componentId, title, components))
4242
.then((component: ComponentState) => {
43-
// ** the dispatch to createComponent will return a promise. The promise will return either the new createdComponent or just the generic state object (which will have an id of 0 by default)
44-
if (component.id !== 0) {
45-
dispatch({
46-
type: types.ADD_COMPONENT,
47-
payload: { component }
48-
});
49-
}
43+
// ** the dispatch to createComponent will return a promise. The promise will return either the new createdComponent.
44+
dispatch({
45+
type: types.ADD_COMPONENT,
46+
payload: { component }
47+
});
5048
})
5149
.catch((err: Error) => console.log(err));
5250
};
@@ -62,26 +60,47 @@ export const updateComponent = (id: number, update: {}) => ({
6260
});
6361

6462
// ** deleteComponent deletes a component from our global state component array
65-
export const deleteComponent = (id: number) => ({
66-
type: types.DELETE_COMPONENT,
67-
payload: { id }
68-
});
69-
70-
export const addChild = ({
71-
title,
72-
childType,
73-
HTMLInfo,
74-
}: {
75-
title: string;
76-
childType: string;
77-
HTMLInfo: object;
78-
}) => (dispatch: any) => {
79-
dispatch({ type: types.ADD_CHILD, payload: { title, childType, HTMLInfo } });
63+
export const deleteComponent = (id: number) => {
64+
return (dispatch, getState) => {
65+
const appComponents = getState().application.components;
66+
appComponents.forEach((parent: ComponentState) => {
67+
parent.children
68+
.filter((child: ChildState) => child.childComponentId === id)
69+
.forEach((child: ChildState) => {
70+
dispatch({
71+
type: types.DELETE_CHILD,
72+
payload: {
73+
parentId: parent.id,
74+
childId: child.childId,
75+
// calledFromDeleteComponent: true,
76+
},
77+
});
78+
});
79+
});
80+
dispatch({
81+
type: types.DELETE_COMPONENT,
82+
payload: { id }
83+
});
84+
}
8085
};
8186

82-
export const deleteChild = ({}) => (dispatch: any) => {
83-
// with no payload, it will delete focusd child
84-
dispatch({ type: types.DELETE_CHILD, payload: {} });
87+
export const addChild = (title: string, childType: string, HTMLInfo?: {}) => ({
88+
type: types.ADD_CHILD,
89+
payload: { title, childType, HTMLInfo }
90+
});
91+
92+
export const deleteChild = (id: number) => {
93+
return (dispatch, getState) => {
94+
const parent = getState().application.focusComponent;
95+
const child = parent.children.find((currentChild: ChildState) => id === currentChild.childComponentId);
96+
dispatch({
97+
type: types.DELETE_CHILD,
98+
payload: {
99+
parentId: parent.id,
100+
childId: child.childId,
101+
},
102+
});
103+
}
85104
};
86105

87106
export const changeFocusComponent = ({ title }: { title: string }) => (dispatch: any) => {

src/components/DataTable.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import React from 'react';
2-
import { withStyles } from '@material-ui/core/styles';
3-
import Table from '@material-ui/core/Table';
4-
import TableBody from '@material-ui/core/TableBody';
5-
import TableCell from '@material-ui/core/TableCell';
6-
import TableHead from '@material-ui/core/TableHead';
7-
import TableRow from '@material-ui/core/TableRow';
8-
import Paper from '@material-ui/core/Paper';
9-
import DeleteIcon from '@material-ui/icons/Delete';
10-
import { IconButton } from '@material-ui/core';
2+
import { withStyles, Table, TableBody, TableCell, TableHead, TableRow, Paper, DeleteIcon, IconButton } from '../utils/material.util';
113

124
const styles = (theme: any) => ({
135
root: {

src/components/HTMLComponentPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class HTMLComponentPanel extends Component<PropsInt, StateInt> {
3232
};
3333

3434
handleCreateHTMLChild = (type: string) => {
35-
this.props.addChild({ title: type, childType: type, HTMLInfo: {} });
35+
this.props.addChild(type, type, {});
3636
};
3737

3838
render() {

src/components/HtmlAttr.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import React, { Component } from 'react';
22
import { connect } from 'react-redux';
3-
import { withStyles } from '@material-ui/core/styles';
4-
import Grid from '@material-ui/core/Grid';
5-
import TextField from '@material-ui/core/TextField';
6-
import SaveIcon from '@material-ui/icons/Save';
7-
import Paper from '@material-ui/core/Paper';
8-
import Fab from '@material-ui/core/Fab';
3+
import { withStyles, Grid, TextField, SaveIcon, Paper, Fab } from '../utils/material.util';
94
import { updateHtmlAttr } from '../actions/actions';
105
import { HTMLelements } from '../utils/htmlElements.util';
116
import { ComponentState, ChildState } from '../types/types';

src/components/KonvaStage.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type Props = {
2020
handleTransform: any;
2121
focusChild: any;
2222
changeComponentFocusChild: any;
23-
deleteChild: any;
2423
width: number;
2524
height: number;
2625
}
@@ -69,18 +68,19 @@ class KonvaStage extends Component<Props, State> {
6968
}
7069

7170
componentDidMount() {
72-
this.stage.current.addEventListener('keydown', this.handleKeyDown);
71+
window.addEventListener('keydown', this.handleKeyDown);
7372
}
7473

7574
componentWillUnmount() {
76-
this.stage.current.removeEventListener('keydown', this.handleKeyDown);
75+
window.removeEventListener('keydown', this.handleKeyDown);
7776
}
7877

7978
handleKeyDown = (e: any) => {
8079
// backspace and delete keys are keyCode 8 and 46, respectively
8180
// this function is only used for deleting children atm, could be used for other things
8281
if (e.keyCode === 8 || e.keyCode === 46) {
83-
this.props.deleteChild({});
82+
const { focusComponent, deleteComponent } = this.props;
83+
deleteComponent(focusComponent.id);
8484
}
8585
};
8686

@@ -97,7 +97,6 @@ class KonvaStage extends Component<Props, State> {
9797

9898
// find clicked rect by its name
9999
const rectChildId = e.target.attrs.childId;
100-
// console.log("user clicked on child rectangle with childId: ", rectChildId);
101100
this.props.changeFocusChild({ childId: rectChildId });
102101
this.props.changeComponentFocusChild({
103102
componentId: this.props.focusComponent.id,

0 commit comments

Comments
 (0)