talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 1 | /*! |
| 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 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 17 | import { actionTypes } from './UserNotificationsConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 18 | |
| 19 | export default (state = {}, action) => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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: |
| 29 | state.numOfNotSeenNotifications + notifications.length |
| 30 | }; |
| 31 | case actionTypes.LOAD_NOTIFICATIONS: |
| 32 | return { |
| 33 | ...state, |
| 34 | ...action.result, |
| 35 | notificationsList: action.result.notifications, |
| 36 | notifications: undefined |
| 37 | }; |
| 38 | case actionTypes.LOAD_PREV_NOTIFICATIONS: |
| 39 | const { |
| 40 | notifications: prevNotifications, |
| 41 | endOfPage: newEndOfPage |
| 42 | } = action.result; |
| 43 | return { |
| 44 | ...state, |
| 45 | notificationsList: [ |
| 46 | ...state.notificationsList, |
| 47 | ...prevNotifications |
| 48 | ], |
| 49 | endOfPage: newEndOfPage |
| 50 | }; |
| 51 | case actionTypes.UPDATE_READ_NOTIFICATION: |
| 52 | let { notificationForUpdate } = action; |
| 53 | notificationForUpdate = { ...notificationForUpdate, read: true }; |
| 54 | const indexForEdit = state.notificationsList.findIndex( |
| 55 | notification => |
| 56 | notification.eventId === notificationForUpdate.eventId |
| 57 | ); |
| 58 | return { |
| 59 | ...state, |
| 60 | notificationsList: [ |
| 61 | ...state.notificationsList.slice(0, indexForEdit), |
| 62 | notificationForUpdate, |
| 63 | ...state.notificationsList.slice(indexForEdit + 1) |
| 64 | ] |
| 65 | }; |
| 66 | case actionTypes.RESET_NEW_NOTIFICATIONS: |
| 67 | return { |
| 68 | ...state, |
| 69 | numOfNotSeenNotifications: 0 |
| 70 | }; |
| 71 | case actionTypes.TOGGLE_OVERLAY: |
| 72 | return { |
| 73 | ...state, |
| 74 | showNotificationsOverlay: action.showNotificationsOverlay |
| 75 | }; |
| 76 | default: |
| 77 | return state; |
| 78 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 79 | }; |