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 | |
| 17 | import React from 'react'; |
| 18 | import PropTypes from 'prop-types'; |
| 19 | import enhanceWithClickOutside from 'react-click-outside'; |
| 20 | import classnames from 'classnames'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 21 | import { connect } from 'react-redux'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 22 | import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js'; |
| 23 | import Overlay from 'nfvo-components/overlay/Overlay.jsx'; |
| 24 | import UserNotifications from 'sdc-app/onboarding/userNotifications/UserNotifications.jsx'; |
| 25 | import UserNotificationsActionHelper from 'sdc-app/onboarding/userNotifications/UserNotificationsActionHelper.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 26 | import { actionTypes } from './UserNotificationsConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 27 | import OnboardingActionHelper from 'sdc-app/onboarding/OnboardingActionHelper.js'; |
| 28 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | const mapStateToProps = ({ |
| 30 | currentScreen, |
| 31 | notifications, |
| 32 | users: { usersList } |
| 33 | }) => { |
| 34 | return { currentScreen, notifications, usersList }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 37 | const mapActionToProps = dispatch => { |
| 38 | return { |
| 39 | resetNewNotifications: notificationId => |
| 40 | UserNotificationsActionHelper.updateLastSeenNotification(dispatch, { |
| 41 | notificationId |
| 42 | }), |
| 43 | toggleOverlay: ({ showNotificationsOverlay }) => |
| 44 | dispatch({ |
| 45 | type: actionTypes.TOGGLE_OVERLAY, |
| 46 | showNotificationsOverlay |
| 47 | }), |
| 48 | onLoadPrevNotifications: (lastScanned, endOfPage) => |
| 49 | UserNotificationsActionHelper.loadPreviousNotifications(dispatch, { |
| 50 | lastScanned, |
| 51 | endOfPage |
| 52 | }), |
| 53 | onSync: ({ itemId, itemName, versionId, versionName, currentScreen }) => |
| 54 | UserNotificationsActionHelper.syncItem(dispatch, { |
| 55 | itemId, |
| 56 | itemName, |
| 57 | versionId, |
| 58 | versionName, |
| 59 | currentScreen |
| 60 | }), |
| 61 | updateNotification: notificationForUpdate => |
| 62 | UserNotificationsActionHelper.updateNotification(dispatch, { |
| 63 | notificationForUpdate |
| 64 | }), |
| 65 | onLoadItemsLists: () => OnboardingActionHelper.loadItemsLists(dispatch) |
| 66 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 69 | class NotificationsView extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | static propTypes = { |
| 71 | currentScreen: PropTypes.object, |
| 72 | notifications: PropTypes.object, |
| 73 | resetNewNotifications: PropTypes.func, |
| 74 | toggleOverlay: PropTypes.func, |
| 75 | onLoadPrevNotifications: PropTypes.func, |
| 76 | onSync: PropTypes.func, |
| 77 | updateNotification: PropTypes.func, |
| 78 | onLoadItemsLists: PropTypes.func |
| 79 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 80 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 81 | render() { |
| 82 | const { |
| 83 | usersList, |
| 84 | notifications, |
| 85 | onLoadPrevNotifications, |
| 86 | onSync, |
| 87 | updateNotification, |
| 88 | onLoadItemsLists, |
| 89 | currentScreen |
| 90 | } = this.props; |
| 91 | const { |
| 92 | notificationsList, |
| 93 | numOfNotSeenNotifications, |
| 94 | showNotificationsOverlay, |
| 95 | lastScanned, |
| 96 | endOfPage |
| 97 | } = notifications; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 98 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 99 | return ( |
| 100 | <div className="onboarding-notifications"> |
| 101 | <div |
| 102 | className="notifications-icon" |
| 103 | onClick={() => this.onNotificationIconClick()}> |
| 104 | <SVGIcon |
| 105 | name={ |
| 106 | numOfNotSeenNotifications > 0 |
| 107 | ? 'notificationFullBell' |
| 108 | : 'notificationBell' |
| 109 | } |
| 110 | color={numOfNotSeenNotifications > 0 ? 'primary' : ''} |
| 111 | /> |
| 112 | <div |
| 113 | className={classnames('notifications-count', { |
| 114 | 'hidden-count': numOfNotSeenNotifications === 0 |
| 115 | })}> |
| 116 | {numOfNotSeenNotifications} |
| 117 | </div> |
| 118 | </div> |
| 119 | {showNotificationsOverlay && ( |
| 120 | <Overlay> |
| 121 | <UserNotifications |
| 122 | notificationsList={notificationsList} |
| 123 | usersList={usersList} |
| 124 | lastScanned={lastScanned} |
| 125 | endOfPage={endOfPage} |
| 126 | onLoadPrevNotifications={onLoadPrevNotifications} |
| 127 | onSync={onSync} |
| 128 | updateNotification={updateNotification} |
| 129 | onLoadItemsLists={onLoadItemsLists} |
| 130 | currentScreen={currentScreen} |
| 131 | /> |
| 132 | </Overlay> |
| 133 | )} |
| 134 | </div> |
| 135 | ); |
| 136 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 137 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 138 | handleClickOutside() { |
| 139 | const { notifications: { showNotificationsOverlay } } = this.props; |
| 140 | if (showNotificationsOverlay) { |
| 141 | this.onCloseOverlay(); |
| 142 | } |
| 143 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 144 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 145 | onNotificationIconClick() { |
| 146 | const { |
| 147 | notifications: { showNotificationsOverlay }, |
| 148 | toggleOverlay |
| 149 | } = this.props; |
| 150 | if (showNotificationsOverlay) { |
| 151 | this.onCloseOverlay(); |
| 152 | } else { |
| 153 | toggleOverlay({ showNotificationsOverlay: true }); |
| 154 | } |
| 155 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 156 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 157 | onCloseOverlay() { |
| 158 | const { |
| 159 | notifications: { numOfNotSeenNotifications, lastScanned }, |
| 160 | resetNewNotifications, |
| 161 | toggleOverlay |
| 162 | } = this.props; |
| 163 | if (numOfNotSeenNotifications) { |
| 164 | resetNewNotifications(lastScanned); |
| 165 | } |
| 166 | toggleOverlay({ showNotificationsOverlay: false }); |
| 167 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | export default connect(mapStateToProps, mapActionToProps)( |
| 171 | enhanceWithClickOutside(NotificationsView) |
| 172 | ); |