Skip to content

Commit e804601

Browse files
committed
added Route type and button to add Routes
1 parent 9383573 commit e804601

File tree

10 files changed

+106
-15
lines changed

10 files changed

+106
-15
lines changed

app/src/components/left/DragDropPanel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Central state contains all available HTML elements (stored in the HTMLTypes prop
1414
initialState.tsx.
1515
1616
Hook state:
17-
-tag:
17+
-tag:
1818
*/
1919
// Extracted the drag and drop functionality from HTMLPanel to make a modular component that can hang wherever the future designers may choose.
2020
const DragDropPanel = (props): JSX.Element => {
@@ -37,9 +37,9 @@ const DragDropPanel = (props): JSX.Element => {
3737
payload: id
3838
});
3939
};
40-
40+
4141
// filter out separator so that it will not appear on the html panel
42-
const htmlTypesToRender = state.HTMLTypes.filter(type => type.name !== 'separator');
42+
const htmlTypesToRender = state.HTMLTypes.filter(type => type.name !== 'separator' && type.name !== 'Route');
4343
return (
4444
<div className="HTMLItems">
4545
<div id="HTMLItemsTopHalf">

app/src/components/main/AddRoute.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { AddRoutes } from '../../interfaces/Interfaces'
2+
import React, {
3+
useRef, useState, useContext, useEffect,
4+
} from 'react';
5+
import StateContext from '../../context/context';
6+
7+
function AddRoute({
8+
id,
9+
name
10+
}: AddRoutes) {
11+
const [state, dispatch] = useContext(StateContext);
12+
13+
const handleClick = (id) => {
14+
dispatch({
15+
type: 'ADD CHILD',
16+
payload: {
17+
type: 'HTML Element',
18+
typeId: -1,
19+
childId: id // this is the id of the parent to attach it to
20+
}
21+
});
22+
}
23+
24+
return (
25+
<div style={{ padding: '1px', float: 'right' }}>
26+
<button id={'routeBtn' + id} onClick={() => handleClick(id)}>+</button>
27+
</div>
28+
);
29+
}
30+
31+
export default AddRoute;

app/src/components/main/Annotation.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Annotation({
3636
};
3737

3838
/**
39-
* Handles when text exists in the textarea of the modal.
39+
* Handles when text exists in the textarea of the modal.
4040
* If text exists/does not exist, corresponding button changes colors.
4141
* Sets hook value to what is contained in the textarea
4242
*/
@@ -53,7 +53,7 @@ function Annotation({
5353
}
5454

5555
/**
56-
* This handler will find the specific anno for the corresponding component on the canvas in the childrenArray -
56+
* This handler will find the specific anno for the corresponding component on the canvas in the childrenArray -
5757
* where the canvas components are placed
5858
*/
5959
const handleFindAnno = (array, id) => {
@@ -65,7 +65,7 @@ function Annotation({
6565
} else if (currentElement.children.length > 0) {
6666
// temp is to prevent a return of empty string since canvas element should always exist and allows the
6767
// recursion to continue
68-
const temp = handleFindAnno(currentElement.children, id)
68+
const temp = handleFindAnno(currentElement.children, id)
6969
if (temp != '') {
7070
return temp;
7171
}
@@ -84,7 +84,7 @@ function Annotation({
8484
}
8585
handleAnnoChange(event);
8686
}, [])
87-
87+
8888
const body = (
8989
<div className='annotate-position'>
9090
<span className='annotate-textarea-header'> Notes for: {name} ( {id} )</span>

app/src/components/main/Canvas.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Canvas() {
1616
const currentComponent: Component = state.components.find(
1717
(elem: Component) => elem.id === state.canvasFocus.componentId
1818
);
19-
19+
2020
// changes focus of the canvas to a new component / child
2121
const changeFocus = (componentId?: number, childId?: number | null) => {
2222
dispatch({ type: 'CHANGE FOCUS', payload: { componentId, childId } });
@@ -27,7 +27,7 @@ function Canvas() {
2727
// note: a null value for the child id means that we are focusing on the top-level component rather than any child
2828
changeFocus(state.canvasFocus.componentId, null);
2929
};
30-
30+
3131
// stores a snapshot of state into the past array for UNDO. snapShotFunc is also invoked for nestable elements in DirectChildHTMLNestable.tsx
3232
const snapShotFunc = () => {
3333
// make a deep clone of state
@@ -50,6 +50,7 @@ function Canvas() {
5050
}
5151
// if item dropped is going to be a new instance (i.e. it came from the left panel), then create a new child component
5252
if (item.newInstance) {
53+
console.log('Canvas first dispatch')
5354
dispatch({
5455
type: 'ADD CHILD',
5556
payload: {

app/src/components/main/DirectChildHTMLNestable.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import renderChildren from '../../helperFunctions/renderChildren';
99
import Annotation from './Annotation'
1010
import validateNewParent from '../../helperFunctions/changePositionValidation'
1111
import componentNest from '../../helperFunctions/componentNestValidation'
12+
// import addRoute from '../../helperFunctions/addRoute';
13+
import AddRoute from './AddRoute';
1214

1315
function DirectChildHTMLNestable({
1416
childId,
@@ -79,7 +81,7 @@ const snapShotFunc = () => {
7981
childId: childId,
8082
}
8183
});
82-
}
84+
}
8385
}
8486
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
8587
else {
@@ -95,7 +97,7 @@ const snapShotFunc = () => {
9597
}
9698
}
9799
},
98-
100+
99101
collect: (monitor: any) => {
100102
return {
101103
isOver: !!monitor.isOver({ shallow: true })
@@ -132,10 +134,16 @@ const snapShotFunc = () => {
132134

133135
drag(drop(ref));
134136

137+
const routeButton = [];
138+
if (typeId === 17) {
139+
routeButton.push(<AddRoute id={childId} name={name} />);
140+
}
141+
135142
return (
136143
<div onClick={onClickHandler} style={combinedStyle} ref={ref} id={`canv${childId}`}>
137144
<strong>{HTMLType.placeHolderShort}</strong>
138145
{` ( ${childId} )`}
146+
{routeButton}
139147
<Annotation
140148
id={childId}
141149
name={name}

app/src/components/main/SeparatorChild.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function DirectChildHTMLNestable({
6464
childId: childId,
6565
}
6666
});
67-
}
67+
}
6868
}
6969
// if item is not a new instance, change position of element dragged inside separator so that separator is new parent (until replacement)
7070
else {

app/src/context/HTMLTypes.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,24 @@ const HTMLTypes: HTMLType[] = [
155155
placeHolderShort: 'list item',
156156
placeHolderLong: '',
157157
icon: ListIcon
158-
},
158+
},
159159
{
160160
id: 17,
161161
tag: 'Switch',
162162
name: 'Switch',
163163
style: {},
164-
placeHolderShort: 'switch item',
164+
placeHolderShort: 'Switch',
165165
placeHolderLong: '',
166-
icon: ListIcon
166+
icon: HeaderIcon
167+
},
168+
{
169+
id: -1,
170+
tag: 'Route',
171+
name: 'Route',
172+
style: {},
173+
placeHolderShort: 'Route',
174+
placeHolderLong: '',
175+
icon: LinkIcon
167176
}
168177
];
169178

app/src/helperFunctions/addRoute.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
State,
3+
Action,
4+
Component,
5+
ChildElement,
6+
HTMLType
7+
} from '../interfaces/Interfaces';
8+
import React, { useState, useCallback, useContext, useEffect } from 'react';
9+
import StateContext from '../context/context';
10+
11+
12+
// const addRoute = (switchItem: ChildElement) => {
13+
const addRoute = (id: number) => {
14+
const [state, dispatch] = useContext(StateContext);
15+
console.log('in addRoute');
16+
// dispatch ADD CHILD to Switch?
17+
dispatch({
18+
type: 'ADD CHILD',
19+
payload: {
20+
type: 'HTML Element',
21+
typeId: -1,
22+
childId: id
23+
}
24+
});
25+
};
26+
27+
export default addRoute;

app/src/interfaces/Interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ export interface StatePropsPanelProps {
9999
selectHandler: (table: any) => void;
100100
deleteHandler: (id: number | any) => void;
101101
}
102+
103+
export interface AddRoutes {
104+
id: number;
105+
name: string;
106+
}

app/src/reducers/componentReducer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import initialState from '../context/initialState';
99
import generateCode from '../helperFunctions/generateCode';
1010
import manageSeparators from '../helperFunctions/manageSeparators';
11+
import addRoute from '../helperFunctions/addRoute';
1112

1213
let separator = initialState.HTMLTypes[1];
1314

@@ -308,6 +309,7 @@ const reducer = (state: State, action: Action) => {
308309
children: []
309310
};
310311

312+
311313
// if the childId is null, this signifies that we are adding a child to the top-level component rather than another child element
312314
// we also add a separator before any new child
313315
// if the newChild Element is an input or img type, delete the children key/value pair
@@ -350,6 +352,14 @@ const reducer = (state: State, action: Action) => {
350352
state.HTMLTypes
351353
);
352354

355+
// TODO: when newChild is a Switch, call addRoute to create a Route child
356+
// passing in newChild
357+
358+
// if (typeId === 17 && !addedInitialRoute) {
359+
// addRoute(newChild);
360+
// addedInitialRoute = true;
361+
// }
362+
353363
return {
354364
...state,
355365
components,

0 commit comments

Comments
 (0)