104 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-09-25 17:26:19 +09:00
import { createReducer, on } from '@ngrx/store';
import {
initialState,
adapterBuddy2,
adapterGroup2,
adapterRoom,
adapterRoomUser,
adapterRoomUserShort
} from './state';
2019-10-10 15:44:39 +09:00
import {
buddy2Success,
group2Success,
roomSuccess,
updateRoomForNewEventMessage
} from './actions';
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-09-25 17:26:19 +09:00
export const reducer = createReducer(
initialState,
on(buddy2Success, (state, action) => {
return {
...state,
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,
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) => {
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,
room: adapterRoom.addAll(action.roomList, {
...state.room,
syncDate: action.syncDate
}),
roomUser: adapterRoomUser.addAll(roomUserList, {
...state.roomUser
}),
roomUserShort: adapterRoomUserShort.addAll(roomUserShortList, {
...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) => {
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,
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
}),
on(AuthenticationStore.logout, (state, action) => {
return {
...initialState
};
2019-09-25 17:26:19 +09:00
})
);