Skip to content

Commit ef1879f

Browse files
committed
Issue #53 solved
1 parent b736516 commit ef1879f

9 files changed

Lines changed: 62 additions & 25 deletions

File tree

‎back/src/messages/consts.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ export const InputMessageTypes = {
44
TRAINER_APPEND_TEXT: 'TRAINER_APPEND_TEXT',
55
TRAINER_SET_FULL_TEXT: 'TRAINER_SET_FULL_TEXT',
66
TRAINER_SAVE_CONTENT: 'TRAINER_SAVE_CONTENT',
7+
TRAINER_REQUEST_FULL_CONTENT: 'TRAINER_REQUEST_FULL_CONTENT',
78
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT',
89
};
910

1011
export const OutputMessageTypes = {
1112
CONNECTION_ESTABLISHED_TRAINER: 'CONNECTION_ESTABLISHED_TRAINER',
1213
CONNECTION_ESTABLISHED_STUDENT: 'CONNECTION_ESTABLISHED_STUDENT',
13-
ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET:
14-
'ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET',
14+
ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET: 'ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET',
1515
APPEND_TEXT: 'APPEND_TEXT',
1616
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
17+
TRAINER_SEND_FULL_CONTENT: 'TRAINER_SEND_FULL_CONTENT',
1718
STUDENT_SEND_FULL_CONTENT: 'STUDENT_SEND_FULL_CONTENT',
1819
};
1920

‎back/src/messages/input-processor.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export const processInputMessage = (
5151
case InputMessageTypes.STUDENT_REQUEST_FULL_CONTENT:
5252
outputActionCollection = handleRequestGetStudentContent(socketInfo);
5353
break;
54+
case InputMessageTypes.TRAINER_REQUEST_FULL_CONTENT:
55+
outputActionCollection = handleRequestGetTrainerContent(socketInfo);
56+
break;
57+
default:
58+
break;
5459
}
5560

5661
return outputActionCollection;
@@ -60,6 +65,10 @@ const handleRequestGetStudentContent = (socketInfo: SocketInfo) => {
6065
return [{ type: OutputMessageTypes.STUDENT_SEND_FULL_CONTENT }];
6166
};
6267

68+
const handleRequestGetTrainerContent = (socketInfo: SocketInfo) => {
69+
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
70+
};
71+
6372
const handleSetTrainerFullText = (socketInfo: SocketInfo, text: string) => {
6473
if (!isTrainerUser(socketInfo.connectionId)) {
6574
return [];

‎back/src/messages/output-processor.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,48 @@ export const processOuputMessage = (socketInfo: SocketInfo, action: Action) => {
3939
break;
4040
case OutputMessageTypes.APPEND_TEXT:
4141
handleAppendText(socketInfo, action.payload);
42+
break;
4243
case OutputMessageTypes.REPLACE_FULL_TEXT:
4344
handleReplaceFullText(socketInfo, action.payload);
45+
break;
4446
case OutputMessageTypes.STUDENT_SEND_FULL_CONTENT:
45-
handleStudentSendContent(socketInfo);
47+
handleSingleStudentSendFullContent(socketInfo);
48+
break;
49+
case OutputMessageTypes.TRAINER_SEND_FULL_CONTENT:
50+
handleTrainerSendFullContent(socketInfo);
51+
break;
52+
default:
53+
break;
4654
}
4755
};
4856

49-
const handleStudentSendContent = (socketInfo: SocketInfo) => {
57+
const handleSingleStudentSendFullContent = (socketInfo: SocketInfo) => {
58+
handleSendFullContent(socketInfo, responseType.STUDENT_GET_FULL_CONTENT);
59+
}
60+
61+
const handleTrainerSendFullContent = (socketInfo: SocketInfo) => {
62+
handleSendFullContent(socketInfo, responseType.TRAINER_GET_FULL_CONTENT);
63+
}
64+
65+
const handleSendFullContent = (socketInfo: SocketInfo, responseType: string) => {
5066
const room = getRoomFromConnectionId(socketInfo.connectionId);
5167
const content = getRoomContent(room);
52-
5368
socketInfo.socket.emit(SocketOuputMessageLiteral.MESSAGE, {
54-
type: responseType.STUDENT_GET_FULL_CONTENT,
69+
type: responseType,
5570
payload: content,
5671
});
5772
};
5873

74+
75+
76+
// TODO: replace with handleSingleStudentSendFullContent code
77+
// but instead of using socketInfo.socket.emit use
78+
// socketInfo.io.in(room)
5979
const handleReplaceFullText = (socketInfo: SocketInfo, text: string) => {
6080
const room = getRoomFromConnectionId(socketInfo.connectionId);
6181
socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, {
62-
type: responseType.REPLACE_FULL_TEXT,
82+
// TODO: remove from const list REPLACE_FULL_TEXT
83+
type: responseType.REPLACE_FULL_TEXT, //STUDENT_GET_FULL_CONTENT
6384
payload: text,
6485
});
6586
};

‎back/src/messages/response.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const responseType = {
33
APPEND_TEXT: 'APPEND_TEXT',
44
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
55
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
6+
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
67
};
78

89
export interface ResponseBase {

‎front/src/common-app/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './session-content';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { SocketOuputMessageLiteral, SocketEmitMessageTypes } from 'core';
2+
3+
export const getPreviousSessionContent = (socketRef: React.MutableRefObject<SocketIO.Socket>, messageType: string) => {
4+
socketRef.current.emit(SocketOuputMessageLiteral.MESSAGE, {
5+
type: messageType,
6+
});
7+
};

‎front/src/core/const.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ export const SocketOuputMessageLiteral = {
44

55
export const SocketEmitMessageTypes = {
66
TRAINER_APPEND_TEXT: 'TRAINER_APPEND_TEXT', // Master creates a user story
7-
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT', // Student checks if there's previous content when connecting
7+
TRAINER_REQUEST_FULL_CONTENT: 'TRAINER_REQUEST_FULL_CONTENT', // Trainer checks if there's previous session content when connecting
8+
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT', // Student checks if there's previous session content when connecting
89
};
910

1011
export const SocketReceiveMessageTypes = {
1112
CONNECTION_ACK: 'CONNECTION_ACK',
1213
APPEND_TEXT: 'APPEND_TEXT',
1314
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
15+
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
1416
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
1517
};
1618

‎front/src/pods/student/student.container.tsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { useLog } from 'core';
1010
import { StudentComponent } from './student.component';
1111
import { useWithRef } from 'common';
12+
import { getPreviousSessionContent } from 'common-app';
1213

1314
interface Params {
1415
room: string;
@@ -44,15 +45,9 @@ export const PlayerContainer = () => {
4445
});
4546
};
4647

47-
const getPreviousSessionContent = () => {
48-
socketRef.current.emit(SocketOuputMessageLiteral.MESSAGE, {
49-
type: SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT,
50-
});
51-
};
52-
5348
React.useEffect(() => {
5449
handleConnection();
55-
getPreviousSessionContent();
50+
getPreviousSessionContent(socketRef, SocketEmitMessageTypes.STUDENT_REQUEST_FULL_CONTENT);
5651
}, []);
5752

5853
return (

‎front/src/pods/trainer/trainer.container.tsx‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { useLog } from 'core';
1212
import { TrainerComponent } from './trainer.component';
1313
import { useWithRef, getHostBaseUrl } from 'common';
14+
import { getPreviousSessionContent } from 'common-app';
1415

1516
interface Params {
1617
token: string;
@@ -20,28 +21,26 @@ interface Params {
2021
export const TrainerContainer = () => {
2122
const { token, room } = useParams<Params>();
2223
const { log, appendToLog, logRef } = useLog();
23-
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(null);
24+
const socketDetails = { room: room, trainertoken: token };
25+
const [socket, setSocket, socketRef] = useWithRef<SocketIO.Socket>(createSocket(socketDetails));
2426

2527
const [currentTrainerUrl, setCurrentTrainerUrl] = React.useState<string>('');
2628
const [currentStudentUrl, setCurrentStudentUrl] = React.useState<string>('');
2729

2830
const handleConnection = () => {
29-
// Connect to socket
30-
const localSocket = createSocket({
31-
room: room,
32-
trainertoken: token,
33-
});
34-
35-
setSocket(localSocket);
36-
37-
localSocket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
31+
socket.on(SocketOuputMessageLiteral.MESSAGE, msg => {
3832
if (msg.type) {
3933
const { type, payload } = msg;
4034

4135
switch (type) {
4236
case SocketReceiveMessageTypes.APPEND_TEXT:
4337
appendToLog(payload);
4438
break;
39+
case SocketReceiveMessageTypes.TRAINER_GET_FULL_CONTENT:
40+
appendToLog(payload);
41+
break;
42+
default:
43+
break;
4544
}
4645
}
4746
});
@@ -51,6 +50,7 @@ export const TrainerContainer = () => {
5150
setCurrentTrainerUrl(`${getHostBaseUrl()}#${routes.trainer(room, token)}`);
5251
setCurrentStudentUrl(`${getHostBaseUrl()}#${routes.student(room)}`);
5352
handleConnection();
53+
getPreviousSessionContent(socketRef, SocketEmitMessageTypes.TRAINER_REQUEST_FULL_CONTENT);
5454
}, []);
5555

5656
const appendLineSeparator = (text: string): string =>

0 commit comments

Comments
 (0)