Skip to content

Commit 7934ee1

Browse files
authored
Merge pull request #68 from Lemoncode/#53-If-a-trainer-refresh-F5-it-should-rerequest-the-content-from-the-server
Issue #53 solved
2 parents 97257b9 + ff9a9cf commit 7934ee1

11 files changed

Lines changed: 90 additions & 28 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
@@ -7,13 +7,15 @@ export const SocketOuputMessageLiteral = {
77

88
export const SocketEmitMessageTypes = {
99
TRAINER_APPEND_TEXT: 'TRAINER_APPEND_TEXT', // Master creates a user story
10-
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT', // Student checks if there's previous content when connecting
10+
TRAINER_REQUEST_FULL_CONTENT: 'TRAINER_REQUEST_FULL_CONTENT', // Trainer checks if there's previous session content when connecting
11+
STUDENT_REQUEST_FULL_CONTENT: 'STUDENT_REQUEST_FULL_CONTENT', // Student checks if there's previous session content when connecting
1112
};
1213

1314
export const SocketReceiveMessageTypes = {
1415
CONNECTION_ACK: 'CONNECTION_ACK',
1516
APPEND_TEXT: 'APPEND_TEXT',
1617
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
18+
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
1719
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
1820
};
1921

‎front/src/core/use-log.hook.spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,30 @@ describe('use-log hook spec', () => {
123123
expect(result.current.log).toEqual<string>(expectedResult);
124124
expect(result.current.logRef.current).toEqual<string>(expectedResult);
125125
});
126+
127+
it('"log" & "logRef.current" should return the same text after calling "setLog" with that text', () => {
128+
// Arrange
129+
const sessionLog: string = `first line
130+
131+
*********************************
132+
133+
second line
134+
135+
*********************************
136+
137+
third line
138+
139+
*********************************`;
140+
141+
// Act
142+
const { result } = renderHook(() => useLog());
143+
144+
act(() => {
145+
result.current.setLog(sessionLog);
146+
});
147+
148+
// Assert
149+
expect(result.current.log).toEqual<string>(sessionLog);
150+
expect(result.current.logRef.current).toEqual<string>(sessionLog);
151+
});
126152
});

‎front/src/core/use-log.hook.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export const useLog = () => {
88
const newText = logRef.current
99
? `${logRef.current}\n${value} `
1010
: `${value} `;
11-
internalSetLog(newText);
12-
logRef.current = newText;
11+
setLog(newText);
1312
};
1413

1514
const setLog = (content: string) => {

‎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 (

0 commit comments

Comments
 (0)