summaryrefslogtreecommitdiffstats
path: root/openecomp-ui/src/sdc-app/onboarding/userNotifications/UserNotifications.jsx
blob: c01424ee1b4e6bde715b8ac1e253e5801c176db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*!
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import i18n from 'nfvo-utils/i18n/i18n.js';
import {notificationType} from './UserNotificationsConstants.js';
import ShowMore from 'react-show-more';

const Notification = ({notification, users, onActionClicked, getNotificationTypeDesc}) => {
	const {eventType, read, eventAttributes, dateTime} = notification;
	const {itemName, userId, description, versionName, permission, granted} = eventAttributes;
	const {fullName: userName} = users.find(user => user.userId === userId);
	return (
		<div className={classnames('notification', {'unread': !read})}>
			<div className='notification-data'>
				<div className='item-name'>
					{itemName}
					{versionName && <span>&nbsp;&nbsp;&nbsp;v{versionName}</span>}
					{!read && <div className='unread-circle-icon'></div> }
				</div>
				<div className='flex-items'>
					<div className='type'>{getNotificationTypeDesc(eventType, permission, granted)}</div>
					<div className='separator'/>
					<div className='user-name'>{`${i18n('By')} ${userName}`}</div>
				</div>
				{(description || versionName) && <div className='description'>
					{description && <ShowMore anchorClass='more-less' lines={2} more={i18n('More')} less={i18n('Less')}>
						{description}
					</ShowMore>}
					{eventType === notificationType.ITEM_CHANGED.SUBMIT &&
						<div>
							<div>{i18n('Version {versionName} was submitted.', {versionName: versionName})}</div>
						</div>
					}
				</div>
				}
				<div className='date'>{dateTime}</div>
			</div>
			<div className='notification-action'>
				<div className={classnames('action-button', {'hidden': read})} onClick={() => onActionClicked(notification)}>
					{eventType === notificationType.PERMISSION_CHANGED ? i18n('Accept') : i18n('Sync')}
				</div>
			</div>
		</div>
	);
};

function getNotificationTypeDesc(eventType, permission, granted) {
	switch (eventType) {
		case notificationType.PERMISSION_CHANGED:
			return i18n('Permission {granted}: {permission}', {granted: granted ? 'Granted' : 'Taken', permission: permission});
		case notificationType.ITEM_CHANGED.COMMIT:
			return i18n('Your Copy Is Out Of Sync');
		case notificationType.ITEM_CHANGED.SUBMIT:
			return i18n('Version Submitted');
	}
}

class UserNotifications extends React.Component {

	static propTypes = {
		currentScreen: PropTypes.object,
		notificationsList: PropTypes.array,
		usersList: PropTypes.array,
		lastScanned: PropTypes.string,
		endOfPage:PropTypes.string,
		onLoadPrevNotifications: PropTypes.func,
		onSync: PropTypes.func,
		updateNotification: PropTypes.func,
		onLoadItemsLists: PropTypes.func
	};

	render() {
		const {notificationsList = [], usersList, lastScanned, endOfPage} = this.props;

		return (
			<div className='user-notifications'>
				<div className='notifications-title'>{i18n('Notifications')}</div>
				<div className='notifications-list' ref='notificationList' onScroll={() => this.loadPrevNotifications(lastScanned, endOfPage)}>
				{
					notificationsList.map(notification => (
						<Notification key={notification.eventId} notification={notification} users={usersList}
							onActionClicked={notification => this.onActionClicked(notification)}
							getNotificationTypeDesc={getNotificationTypeDesc}/>))
				}
				</div>
			</div>
		);
	}

	onActionClicked(notification) {
		const {onSync, updateNotification, currentScreen, onLoadItemsLists} = this.props;
		const {eventType, eventAttributes: {itemId, itemName, versionId, versionName}} = notification;
		if(eventType !== notificationType.PERMISSION_CHANGED) {
			onSync({itemId, itemName, versionId, versionName, currentScreen});
		}
		else {
			onLoadItemsLists();
		}
		updateNotification(notification);
	}

	loadPrevNotifications(lastScanned, endOfPage) {
		if(endOfPage && lastScanned) {
			let element = ReactDOM.findDOMNode(this.refs['notificationList']);
			const {onLoadPrevNotifications} = this.props;

			if (element && element.clientHeight + element.scrollTop === element.scrollHeight) {
				onLoadPrevNotifications(lastScanned, endOfPage);
			}
		}
	}
}

export default UserNotifications;