Skip to content

Commit a5073c5

Browse files
authored
Merge pull request #71 from Lemoncode/#4-Edit-full-text-plus-replace-only-trainer
#4 edit full text plus replace only trainer
2 parents 84df1e9 + 98bd8bd commit a5073c5

12 files changed

Lines changed: 110 additions & 63 deletions

File tree

‎back/src/messages/consts.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export const OutputMessageTypes = {
1313
CONNECTION_ESTABLISHED_STUDENT: 'CONNECTION_ESTABLISHED_STUDENT',
1414
ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET: 'ERROR_MULTI_TRAINER_NOT_IMPLEMENTED_YET',
1515
APPEND_TEXT: 'APPEND_TEXT',
16-
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
1716
TRAINER_SEND_FULL_CONTENT: 'TRAINER_SEND_FULL_CONTENT',
1817
STUDENT_SEND_FULL_CONTENT: 'STUDENT_SEND_FULL_CONTENT',
18+
UPDATE_FULL_CONTENT: 'UPDATE_FULL_CONTENT',
1919
};
2020

2121
export const SocketMessageTypes = {};

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import {
1515
} from '../storage';
1616
import { processOuputMessage } from './output-processor';
1717

18-
export const processInputMessage = (
19-
socketInfo: SocketInfo,
20-
action: Action
21-
): Action[] => {
18+
export const processInputMessage = (socketInfo: SocketInfo, action: Action): Action[] => {
2219
let outputActionCollection: Action[] = [];
2320
switch (action.type) {
2421
case InputMessageTypes.ESTABLISH_CONNECTION_TRAINER:
@@ -37,15 +34,17 @@ export const processInputMessage = (
3734
);
3835
break;
3936
case InputMessageTypes.TRAINER_APPEND_TEXT:
40-
outputActionCollection = handleTrainerAppendText(
37+
outputActionCollection = handleTrainerSendText(
4138
socketInfo,
42-
action.payload
39+
action.payload,
40+
InputMessageTypes.TRAINER_APPEND_TEXT
4341
);
4442
break;
4543
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
46-
outputActionCollection = handleSetTrainerFullText(
44+
outputActionCollection = handleTrainerSendText(
4745
socketInfo,
48-
action.payload
46+
action.payload,
47+
InputMessageTypes.TRAINER_SET_FULL_TEXT
4948
);
5049
break;
5150
case InputMessageTypes.STUDENT_REQUEST_FULL_CONTENT:
@@ -69,24 +68,21 @@ const handleRequestGetTrainerContent = (socketInfo: SocketInfo) => {
6968
return [{ type: OutputMessageTypes.TRAINER_SEND_FULL_CONTENT }];
7069
};
7170

72-
const handleSetTrainerFullText = (socketInfo: SocketInfo, text: string) => {
71+
const handleTrainerSendText = (socketInfo: SocketInfo, text: string, action: string) => {
7372
if (!isTrainerUser(socketInfo.connectionId)) {
7473
return [];
7574
}
76-
77-
return [{ type: OutputMessageTypes.REPLACE_FULL_TEXT, payload: text }];
78-
};
79-
80-
const handleTrainerAppendText = (socketInfo: SocketInfo, text: string) => {
81-
if (!isTrainerUser(socketInfo.connectionId)) {
82-
return [];
83-
}
84-
8575
const room = getRoomFromConnectionId(socketInfo.connectionId);
8676
const roomInfo = { room, content: text };
87-
saveRoomInfo(roomInfo);
88-
89-
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
77+
saveRoomInfo(roomInfo, action);
78+
switch(action){
79+
case InputMessageTypes.TRAINER_APPEND_TEXT:
80+
return [{ type: OutputMessageTypes.APPEND_TEXT, payload: text }];
81+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
82+
return [{ type: OutputMessageTypes.UPDATE_FULL_CONTENT}];
83+
default:
84+
return [];
85+
};
9086
};
9187

9288
const handleEstablishConnectionStudent = (

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export const processOuputMessage = (socketInfo: SocketInfo, action: Action) => {
4040
case OutputMessageTypes.APPEND_TEXT:
4141
handleAppendText(socketInfo, action.payload);
4242
break;
43-
case OutputMessageTypes.REPLACE_FULL_TEXT:
44-
handleReplaceFullText(socketInfo, action.payload);
43+
case OutputMessageTypes.UPDATE_FULL_CONTENT:
44+
handleUpdateFullContent(socketInfo);
4545
break;
4646
case OutputMessageTypes.STUDENT_SEND_FULL_CONTENT:
4747
handleSingleStudentSendFullContent(socketInfo);
@@ -55,34 +55,23 @@ export const processOuputMessage = (socketInfo: SocketInfo, action: Action) => {
5555
};
5656

5757
const handleSingleStudentSendFullContent = (socketInfo: SocketInfo) => {
58-
handleSendFullContent(socketInfo, responseType.STUDENT_GET_FULL_CONTENT);
58+
handleSendFullContent(socketInfo, responseType.STUDENT_GET_FULL_CONTENT, false);
5959
}
6060

6161
const handleTrainerSendFullContent = (socketInfo: SocketInfo) => {
62-
handleSendFullContent(socketInfo, responseType.TRAINER_GET_FULL_CONTENT);
62+
handleSendFullContent(socketInfo, responseType.TRAINER_GET_FULL_CONTENT, false);
6363
}
6464

65-
const handleSendFullContent = (socketInfo: SocketInfo, responseType: string) => {
66-
const room = getRoomFromConnectionId(socketInfo.connectionId);
67-
const content = getRoomContent(room);
68-
socketInfo.socket.emit(SocketOuputMessageLiteral.MESSAGE, {
69-
type: responseType,
70-
payload: content,
71-
});
72-
};
73-
74-
65+
const handleUpdateFullContent = (socketInfo: SocketInfo) => {
66+
handleSendFullContent(socketInfo, responseType.UPDATE_FULL_CONTENT, true);
67+
}
7568

76-
// TODO: replace with handleSingleStudentSendFullContent code
77-
// but instead of using socketInfo.socket.emit use
78-
// socketInfo.io.in(room)
79-
const handleReplaceFullText = (socketInfo: SocketInfo, text: string) => {
69+
const handleSendFullContent = (socketInfo: SocketInfo, responseType: string, sendToAll: boolean) => {
8070
const room = getRoomFromConnectionId(socketInfo.connectionId);
81-
socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, {
82-
// TODO: remove from const list REPLACE_FULL_TEXT
83-
type: responseType.REPLACE_FULL_TEXT, //STUDENT_GET_FULL_CONTENT
84-
payload: text,
85-
});
71+
const content = getRoomContent(room);
72+
const msg = {type: responseType, payload: content};
73+
sendToAll ? socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, msg) :
74+
socketInfo.socket.emit(SocketOuputMessageLiteral.MESSAGE, msg);
8675
};
8776

8877
const handleAppendText = (socketInfo: SocketInfo, text: string) => {

‎back/src/messages/response.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export const responseType = {
22
CONNECTION_ACK: 'CONNECTION_ACK',
33
APPEND_TEXT: 'APPEND_TEXT',
4-
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
54
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
65
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
6+
UPDATE_FULL_CONTENT: 'UPDATE_FULL_CONTENT'
77
};
88

99
export interface ResponseBase {

‎back/src/storage/session.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This is just a demo approach, storing in memory session Info
22
// Another way to identify users: https://stackoverflow.com/questions/6979992/how-to-get-session-id-of-socket-io-client-in-client
33
import produce from 'immer';
4+
import { InputMessageTypes } from '../messages';
45

56
interface ConnectSessionInfo {
67
room: string;
@@ -38,14 +39,13 @@ export const addNewUser = (
3839
];
3940
};
4041

41-
export const saveRoomInfo = (newRoomInfo: RoomInfo) => {
42+
export const saveRoomInfo = (newRoomInfo: RoomInfo, action: string) => {
4243
const roomIndex: number = getRoomIndexByName(newRoomInfo);
4344
const content: string = newRoomInfo.content;
44-
4545
roomCollectionSession =
4646
roomIndex === -1
4747
? insertRoom(newRoomInfo)
48-
: updateRoomContent(roomIndex, content);
48+
: updateRoomContent(roomIndex, content, action);
4949
};
5050

5151
export const getRoomContent = (room: string) => {
@@ -81,9 +81,16 @@ export const getRoomIndexByName = (newRoomInfo: RoomInfo): number => {
8181
);
8282
};
8383

84-
export const updateRoomContent = (index: number, content: string) => {
84+
export const updateRoomContent = (index: number, content: string, action:string) => {
8585
return produce(roomCollectionSession, (draftState: RoomInfo[]) => {
86-
draftState[index].content += '\n' + content;
86+
switch (action) {
87+
case InputMessageTypes.TRAINER_APPEND_TEXT:
88+
draftState[index].content += '\n' + content;
89+
break;
90+
case InputMessageTypes.TRAINER_SET_FULL_TEXT:
91+
draftState[index].content = content;
92+
break;
93+
};
8794
});
8895
};
8996

‎front/src/core/const.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ export const SocketOuputMessageLiteral = {
77

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

1415
export const SocketReceiveMessageTypes = {
1516
CONNECTION_ACK: 'CONNECTION_ACK',
1617
APPEND_TEXT: 'APPEND_TEXT',
17-
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
1818
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
1919
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
20+
UPDATE_FULL_CONTENT: 'UPDATE_FULL_CONTENT',
2021
};
2122

2223
export const lineSeparator = '\n\n*********************************\n';

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ export const PlayerContainer = () => {
3838
appendToLog(payload);
3939
break;
4040
case SocketReceiveMessageTypes.STUDENT_GET_FULL_CONTENT:
41+
case SocketReceiveMessageTypes.UPDATE_FULL_CONTENT:
4142
setLog(payload);
4243
break;
44+
default:
45+
break;
4346
}
4447
}
4548
});

‎front/src/pods/trainer/components/new-text.component.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Button from '@material-ui/core/Button';
55
import TextareaAutosize from '@material-ui/core/TextareaAutosize';
66

77
interface Props {
8-
handleAppendTrainerText: (trainerText: string) => void;
8+
handleAppendTrainerText: (trainerText: string,) => void;
99
}
1010

1111
export const NewTextComponent: React.FC<Props> = props => {

‎front/src/pods/trainer/components/session.component.tsx‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,35 @@ import React from 'react';
22
import * as classes from './session.styles';
33
// Material UI ~ components
44
import TextareaAutosize from '@material-ui/core/TextareaAutosize';
5+
import Button from '@material-ui/core/Button';
56

67
interface Props {
78
log: string;
9+
handleSendFullContentLog: (fullContent: string) => void;
810
}
911

12+
const getTextArea = (elementId: string): HTMLInputElement =>
13+
document.getElementById('session') as HTMLInputElement;
14+
15+
const handleSetSessionContent = (sessionContent: string) => {
16+
const sessionTextArea: HTMLInputElement = getTextArea('session');
17+
sessionTextArea ? (sessionTextArea.value = sessionContent) : undefined;
18+
};
19+
20+
const getFullContent = (currenSessionContent: string) => {
21+
const sessionTextArea: HTMLInputElement = getTextArea('session');
22+
return sessionTextArea && sessionTextArea.value != currenSessionContent
23+
? sessionTextArea.value
24+
: undefined;
25+
};
26+
1027
export const SessionComponent: React.FC<Props> = props => {
11-
const { log } = props;
12-
const { labelTextarea, studentBoard } = classes;
28+
const { log, handleSendFullContentLog } = props;
29+
const { labelTextarea, studentBoard, sendBtn, undoBtn } = classes;
30+
31+
React.useEffect(() => {
32+
handleSetSessionContent(log);
33+
}, [log]);
1334

1435
return (
1536
<div>
@@ -21,10 +42,23 @@ export const SessionComponent: React.FC<Props> = props => {
2142
rowsMax={20}
2243
rowsMin={20}
2344
className={studentBoard}
24-
value={log}
25-
read-only="true"
26-
aria-readonly
2745
/>
46+
<Button
47+
variant="contained"
48+
color="secondary"
49+
className={undoBtn}
50+
onClick={() => handleSetSessionContent(log)}
51+
>
52+
Undo
53+
</Button>
54+
<Button
55+
variant="contained"
56+
color="primary"
57+
className={sendBtn}
58+
onClick={() => handleSendFullContentLog(getFullContent(log))}
59+
>
60+
Send Full Content
61+
</Button>
2862
</div>
2963
);
3064
};

‎front/src/pods/trainer/components/session.styles.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ export const studentBoard = css`
2121
export const labelTextarea = css`
2222
font-family: ${typography.fontFamily};
2323
`;
24+
25+
export const sendBtn = css`
26+
width: 50%;
27+
`;
28+
29+
export const undoBtn = css`
30+
width: 50%;
31+
color: red;
32+
`;

0 commit comments

Comments
 (0)