Skip to content

Commit 60705de

Browse files
committed
Issue #4-Edit-full-text-plus-replace-only-trainer solved
1 parent 06ec3e9 commit 60705de

11 files changed

Lines changed: 73 additions & 71 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 & 21 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,32 +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-
};
65+
const handleUpdateFullContent = (socketInfo: SocketInfo) => {
66+
handleSendFullContent(socketInfo, responseType.UPDATE_FULL_CONTENT, true);
67+
}
7368

74-
// TODO: replace with handleSingleStudentSendFullContent code
75-
// but instead of using socketInfo.socket.emit use
76-
// socketInfo.io.in(room)
77-
const handleReplaceFullText = (socketInfo: SocketInfo, text: string) => {
69+
const handleSendFullContent = (socketInfo: SocketInfo, responseType: string, sendToAll: boolean) => {
7870
const room = getRoomFromConnectionId(socketInfo.connectionId);
79-
socketInfo.io.in(room).emit(SocketOuputMessageLiteral.MESSAGE, {
80-
// TODO: remove from const list REPLACE_FULL_TEXT
81-
type: responseType.REPLACE_FULL_TEXT, //STUDENT_GET_FULL_CONTENT
82-
payload: text,
83-
});
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);
8475
};
8576

8677
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
@@ -4,16 +4,17 @@ export const SocketOuputMessageLiteral = {
44

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

1112
export const SocketReceiveMessageTypes = {
1213
CONNECTION_ACK: 'CONNECTION_ACK',
1314
APPEND_TEXT: 'APPEND_TEXT',
14-
REPLACE_FULL_TEXT: 'REPLACE_FULL_TEXT',
1515
TRAINER_GET_FULL_CONTENT: 'TRAINER_GET_FULL_CONTENT',
1616
STUDENT_GET_FULL_CONTENT: 'STUDENT_GET_FULL_CONTENT',
17+
UPDATE_FULL_CONTENT: 'UPDATE_FULL_CONTENT',
1718
};
1819

1920
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ import Button from '@material-ui/core/Button';
66

77
interface Props {
88
log: string;
9+
handleSendFullContentLog: (fullContent: string) => void;
910
}
1011

12+
const getTextArea= (elementId: string): HTMLInputElement => document.getElementById("session") as HTMLInputElement;
13+
1114
const handleSetSessionContent = (sessionContent: string) => {
12-
const sessionTextArea: HTMLInputElement = document.getElementById("session") as HTMLInputElement;
15+
const sessionTextArea: HTMLInputElement = getTextArea("session");
1316
sessionTextArea ? sessionTextArea.value=sessionContent : undefined;
1417
}
1518

19+
const getFullContent = (currenSessionContent: string) => {
20+
const sessionTextArea: HTMLInputElement = getTextArea("session");
21+
return (sessionTextArea && (sessionTextArea.value!=currenSessionContent)) ? sessionTextArea.value : undefined;
22+
}
23+
1624
export const SessionComponent: React.FC<Props> = props => {
17-
const { log } = props;
25+
const { log, handleSendFullContentLog } = props;
1826
const { labelTextarea, studentBoard, sendBtn, undoBtn } = classes;
1927

2028
React.useEffect(()=>{
@@ -47,10 +55,10 @@ export const SessionComponent: React.FC<Props> = props => {
4755
variant="contained"
4856
color="primary"
4957
className={sendBtn}
50-
// onClick={() => handleAppendTrainerTextInternal()}
58+
onClick={() => handleSendFullContentLog(getFullContent(log))}
5159
// disabled={!trainerText}
5260
>
53-
Send
61+
Send Full Content
5462
</Button>
5563
</div>
5664
);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { NewTextComponent } from './components/new-text.component';
55
import { SessionComponent } from './components/session.component';
66

77
interface Props {
8-
handleAppendTrainerText: (trainerText: string) => void;
8+
handleAppendTrainerText: (trainerText: string,) => void;
9+
handleSendFullContentLog: (fullContent: string) => void;
910
currentTrainerUrl: string;
1011
currentStudentUrl: string;
1112
log: string;
@@ -14,6 +15,7 @@ interface Props {
1415
export const TrainerComponent: React.FC<Props> = props => {
1516
const {
1617
handleAppendTrainerText,
18+
handleSendFullContentLog,
1719
currentTrainerUrl,
1820
currentStudentUrl,
1921
log,
@@ -29,7 +31,7 @@ export const TrainerComponent: React.FC<Props> = props => {
2931
currentStudentUrl={currentStudentUrl}
3032
/>
3133
<NewTextComponent handleAppendTrainerText={handleAppendTrainerText} />
32-
<SessionComponent log={log} />
34+
<SessionComponent log={log} handleSendFullContentLog={handleSendFullContentLog}/>
3335
</main>
3436
</>
3537
);

0 commit comments

Comments
 (0)