blob: 2c3442ee68667d58ea7511abba232be18f6bb9fe [file] [log] [blame]
talig8e9c0652017-12-20 14:30:43 +02001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17import {actionTypes} from './UserNotificationsConstants.js';
18
19export default (state = {}, action) => {
20 switch (action.type) {
21 case actionTypes.NOTIFICATION:
22 let list = (state.notificationsList) ? state.notificationsList : [];
23 const {notifications, lastScanned} = action.data;
24 return {
25 ...state,
26 lastScanned,
27 notificationsList: [...notifications, ...list],
28 numOfNotSeenNotifications: state.numOfNotSeenNotifications + notifications.length
29 };
30 case actionTypes.LOAD_NOTIFICATIONS:
31 return {
32 ...state,
33 ...action.result,
34 notificationsList: action.result.notifications,
35 notifications: undefined
36 };
37 case actionTypes.LOAD_PREV_NOTIFICATIONS:
38 const {notifications: prevNotifications, endOfPage: newEndOfPage} = action.result;
39 return {
40 ...state,
41 notificationsList: [
42 ...state.notificationsList,
43 ...prevNotifications
44 ],
45 endOfPage: newEndOfPage
46 };
47 case actionTypes.UPDATE_READ_NOTIFICATION:
48 let {notificationForUpdate} = action;
49 notificationForUpdate = {...notificationForUpdate, read: true};
50 const indexForEdit = state.notificationsList.findIndex(notification => notification.eventId === notificationForUpdate.eventId);
51 return {
52 ...state,
53 notificationsList: [
54 ...state.notificationsList.slice(0, indexForEdit),
55 notificationForUpdate,
56 ...state.notificationsList.slice(indexForEdit + 1)
57 ]
58 };
59 case actionTypes.RESET_NEW_NOTIFICATIONS:
60 return {
61 ...state,
62 numOfNotSeenNotifications: 0
63 };
64 case actionTypes.TOGGLE_OVERLAY:
65 return {
66 ...state,
67 showNotificationsOverlay: action.showNotificationsOverlay
68 };
69 default:
70 return state;
71 }
72};