11import produce from 'immer' ;
2- import { InputMessageTypes } from 'pods/messages' ;
32import { RoomInfo , UserSession } from 'dals'
43
54let userCollectionSession : UserSession [ ] = [ ] ;
@@ -8,13 +7,13 @@ let roomCollectionSession: RoomInfo[] = [];
87export const isRoomAvailable = async ( room : string ) : Promise < boolean > =>
98 ! userCollectionSession . find ( ( session ) => session . room === room ) ;
109
11- export const saveRoomInfo = async ( newRoomInfo : RoomInfo , action : string ) : Promise < void > => {
10+ export const saveRoomInfo = async ( newRoomInfo : RoomInfo , setFullText : boolean ) : Promise < void > => {
1211 const roomIndex : number = await getRoomIndexByName ( newRoomInfo ) ;
1312 const content : string = newRoomInfo . content ;
14- roomCollectionSession =
13+ roomCollectionSession =
1514 roomIndex === - 1
1615 ? await insertRoom ( newRoomInfo )
17- : await updateRoomContent ( roomIndex , content , action ) ;
16+ : await updateRoomContent ( roomIndex , content , setFullText ) ;
1817} ;
1918
2019export const getRoomContent = async ( room : string ) : Promise < string > => {
@@ -31,21 +30,23 @@ const getRoomIndexByName = async (newRoomInfo: RoomInfo): Promise<number> => {
3130 ) ;
3231} ;
3332
34- const updateRoomContent = async ( index : number , content : string , action : string ) : Promise < RoomInfo [ ] > => {
35- return produce ( roomCollectionSession , ( draftState : RoomInfo [ ] ) => {
36- switch ( action ) {
37- case InputMessageTypes . TRAINER_APPEND_TEXT :
38- draftState [ index ] . content += '\n' + content ;
39- break ;
40- case InputMessageTypes . TRAINER_SET_FULL_TEXT :
41- draftState [ index ] . content = content ;
42- break ;
43- } ;
33+ const updateRoomContent = async ( index : number , content : string , setFullText : boolean ) : Promise < RoomInfo [ ] > => {
34+ if ( ! Boolean ( content ) )
35+ return roomCollectionSession ;
36+
37+ const newRoomCollectionSession = produce ( roomCollectionSession , ( draftState : RoomInfo [ ] ) => {
38+ const someCurrentContent : boolean = Boolean ( draftState [ index ] . content ) ;
39+ if ( someCurrentContent && ! setFullText ) {
40+ draftState [ index ] . content += '\n' ;
41+ }
42+ draftState [ index ] . content = setFullText ? content : ( draftState [ index ] . content + content ) ;
4443 } ) ;
44+
45+ return newRoomCollectionSession ;
4546} ;
4647
4748const insertRoom = async ( newRoomInfo : RoomInfo ) : Promise < RoomInfo [ ] > => {
4849 return produce ( roomCollectionSession , ( draftState : RoomInfo [ ] ) => {
4950 draftState . push ( newRoomInfo ) ;
5051 } ) ;
51- } ;
52+ } ;
0 commit comments