Skip to content

Commit 8c428ad

Browse files
added interfaces of major state objects to findComponentById and removed loosely equal comparators
1 parent 732efed commit 8c428ad

File tree

8 files changed

+79
-43
lines changed

8 files changed

+79
-43
lines changed

src/actions/components.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,15 @@ export const deleteChild = ({}) => dispatch => {
7878

7979
export const deleteComponent = ({ componentId, stateComponents }) => dispatch => {
8080
// find all places where the "to be delted" is a child and do what u gotta do
81-
stateComponents.forEach(parent => {
82-
parent.childrenArray
83-
.filter(child => child.childComponentId == componentId)
84-
.forEach(child => {
85-
dispatch({
86-
type: DELETE_CHILD,
87-
payload: {
88-
parentId: parent.id,
89-
childId: child.childId,
90-
calledFromDeleteComponent: true,
91-
},
92-
});
81+
stateComponents.forEach((parent) => {
82+
parent.childrenArray.filter(child => child.childComponentId === componentId).forEach((child) => {
83+
dispatch({
84+
type: DELETE_CHILD,
85+
payload: {
86+
parentId: parent.id,
87+
childId: child.childId,
88+
calledFromDeleteComponent: true,
89+
},
9390
});
9491
});
9592

src/components/GrandchildRectangle.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React, { Component } from 'react';
22
import { Rect, Group } from 'react-konva';
3+
// import findComponentById from '../utils/findComponentById.ts';
34

45
class GrandchildRectangle extends Component {
56
getComponentColor(componentId) {
6-
const color = this.props.components.find(comp => comp.id == componentId).color;
7+
// const color = findComponentById(componentId, this.props.components).color;
8+
const color = this.props.components.find(comp => comp.id === componentId).color;
79
return color;
810
}
911

src/components/LeftColExpansionPanel.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ const LeftColExpansionPanel = (props) => {
2828

2929
// show a string of all direct parents. SO the user can gaze at it.
3030
const directParents = components
31-
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId == id))
31+
.filter(comp => comp.childrenArray.some(child => child.childComponentId === id))
3232
.map(comp => comp.title)
3333
.join(',');
3434

3535
function isFocused() {
36-
return focusComponent.id == id ? 'focused' : '';
36+
return focusComponent.id === id ? 'focused' : '';
3737
}
3838

3939
return (
@@ -67,7 +67,7 @@ const LeftColExpansionPanel = (props) => {
6767
</ListItem>
6868
</List>
6969
</Grid>
70-
{id == 1 || !isFocused() ? (
70+
{id === 1 || !isFocused() ? (
7171
<div />
7272
) : (
7373
<Fragment>
@@ -134,7 +134,7 @@ const LeftColExpansionPanel = (props) => {
134134
</Grid>
135135

136136
<Grid item xs={3}>
137-
{id == 1 || isFocused() || !selectableChildren.includes(id) ? (
137+
{id === 1 || isFocused() || !selectableChildren.includes(id) ? (
138138
<div />
139139
) : (
140140
<IconButton aria-label="Add">

src/components/Rectangle.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Rectangle extends Component {
88
if (componentId === '888') {
99
return '#000000';
1010
}
11-
const color = this.props.components.find(comp => comp.id == componentId).color;
11+
const color = this.props.components.find(comp => comp.id === componentId).color;
1212
return color;
1313
}
1414

@@ -23,7 +23,7 @@ class Rectangle extends Component {
2323
.find(comp => comp.id === this.props.componentId)
2424
.childrenArray.find(child => child.childId === childId);
2525

26-
if (childId == -1) {
26+
if (childId === -1) {
2727
focChild = this.props.components.find(comp => comp.id === this.props.componentId);
2828
}
2929
const transformation = {
@@ -129,7 +129,7 @@ class Rectangle extends Component {
129129
</Label>
130130
{// for all children other than the pseudoChild, find their component's children array and recursively render the children found there
131131
childId !== -1 &&
132-
childType == 'COMP' &&
132+
childType === 'COMP' &&
133133
components
134134
.find(comp => comp.title === childComponentName)
135135
.childrenArray.filter(child => child.childId !== -1)

src/containers/MainContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class MainContainer extends Component {
205205
const directParents = !focusComponent.id
206206
? 'Waiting for a focused component'
207207
: stateComponents
208-
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId == focusComponent.id))
208+
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId === focusComponent.id))
209209
.map(comp => comp.title)
210210
.join(',');
211211

src/reducers/componentReducer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ const appComponent = {
7979
draggable: true,
8080
childrenIds: [],
8181
selectableParents: [],
82-
expanded: true,
8382
props: [],
8483
nextPropId: 1,
8584
position: {

src/utils/componentReducer.util.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const initialComponentState = {
1313
//draggable: true,
1414
childrenIds: [],
1515
selectableParents: [],
16-
expanded: true,
1716
props: [],
1817
nextPropId: 1,
1918
position: {
@@ -111,7 +110,7 @@ export const addChild = (state, { title, childType = '', HTMLInfo = {} }) => {
111110
}
112111

113112
let htmlElemPosition;
114-
if (childType == 'HTML') {
113+
if (childType === 'HTML') {
115114
htmlElemPosition = getSize(htmlElement);
116115
// if above function doesnt reutn anything, it means html element is not in our database
117116
if (!htmlElemPosition.width) {
@@ -139,7 +138,7 @@ export const addChild = (state, { title, childType = '', HTMLInfo = {} }) => {
139138
const newChild = {
140139
childId: view.nextChildId,
141140
childType,
142-
childComponentId: childType == 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
141+
childComponentId: childType === 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
143142
componentName: strippedTitle,
144143
position: newPosition,
145144
// draggable: true,
@@ -198,17 +197,17 @@ export const deleteChild = (
198197
return state;
199198
}
200199
// make a DEEP copy of the parent component (the one thats about to loose a child)
201-
const parentComponentCopy = cloneDeep(state.components.find(c => c.id == parentId));
200+
const parentComponentCopy = cloneDeep(state.components.find(c => c.id === parentId));
202201

203202
// delete the CHILD from the copied array
204-
const indexToDelete = parentComponentCopy.childrenArray.findIndex(elem => elem.childId == childId);
203+
const indexToDelete = parentComponentCopy.childrenArray.findIndex(elem => elem.childId === childId);
205204
if (indexToDelete < 0) {
206205
return window.alert('No such child component found');
207206
}
208207
parentComponentCopy.childrenArray.splice(indexToDelete, 1);
209208

210209
// if deleted child is selected, reset it
211-
if (parentComponentCopy.focusChildId == childId) {
210+
if (parentComponentCopy.focusChildId === childId) {
212211
parentComponentCopy.focusChildId = 0;
213212
}
214213

@@ -336,7 +335,7 @@ export const deleteComponent = (state, { componentId }) => {
336335
// ...state.components.slice(0, index),
337336
// ...state.components.slice(index + 1)
338337
// ];
339-
if (componentId == 1) {
338+
if (componentId === 1) {
340339
return {
341340
...state,
342341
};
@@ -368,7 +367,7 @@ export const changeFocusComponent = (state, { title = state.focusComponent.title
368367

369368
let newFocusChild; // check if the components has a child saved as a Focus child
370369
if (newFocusComp.focusChildId > 0) {
371-
newFocusChild = newFocusComp.childrenArray.find(child => child.childId == newFocusComp.focusChildId);
370+
newFocusChild = newFocusComp.childrenArray.find(child => child.childId === newFocusComp.focusChildId);
372371
}
373372

374373
// if no docus child found .. reset
@@ -417,9 +416,9 @@ export const changeFocusChild = (state, { title, childId }) => {
417416
};
418417

419418
export const changeComponentFocusChild = (state, { componentId, childId }) => {
420-
const component = state.components.find(comp => comp.id == componentId);
419+
const component = state.components.find(comp => comp.id === componentId);
421420
component.focusChildId = childId;
422-
const components = state.components.filter(comp => comp.id != componentId);
421+
const components = state.components.filter(comp => comp.id !== componentId);
423422
return {
424423
...state,
425424
components: [component, ...components],
@@ -584,7 +583,7 @@ export const addProp = (state, { key, value = null, required, type }) => {
584583
return state;
585584
}
586585

587-
const selectedComponent = state.components.find(comp => comp.id == state.focusComponent.id);
586+
const selectedComponent = state.components.find(comp => comp.id === state.focusComponent.id);
588587

589588
const newProp = {
590589
id: selectedComponent.nextPropId,
@@ -617,17 +616,17 @@ export const deleteProp = (state, propId) => {
617616
return state;
618617
}
619618
// make a deep copy of focusCOmponent. we are gonne be modifying that copy
620-
const modifiedComponent = cloneDeep(state.components.find(comp => comp.id == state.focusComponent.id));
619+
const modifiedComponent = cloneDeep(state.components.find(comp => comp.id === state.focusComponent.id));
621620

622-
const indexToDelete = modifiedComponent.props.findIndex(prop => prop.id == propId);
621+
const indexToDelete = modifiedComponent.props.findIndex(prop => prop.id === propId);
623622
if (indexToDelete < 0) {
624623
console.log(`Delete prop Error. Prop id:${propId} not found in ${modifiedComponent.title}`);
625624
return state;
626625
}
627626

628627
modifiedComponent.props.splice(indexToDelete, 1);
629628

630-
const newComponentsArray = state.components.filter(comp => comp.id != modifiedComponent.id);
629+
const newComponentsArray = state.components.filter(comp => comp.id !== modifiedComponent.id);
631630
newComponentsArray.push(modifiedComponent);
632631

633632
return {
@@ -647,14 +646,14 @@ export const updateHtmlAttr = (state, { attr, value }) => {
647646
modifiedChild.HTMLInfo[attr] = value;
648647

649648
// make a deep copy of focusCOmponent. we are gonne be modifying that copy
650-
const modifiedComponent = cloneDeep(state.components.find(comp => comp.id == state.focusComponent.id));
649+
const modifiedComponent = cloneDeep(state.components.find(comp => comp.id === state.focusComponent.id));
651650

652651
modifiedComponent.childrenArray = modifiedComponent.childrenArray.filter(
653-
child => child.childId != modifiedChild.childId,
652+
child => child.childId !== modifiedChild.childId,
654653
);
655654
modifiedComponent.childrenArray.push(modifiedChild);
656655

657-
const newComponentsArray = state.components.filter(comp => comp.id != modifiedComponent.id);
656+
const newComponentsArray = state.components.filter(comp => comp.id !== modifiedComponent.id);
658657
newComponentsArray.push(modifiedComponent);
659658

660659
return {

src/utils/findComponentById.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
1+
interface Prop {
2+
id: number;
3+
key: string;
4+
value: string;
5+
required: boolean;
6+
type: string;
7+
}
8+
9+
interface Position {
10+
x: number;
11+
y: number;
12+
width: number;
13+
height: number;
14+
}
115

2-
interface component{
3-
id: string;
16+
interface Child {
17+
childId: number;
18+
childType: string;
19+
childComponentId: number;
20+
componentName: string;
21+
position: Position;
22+
color: string | null; // maybe optional instead, look up null vs undefined
23+
htmlElement: string | null; // maybe should be optional instead
24+
HTMLInfo: object; // replace with HTMLinfo specifics
425
}
526

6-
function findComponentById(id: number, components: ){
27+
interface Component {
28+
id: number;
29+
stateful: boolean;
30+
title: string;
31+
parentIds: number[];
32+
color: string;
33+
draggable: boolean;
34+
childrenIds: number[];
35+
selectableParents?: any[]; // unused
36+
expanded?: boolean; // unused
37+
props: Prop[];
38+
nextPropId: number;
39+
position: Position;
40+
childrenArray: Child[];
41+
nextChildId: number;
42+
focusChildId: number;
43+
}
744

45+
function findComponentById(id: number, components: Component[]) {
46+
return components.find(comp => comp.id === id);
847
}
948

10-
export default findComponentById
49+
export default findComponentById;

0 commit comments

Comments
 (0)