2019-12-23 15:23:27 +09:00

151 lines
3.4 KiB
TypeScript

import { createReducer, on } from '@ngrx/store';
import {
initialState,
adapterInfoList,
adapterFileInfoList,
adapterFileInfoCheckList
} from './state';
import {
infoSuccess,
appendInfoList,
info,
infoFailure,
recallInfoList,
delInfoList,
infoMoreSuccess,
fileInfoSuccess,
infoForSearch,
infoForSearchEnd
} from './actions';
import * as AuthenticationStore from '@app/store/account/authentication';
import * as ChatStore from '@app/store/messenger/chat';
import { Info, EventType, EventJson } from '@ucap-webmessenger/protocol-event';
export const reducer = createReducer(
initialState,
on(info, (state, action) => {
return {
...state,
infoListProcessing: true
};
}),
on(infoForSearch, (state, action) => {
return {
...state,
infoListProcessing: true,
infoSearchListProcessing: true
};
}),
on(infoForSearchEnd, (state, action) => {
return {
...state,
infoSearchListProcessing: false
};
}),
on(infoSuccess, (state, action) => {
return {
...state,
infoList: adapterInfoList.addAll(action.infoList, {
...state.infoList
}),
infoStatus: action.res,
infoListProcessing: false,
remainInfo: action.remainInfo
};
}),
on(infoMoreSuccess, (state, action) => {
return {
...state,
infoList: adapterInfoList.upsertMany(action.infoList, {
...state.infoList
}),
infoStatus: action.res,
infoListProcessing: false,
remainInfo: action.remainInfo
};
}),
on(infoFailure, (state, action) => {
return {
...state,
infoListProcessing: false
};
}),
on(fileInfoSuccess, (state, action) => {
return {
...state,
fileInfoList: adapterFileInfoList.addAll(action.fileInfoList, {
...state.fileInfoList
}),
fileInfoCheckList: adapterFileInfoCheckList.addAll(
action.fileInfoCheckList,
{
...state.fileInfoCheckList
}
),
fileInfoListProcessing: false,
fileInfoSyncDate: new Date().toString()
};
}),
on(appendInfoList, (state, action) => {
const eventinfo = action.info;
const statusEventInfo: Info<EventJson> = {
...state.infoList.entities[eventinfo.seq],
type: eventinfo.type,
senderSeq: eventinfo.senderSeq,
sendDate: eventinfo.sendDate,
sentMessage: eventinfo.sentMessage,
receiverCount: eventinfo.receiverCount
};
return {
...state,
infoList: adapterInfoList.upsertOne(eventinfo, {
...state.infoList
})
};
}),
on(recallInfoList, (state, action) => {
const eventSeq = action.eventSeq;
const statusEventInfo: Info<EventJson> = {
...state.infoList.entities[eventSeq],
type: EventType.RecalledMessage,
sentMessage: '회수된 메시지'
};
return {
...state,
infoList: adapterInfoList.updateOne(
{ id: eventSeq, changes: statusEventInfo },
{ ...state.infoList }
)
};
}),
on(delInfoList, (state, action) => {
return {
...state,
infoList: adapterInfoList.removeMany(action.eventSeqs, {
...state.infoList
})
};
}),
on(ChatStore.clearSelectedRoom, (state, action) => {
return {
...initialState
};
}),
on(AuthenticationStore.logoutInitialize, (state, action) => {
return {
...initialState
};
})
);