Skip to content

Commit a4decf2

Browse files
added interfaces of major state objects to findComponentById and removed loosely equal comparators
1 parent 27b00f7 commit a4decf2

File tree

8 files changed

+71
-32
lines changed

8 files changed

+71
-32
lines changed

src/actions/components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const deleteChild = ({}) => (dispatch) => {
7777
export const deleteComponent = ({ componentId, stateComponents }) => (dispatch) => {
7878
// find all places where the "to be delted" is a child and do what u gotta do
7979
stateComponents.forEach((parent) => {
80-
parent.childrenArray.filter(child => child.childComponentId == componentId).forEach((child) => {
80+
parent.childrenArray.filter(child => child.childComponentId === componentId).forEach((child) => {
8181
dispatch({
8282
type: DELETE_CHILD,
8383
payload: {

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.jsx

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

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

28-
if (childId == -1) {
28+
if (childId === -1) {
2929
focChild = this.props.components.find(comp => comp.id === this.props.componentId);
3030
}
3131
const transformation = {
@@ -127,7 +127,7 @@ class Rectangle extends Component {
127127
</Label>
128128
{// for all children other than the pseudoChild, find their component's children array and recursively render the children found there
129129
childId !== -1
130-
&& childType == 'COMP'
130+
&& childType === 'COMP'
131131
&& components
132132
.find(comp => comp.title === childComponentName)
133133
.childrenArray.filter(child => child.childId !== -1)

src/containers/MainContainer.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class MainContainer extends Component {
196196
const directParents = !focusComponent.id
197197
? 'Waiting for a focused component'
198198
: stateComponents
199-
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId == focusComponent.id))
199+
.filter(comp => comp.childrenArray.some(kiddy => kiddy.childComponentId === focusComponent.id))
200200
.map(comp => comp.title)
201201
.join(',');
202202

src/reducers/componentReducer.js

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: {
@@ -115,7 +114,7 @@ export const addChild = (state, { title, childType = '', HTMLInfo = {} }) => {
115114
}
116115

117116
let htmlElemPosition;
118-
if (childType == 'HTML') {
117+
if (childType === 'HTML') {
119118
htmlElemPosition = getSize(htmlElement);
120119
// if above function doesnt reutn anything, it means html element is not in our database
121120
if (!htmlElemPosition.width) {
@@ -144,7 +143,7 @@ export const addChild = (state, { title, childType = '', HTMLInfo = {} }) => {
144143
const newChild = {
145144
childId: view.nextChildId,
146145
childType,
147-
childComponentId: childType == 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
146+
childComponentId: childType === 'COMP' ? parentComponent.id : null, // only relevant fot children of type COMPONENT
148147
componentName: strippedTitle,
149148
position: newPosition,
150149
// draggable: true,
@@ -207,19 +206,19 @@ export const deleteChild = (
207206
return state;
208207
}
209208
// make a DEEP copy of the parent component (the one thats about to loose a child)
210-
const parentComponentCopy = cloneDeep(state.components.find(c => c.id == parentId));
209+
const parentComponentCopy = cloneDeep(state.components.find(c => c.id === parentId));
211210

212211
// delete the CHILD from the copied array
213212
const indexToDelete = parentComponentCopy.childrenArray.findIndex(
214-
elem => elem.childId == childId,
213+
elem => elem.childId === childId,
215214
);
216215
if (indexToDelete < 0) {
217216
return window.alert('No such child component found');
218217
}
219218
parentComponentCopy.childrenArray.splice(indexToDelete, 1);
220219

221220
// if deleted child is selected, reset it
222-
if (parentComponentCopy.focusChildId == childId) {
221+
if (parentComponentCopy.focusChildId === childId) {
223222
parentComponentCopy.focusChildId = 0;
224223
}
225224

@@ -347,7 +346,7 @@ export const deleteComponent = (state, { componentId }) => {
347346
// ...state.components.slice(0, index),
348347
// ...state.components.slice(index + 1)
349348
// ];
350-
if (componentId == 1) {
349+
if (componentId === 1) {
351350
return {
352351
...state,
353352
};
@@ -380,7 +379,7 @@ export const changeFocusComponent = (state, { title = state.focusComponent.title
380379
let newFocusChild; // check if the components has a child saved as a Focus child
381380
if (newFocusComp.focusChildId > 0) {
382381
newFocusChild = newFocusComp.childrenArray.find(
383-
child => child.childId == newFocusComp.focusChildId,
382+
child => child.childId === newFocusComp.focusChildId,
384383
);
385384
}
386385

@@ -430,9 +429,9 @@ export const changeFocusChild = (state, { title, childId }) => {
430429
};
431430

432431
export const changeComponentFocusChild = (state, { componentId, childId }) => {
433-
const component = state.components.find(comp => comp.id == componentId);
432+
const component = state.components.find(comp => comp.id === componentId);
434433
component.focusChildId = childId;
435-
const components = state.components.filter(comp => comp.id != componentId);
434+
const components = state.components.filter(comp => comp.id !== componentId);
436435
return {
437436
...state,
438437
components: [component, ...components],
@@ -599,7 +598,7 @@ export const addProp = (state, {
599598
return state;
600599
}
601600

602-
const selectedComponent = state.components.find(comp => comp.id == state.focusComponent.id);
601+
const selectedComponent = state.components.find(comp => comp.id === state.focusComponent.id);
603602

604603
const newProp = {
605604
id: selectedComponent.nextPropId,
@@ -633,18 +632,18 @@ export const deleteProp = (state, propId) => {
633632
}
634633
// make a deep copy of focusCOmponent. we are gonne be modifying that copy
635634
const modifiedComponent = cloneDeep(
636-
state.components.find(comp => comp.id == state.focusComponent.id),
635+
state.components.find(comp => comp.id === state.focusComponent.id),
637636
);
638637

639-
const indexToDelete = modifiedComponent.props.findIndex(prop => prop.id == propId);
638+
const indexToDelete = modifiedComponent.props.findIndex(prop => prop.id === propId);
640639
if (indexToDelete < 0) {
641640
console.log(`Delete prop Error. Prop id:${propId} not found in ${modifiedComponent.title}`);
642641
return state;
643642
}
644643

645644
modifiedComponent.props.splice(indexToDelete, 1);
646645

647-
const newComponentsArray = state.components.filter(comp => comp.id != modifiedComponent.id);
646+
const newComponentsArray = state.components.filter(comp => comp.id !== modifiedComponent.id);
648647
newComponentsArray.push(modifiedComponent);
649648

650649
return {
@@ -665,15 +664,15 @@ export const updateHtmlAttr = (state, { attr, value }) => {
665664

666665
// make a deep copy of focusCOmponent. we are gonne be modifying that copy
667666
const modifiedComponent = cloneDeep(
668-
state.components.find(comp => comp.id == state.focusComponent.id),
667+
state.components.find(comp => comp.id === state.focusComponent.id),
669668
);
670669

671670
modifiedComponent.childrenArray = modifiedComponent.childrenArray.filter(
672-
child => child.childId != modifiedChild.childId,
671+
child => child.childId !== modifiedChild.childId,
673672
);
674673
modifiedComponent.childrenArray.push(modifiedChild);
675674

676-
const newComponentsArray = state.components.filter(comp => comp.id != modifiedComponent.id);
675+
const newComponentsArray = state.components.filter(comp => comp.id !== modifiedComponent.id);
677676
newComponentsArray.push(modifiedComponent);
678677

679678
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)