ucap-doc/documents/업무/6월/1째주/backup/chat/reducers.ts

254 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-06-05 09:45:18 +00:00
import { createReducer, on } from '@ngrx/store';
import {
initialState,
adapterChatting,
adapterEventList,
Chatting,
adapterFileInfoList,
adapterFileInfoCheckList
} from './state';
import * as RoomActions from '../room/actions';
import {
eventsSuccess,
eventsFailure,
fileInfosSuccess,
fileInfosFailure,
addEvent,
delEventList,
updateEventList
} from './actions';
import { Info, EventJson, EventType } from '@ucap/protocol-event';
export const reducer = createReducer(
initialState,
on(eventsSuccess, (state, action) => {
const roomId = action.res.roomId;
const chatting = state.chattings.entities[roomId] || {};
let trgtChatting: Chatting = {
roomId,
eventListProcessing: false,
eventList: adapterEventList.getInitialState(),
eventStatus: null,
remainEvent: false,
fileInfoListProcessing: false,
fileInfoList: adapterFileInfoList.getInitialState(),
fileInfoCheckList: adapterFileInfoCheckList.getInitialState(),
fileInfoSyncDate: '',
...chatting
};
trgtChatting = {
...trgtChatting,
eventList: adapterEventList.upsertMany(action.eventInfoList, {
...trgtChatting.eventList
}),
eventStatus: action.res,
remainEvent: action.remainEvent,
eventListProcessing: false
};
return {
...state,
chattings: adapterChatting.upsertOne(trgtChatting, {
...state.chattings
})
};
}),
on(eventsFailure, (state, action) => {
const roomId = action.roomId;
const chatting = state.chattings.entities[roomId];
let trgtChatting: Chatting;
if (!!chatting) {
trgtChatting = {
...chatting,
eventListProcessing: false
};
}
return {
...state,
chattings: adapterChatting.upsertOne(trgtChatting, {
...state.chattings
})
};
}),
on(fileInfosSuccess, (state, action) => {
const roomId = action.res?.roomId;
if (!roomId) {
return state;
}
const chatting = state.chattings.entities[roomId] || {};
let trgtChatting: Chatting = {
roomId,
eventListProcessing: false,
eventList: adapterEventList.getInitialState(),
eventStatus: null,
remainEvent: false,
fileInfoListProcessing: false,
fileInfoList: adapterFileInfoList.getInitialState(),
fileInfoCheckList: adapterFileInfoCheckList.getInitialState(),
fileInfoSyncDate: '',
...chatting
};
const fileInfoList = action.fileInfoList;
const fileInfoCheckList = action.fileInfoCheckList;
trgtChatting = {
...trgtChatting,
fileInfoList: !!fileInfoList
? adapterFileInfoList.upsertMany(fileInfoList, {
...trgtChatting.fileInfoList
})
: trgtChatting.fileInfoList,
fileInfoCheckList: !!fileInfoCheckList
? adapterFileInfoCheckList.upsertMany(fileInfoCheckList, {
...trgtChatting.fileInfoCheckList
})
: trgtChatting.fileInfoCheckList,
fileInfoListProcessing: false
};
return {
...state,
chattings: adapterChatting.upsertOne(trgtChatting, {
...state.chattings
})
};
}),
on(fileInfosFailure, (state, action) => {
const roomId = action.roomId;
const chatting = state.chattings.entities[roomId];
let trgtChatting: Chatting;
if (!!chatting) {
trgtChatting = {
...chatting,
fileInfoListProcessing: false
};
}
return {
...state,
chattings: adapterChatting.upsertOne(trgtChatting, {
...state.chattings
})
};
}),
on(addEvent, (state, action) => {
const roomId = action.roomId;
const eventInfo = action.info;
const chatting = state.chattings.entities[roomId];
if (!!chatting) {
return {
...state,
chattings: adapterChatting.upsertOne(
{
...chatting,
eventList: adapterEventList.upsertOne(eventInfo, {
...chatting.eventList
})
},
{ ...state.chattings }
)
};
} else {
return state;
}
}),
/*******************************************************************
* [Room Action watching.]
*******************************************************************/
on(RoomActions.selectedRoomSuccess, (state, action) => {
return {
...state,
activeRoomId: action.roomId
};
}),
on(RoomActions.clearSelectedRoom, (state, action) => {
if (action.roomId === state.activeRoomId) {
return {
...state,
activeRoomId: null
};
} else {
return state;
}
}),
on(RoomActions.close, (state, action) => {
const roomIds = action.roomIds;
return {
...state,
chattings: adapterChatting.removeMany(roomIds, { ...state.chattings })
};
}),
on(delEventList, (state, action) => {
const roomId = action.roomId;
const chatting = state.chattings.entities[roomId];
return {
...state,
chattings: adapterChatting.upsertOne(
{
...chatting,
eventList: adapterEventList.removeMany(action.eventSeqs, {
...chatting.eventList
})
},
{ ...state.chattings }
)
};
}),
on(updateEventList, (state, action) => {
const roomId = action.roomId;
const eventSeq = action.eventSeq;
const sentMessage = action.sentMessage;
const chatting = state.chattings.entities[roomId];
const statusEventInfo: Info<EventJson> = {
...chatting.eventList[eventSeq],
type: EventType.RecalledMessage,
sentMessage
};
return {
...state,
chattings: adapterChatting.upsertOne(
{
...chatting,
eventList: adapterEventList.updateOne(
{ id: eventSeq, changes: statusEventInfo },
{
...chatting.eventList
}
)
},
{ ...state.chattings }
)
};
})
);