2019-09-25 17:26:19 +09:00
|
|
|
import { createReducer, on } from '@ngrx/store';
|
2019-10-11 11:58:03 +09:00
|
|
|
import {
|
|
|
|
initialState,
|
|
|
|
adapterBuddy2,
|
|
|
|
adapterGroup2,
|
|
|
|
adapterRoom,
|
|
|
|
adapterRoomUser,
|
|
|
|
adapterRoomUserShort
|
|
|
|
} from './state';
|
2019-10-10 15:44:39 +09:00
|
|
|
import {
|
|
|
|
buddy2Success,
|
|
|
|
group2Success,
|
|
|
|
roomSuccess,
|
2019-10-16 10:21:26 +09:00
|
|
|
updateRoomForNewEventMessage,
|
|
|
|
refreshRoomSuccess
|
2019-10-10 15:44:39 +09:00
|
|
|
} from './actions';
|
2019-10-11 11:58:03 +09:00
|
|
|
import {
|
|
|
|
RoomUserDetailData,
|
|
|
|
RoomUserData
|
|
|
|
} from '@ucap-webmessenger/protocol-sync';
|
2019-10-10 14:50:58 +09:00
|
|
|
|
2019-10-11 13:11:48 +09:00
|
|
|
import * as AuthenticationStore from '@app/store/account/authentication';
|
2019-10-11 15:55:27 +09:00
|
|
|
import * as RoomStore from '@app/store/messenger/room';
|
|
|
|
import { RoomInfo } from '@ucap-webmessenger/protocol-room';
|
2019-10-11 13:11:48 +09:00
|
|
|
|
2019-09-25 17:26:19 +09:00
|
|
|
export const reducer = createReducer(
|
|
|
|
initialState,
|
|
|
|
on(buddy2Success, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-11 11:58:03 +09:00
|
|
|
buddy2: adapterBuddy2.addAll(action.buddyList, {
|
|
|
|
...state.buddy2,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
})
|
2019-09-25 18:08:50 +09:00
|
|
|
};
|
|
|
|
}),
|
2019-10-02 14:34:17 +09:00
|
|
|
|
2019-09-25 18:08:50 +09:00
|
|
|
on(group2Success, (state, action) => {
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-11 11:58:03 +09:00
|
|
|
group2: adapterGroup2.addAll(action.groupList, {
|
|
|
|
...state.group2,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
})
|
2019-09-25 18:08:50 +09:00
|
|
|
};
|
2019-10-02 15:49:25 +09:00
|
|
|
}),
|
|
|
|
|
|
|
|
on(roomSuccess, (state, action) => {
|
2019-10-11 11:58:03 +09:00
|
|
|
const roomUserList: RoomUserDetailData[] = [];
|
|
|
|
const roomUserShortList: RoomUserData[] = [];
|
|
|
|
|
|
|
|
for (const key in action.roomUserInfoMap) {
|
|
|
|
if (action.roomUserInfoMap.hasOwnProperty(key)) {
|
|
|
|
const element = action.roomUserInfoMap[key];
|
|
|
|
if (!!element.userInfoList && 0 < element.userInfoList.length) {
|
|
|
|
roomUserList.push({ roomSeq: key, userInfos: element.userInfoList });
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!!element.userInfoShortList &&
|
|
|
|
0 < element.userInfoShortList.length
|
|
|
|
) {
|
|
|
|
roomUserShortList.push({
|
|
|
|
roomSeq: key,
|
|
|
|
userInfos: element.userInfoShortList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 15:49:25 +09:00
|
|
|
return {
|
|
|
|
...state,
|
2019-10-15 14:40:44 +09:00
|
|
|
room: adapterRoom.upsertMany(action.roomList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.room,
|
|
|
|
syncDate: action.syncDate
|
|
|
|
}),
|
2019-10-15 14:40:44 +09:00
|
|
|
roomUser: adapterRoomUser.upsertMany(roomUserList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.roomUser
|
|
|
|
}),
|
2019-10-15 14:40:44 +09:00
|
|
|
roomUserShort: adapterRoomUserShort.upsertMany(roomUserShortList, {
|
2019-10-11 11:58:03 +09:00
|
|
|
...state.roomUserShort
|
|
|
|
})
|
2019-10-02 15:49:25 +09:00
|
|
|
};
|
2019-10-10 14:50:58 +09:00
|
|
|
}),
|
|
|
|
|
2019-10-10 15:44:39 +09:00
|
|
|
on(updateRoomForNewEventMessage, (state, action) => {
|
2019-10-11 11:58:03 +09:00
|
|
|
const roomInfo = {
|
|
|
|
...state.room.entities[action.roomSeq],
|
|
|
|
finalEventDate: action.info.sendDate,
|
|
|
|
finalEventMessage: action.info.sentMessage
|
|
|
|
};
|
2019-10-10 14:50:58 +09:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2019-10-11 11:58:03 +09:00
|
|
|
room: adapterRoom.updateOne(
|
|
|
|
{ id: action.roomSeq, changes: roomInfo },
|
|
|
|
{ ...state.room }
|
|
|
|
)
|
2019-10-10 14:50:58 +09:00
|
|
|
};
|
2019-10-11 13:11:48 +09:00
|
|
|
}),
|
|
|
|
|
2019-10-11 15:55:27 +09:00
|
|
|
on(RoomStore.updateSuccess, (state, action) => {
|
|
|
|
const roomInfo: RoomInfo = {
|
|
|
|
...state.room.entities[action.res.roomSeq],
|
|
|
|
roomName: action.res.roomName,
|
|
|
|
receiveAlarm: action.res.receiveAlarm
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
room: adapterRoom.updateOne(
|
|
|
|
{ id: action.res.roomSeq, changes: roomInfo },
|
|
|
|
{ ...state.room }
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-10-16 10:21:26 +09:00
|
|
|
on(refreshRoomSuccess, (state, action) => {
|
|
|
|
const roomUserList: RoomUserDetailData[] = [];
|
|
|
|
const roomUserShortList: RoomUserData[] = [];
|
|
|
|
|
|
|
|
if (action.userInfoList) {
|
|
|
|
roomUserList.push({
|
|
|
|
roomSeq: action.roomInfo.roomSeq,
|
|
|
|
userInfos: action.userInfoList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (action.userInfoShortList) {
|
|
|
|
roomUserShortList.push({
|
|
|
|
roomSeq: action.roomInfo.roomSeq,
|
|
|
|
userInfos: action.userInfoShortList
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
room: adapterRoom.upsertOne(action.roomInfo, {
|
|
|
|
...state.room
|
|
|
|
}),
|
|
|
|
roomUser: adapterRoomUser.upsertMany(roomUserList, {
|
|
|
|
...state.roomUser
|
|
|
|
}),
|
|
|
|
roomUserShort: adapterRoomUserShort.upsertMany(roomUserShortList, {
|
|
|
|
...state.roomUserShort
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2019-10-11 13:11:48 +09:00
|
|
|
on(AuthenticationStore.logout, (state, action) => {
|
|
|
|
return {
|
|
|
|
...initialState
|
|
|
|
};
|
2019-09-25 17:26:19 +09:00
|
|
|
})
|
|
|
|
);
|