Skip to content

Commit 91fd4e7

Browse files
authored
Merge pull request #9 from oslabs-beta/Lauren
Added events for position, delete, focus, and add
2 parents 6f97912 + dcde6de commit 91fd4e7

File tree

9 files changed

+162
-3
lines changed

9 files changed

+162
-3
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { RootState } from '../../redux/store';
1010
import TextField from '@mui/material/TextField';
1111
import {
1212
allCooperativeState,
13-
addChild
13+
addChild,
14+
changeFocus,
15+
deleteChild,
16+
changePosition
1417
} from '../../redux/reducers/slice/appStateSlice';
1518
import {
1619
setRoomCode,
@@ -76,13 +79,35 @@ const RoomsContainer = () => {
7679

7780
// update user list when there's a change: new join or leave the room
7881
socket.on('updateUserList', (newUserList) => {
82+
console.log('user list received from server');
7983
dispatch(setUserList(newUserList));
8084
});
8185

8286
socket.on('child data from server', (childData: object) => {
8387
console.log('child data received by users', childData);
8488
store.dispatch(addChild(childData));
8589
});
90+
91+
socket.on('focus data from server', (focusData: object) => {
92+
console.log('focus data received from server', focusData);
93+
store.dispatch(changeFocus(focusData));
94+
});
95+
96+
socket.on('delete data from server', (deleteData: object) => {
97+
console.log('delete data received from server', deleteData);
98+
store.dispatch(deleteChild(deleteData));
99+
});
100+
101+
socket.on(
102+
'item position data from server',
103+
(itemPositionData: object) => {
104+
console.log(
105+
'item position data received from server',
106+
itemPositionData
107+
);
108+
store.dispatch(changePosition(itemPositionData));
109+
}
110+
);
86111
}
87112
}
88113

app/src/components/main/Canvas.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ function Canvas(props: {}): JSX.Element {
3939
childId?: number | null
4040
) => {
4141
dispatch(changeFocus({ componentId, childId }));
42+
//if room exists, send focus dispatcht to all users
43+
if (roomCode) {
44+
emitEvent('changeFocusAction', roomCode, {
45+
componentId: componentId,
46+
childId: childId
47+
});
48+
}
49+
console.log('emit changeFocusAction event is triggered in canvas');
4250
};
4351

4452
// onClickHandler is responsible for changing the focused component and child component
@@ -98,7 +106,7 @@ function Canvas(props: {}): JSX.Element {
98106
let hasDiffParent = false;
99107
const components = state.components;
100108
let newChildName = '';
101-
// loop over componenets array
109+
// loop over components array
102110
for (let i = 0; i < components.length; i++) {
103111
const comp = components[i];
104112
//loop over each componenets child
@@ -133,6 +141,16 @@ function Canvas(props: {}): JSX.Element {
133141
contextParam: contextParam
134142
})
135143
);
144+
if (roomCode) {
145+
emitEvent('addChildAction', roomCode, {
146+
type: item.instanceType,
147+
typeId: item.instanceTypeId,
148+
childId: null,
149+
contextParam: contextParam
150+
});
151+
152+
console.log('emit addChildAction event is triggered in canvas');
153+
}
136154
}
137155
},
138156
collect: (monitor) => ({

app/src/components/main/DeleteButton.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ import { DeleteButtons } from '../../interfaces/Interfaces';
33
import { useDispatch, useSelector } from 'react-redux';
44
import { deleteChild } from '../../redux/reducers/slice/appStateSlice';
55
import { RootState } from '../../redux/store';
6+
import { emitEvent } from '../../helperFunctions/socket';
67

78
function DeleteButton({ id, name }: DeleteButtons) {
89
const contextParam = useSelector((store: RootState) => store.contextSlice);
910

11+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
12+
1013
const dispatch = useDispatch();
1114

1215
const deleteHTMLtype = (id: number) => {
1316
dispatch(deleteChild({ id: id, contextParam: contextParam }));
17+
if (roomCode) {
18+
emitEvent('deleteChildAction', roomCode, {
19+
id,
20+
contextParam
21+
});
22+
23+
console.log(
24+
'emit deleteChildAction event is triggered in DeleteButton.tsx'
25+
);
26+
}
1427
};
1528

1629
return (

app/src/components/main/DirectChildComponent.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import globalDefaultStyle from '../../public/styles/globalDefaultStyles';
88
import { useDispatch, useSelector } from 'react-redux';
99
import { changeFocus } from '../../redux/reducers/slice/appStateSlice';
1010
import { RootState } from '../../redux/store';
11+
import { emitEvent } from '../../helperFunctions/socket';
1112

1213
function DirectChildComponent({ childId, type, typeId, name }: ChildElement) {
1314
const state = useSelector((store: RootState) => store.appState);
15+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
16+
1417
const dispatch = useDispatch();
1518

1619
// find the top-level component corresponding to this instance of the component
@@ -34,6 +37,13 @@ function DirectChildComponent({ childId, type, typeId, name }: ChildElement) {
3437
});
3538
const changeFocusFunction = (componentId: number, childId: number | null) => {
3639
dispatch(changeFocus({ componentId, childId }));
40+
if (roomCode) {
41+
emitEvent('changeFocusAction', roomCode, {
42+
componentId: componentId,
43+
childId: childId
44+
});
45+
console.log('emit focus event from DirectChildComponent');
46+
}
3747
};
3848

3949
// onClickHandler is responsible for changing the focused component and child component

app/src/components/main/DirectChildHTML.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import DeleteButton from './DeleteButton';
88
import { useDispatch, useSelector } from 'react-redux';
99
import { changeFocus } from '../../redux/reducers/slice/appStateSlice';
1010
import { RootState } from '../../redux/store';
11+
import { emitEvent } from '../../helperFunctions/socket';
1112

1213
function DirectChildHTML({ childId, name, type, typeId, style }: ChildElement) {
1314
const state = useSelector((store: RootState) => store.appState);
15+
16+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
17+
1418
const dispatch = useDispatch();
1519

1620
// find the HTML element corresponding with this instance of an HTML element
@@ -35,6 +39,13 @@ function DirectChildHTML({ childId, name, type, typeId, style }: ChildElement) {
3539

3640
const changeFocusFunction = (componentId: number, childId: number | null) => {
3741
dispatch(changeFocus({ componentId, childId }));
42+
if (roomCode) {
43+
emitEvent('changeFocusAction', roomCode, {
44+
componentId: componentId,
45+
childId: childId
46+
});
47+
console.log('emit focus event from DirectChildHTML');
48+
}
3849
};
3950

4051
// onClickHandler is responsible for changing the focused component and child component

app/src/components/main/DirectChildHTMLNestable.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import AddRoute from './AddRoute';
1212
import AddLink from './AddLink';
1313
import { useDispatch, useSelector } from 'react-redux';
1414
import { RootState } from '../../redux/store';
15+
import { emitEvent } from '../../helperFunctions/socket';
1516

1617
import {
1718
changeFocus,
@@ -33,6 +34,8 @@ function DirectChildHTMLNestable({
3334
const contextParam = useSelector((store: RootState) => store.contextSlice);
3435

3536
const dispatch = useDispatch();
37+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
38+
3639
const ref = useRef(null);
3740

3841
// takes a snapshot of state to be used in UNDO and REDO cases. snapShotFunc is also invoked in Canvas.tsx
@@ -104,6 +107,18 @@ function DirectChildHTMLNestable({
104107
contextParam: contextParam
105108
})
106109
);
110+
if (roomCode) {
111+
emitEvent('addChildAction', roomCode, {
112+
type: item.instanceType,
113+
typeId: item.instanceTypeId,
114+
childId: childId,
115+
contextParam: contextParam
116+
});
117+
118+
console.log(
119+
'emit addChildAction event is triggered in DirectChildHTMLNestable'
120+
);
121+
}
107122
}
108123
}
109124
// if item is not a new instance, change position of element dragged inside div so that the div is the new parent
@@ -117,6 +132,17 @@ function DirectChildHTMLNestable({
117132
contextParam: contextParam
118133
})
119134
);
135+
if (roomCode) {
136+
emitEvent('changePositionAction', roomCode, {
137+
currentChildId: item.childId,
138+
newParentChildId: childId,
139+
contextParam: contextParam
140+
});
141+
142+
console.log(
143+
'emit changePosition event is triggered in DirectChildHTMLNestable'
144+
);
145+
}
120146
}
121147
}
122148
},
@@ -130,6 +156,13 @@ function DirectChildHTMLNestable({
130156

131157
const changeFocusFunction = (componentId: number, childId: number | null) => {
132158
dispatch(changeFocus({ componentId, childId }));
159+
if (roomCode) {
160+
emitEvent('changeFocusAction', roomCode, {
161+
componentId: componentId,
162+
childId: childId
163+
});
164+
console.log('emit focus event from DirectChildHTMLNestable');
165+
}
133166
};
134167

135168
// onClickHandler is responsible for changing the focused component and child component
@@ -185,7 +218,9 @@ function DirectChildHTMLNestable({
185218
id={`canv${childId}`}
186219
>
187220
<span>
188-
<strong style={{ color: '#f2fbf8' }}>{HTMLType.placeHolderShort}</strong>
221+
<strong style={{ color: '#f2fbf8' }}>
222+
{HTMLType.placeHolderShort}
223+
</strong>
189224
<strong style={{ color: '#29a38a' }}>
190225
{attributes && attributes.compLink ? ` ${attributes.compLink}` : ''}
191226
</strong>

app/src/components/main/SeparatorChild.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
changePosition,
1515
addChild
1616
} from '../../redux/reducers/slice/appStateSlice';
17+
import { emitEvent } from '../../helperFunctions/socket';
1718

1819
function DirectChildHTMLNestable({
1920
childId,
@@ -25,6 +26,8 @@ function DirectChildHTMLNestable({
2526
const state = useSelector((store: RootState) => store.appState);
2627
const contextParam = useSelector((store: RootState) => store.contextSlice);
2728

29+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
30+
2831
const dispatch = useDispatch();
2932
const ref = useRef(null);
3033

@@ -80,6 +83,18 @@ function DirectChildHTMLNestable({
8083
contextParam: contextParam
8184
})
8285
);
86+
if (roomCode) {
87+
emitEvent('addChildAction', roomCode, {
88+
type: item.instanceType,
89+
typeId: item.instanceTypeId,
90+
childId: childId,
91+
contextParam: contextParam
92+
});
93+
94+
console.log(
95+
'emit addChildAction event is triggered in SeparatorChild'
96+
);
97+
}
8398
}
8499
}
85100
// if item is not a new instance, change position of element dragged inside separator so that separator is new parent (until replacement)
@@ -93,6 +108,17 @@ function DirectChildHTMLNestable({
93108
contextParam: contextParam
94109
})
95110
);
111+
if (roomCode) {
112+
emitEvent('changePositionAction', roomCode, {
113+
currentChildId: item.childId,
114+
newParentChildId: childId,
115+
contextParam: contextParam
116+
});
117+
118+
console.log(
119+
'emit changePosition event is triggered in SeparatorChild'
120+
);
121+
}
96122
}
97123
}
98124
},

app/src/redux/reducers/slice/userSlice.ts

Whitespace-only changes.

server/server.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ io.on('connection', (client) => {
169169
client.to(roomCode).emit('child data from server', childData);
170170
}
171171
});
172+
173+
client.on('changeFocusAction', (roomCode: string, focusData: object) => {
174+
//console.log('focus data received on server:', focusData);
175+
if (roomCode) {
176+
//server send the focus data to everyone in room
177+
client.to(roomCode).emit('focus data from server', focusData);
178+
}
179+
});
180+
181+
client.on('deleteChildAction', (roomCode: string, deleteData: object) => {
182+
client.to(roomCode).emit('delete data from server', deleteData);
183+
});
184+
185+
client.on(
186+
'changePositionAction',
187+
(roomCode: string, itemPositionData: object) => {
188+
client
189+
.to(roomCode)
190+
.emit('item position data from server', itemPositionData);
191+
}
192+
);
172193
});
173194

174195
//--------------------------------

0 commit comments

Comments
 (0)