blob: 6ff3c806a82946ef26f47ad8cfc428164415f8a9 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
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 */
16import React, {Component} from 'react';
17import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger.js';
18import Tooltip from 'react-bootstrap/lib/Tooltip.js';
19import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
20import SVGIcon from 'nfvo-components/icon/SVGIcon.jsx';
21import i18n from 'nfvo-utils/i18n/i18n.js';
22
23function ActivityLogSortableCellHeader({isHeader, data, isDes, onSort}) {
24 if (isHeader) {
25 return (
26 <span className='date-header' onClick={onSort}>
27 <span>{data}</span>
28 <span className={`header-sort-arrow ${isDes ? 'up' : 'down'}`}></span>
29 </span>
30 );
31 }
32 return (
33 <span className='date-cell'>
34 <span>{i18n.dateNormal(data, {
35 year: 'numeric', month: 'numeric', day: 'numeric'
36 })}</span>
37 <span>{i18n.dateNormal(data, {
38 hour: 'numeric', minute: 'numeric',
39 hour12: true
40 })}</span>
41 </span>
42 );
43}
44
45function ActivityLogStatus({status, isHeader}) {
46 if (isHeader) {
47 return <span>{status}</span>;
48 }
49 let {message, success} = status;
50 return (
51 <span>
52 <span className={`status-icon ${success}`}>{`${success ? i18n('Success') : i18n('Failure')}`}</span>
53 {success && <SVGIcon name='check-circle'/>}
54 {!success && <OverlayTrigger placement='bottom' overlay={<Tooltip className='activity-log-message-tooltip' id={'activity-log-message-tooltip'}>
55 <div className='message-block'>{message}</div>
56 </Tooltip>}>
57 <span className='message-further-info-icon'>{'?'}</span>
58 </OverlayTrigger>}
59 </span>
60 );
61}
62
63export function ActivityListItem({activity, isHeader, isDes, onSort}) {
64 let {type, timestamp, comment, user, status} = activity;
65 return (
66 <li className={`activity-list-item ${isHeader ? 'header' : ''}`} data-test-id='activity-list-item'>
67 <div className='table-cell activity-date' data-test-id='activity-date'><ActivityLogSortableCellHeader isHeader={isHeader} data={timestamp} isDes={isDes} onSort={onSort}/></div>
68 <div className='table-cell activity-action' data-test-id='activity-action'>{type}</div>
69 <div className='table-cell activity-comment' title={comment} data-test-id='activity-comment'><span>{comment}</span></div>
70 <div className='table-cell activity-username' data-test-id='activity-username'>{user}</div>
71 <div className='table-cell activity-status' data-test-id='activity-status'><ActivityLogStatus isHeader={isHeader} status={status}/></div>
72 </li>
73 );
74}
75
76class ActivityLogView extends Component {
77
78 state = {
79 localFilter: '',
80 sortDescending: true
81 };
82
83 render() {
84 return (
85 <div className='activity-log-view'>
86 <ListEditorView
87 title={i18n('Activity Log')}
88 filterValue={this.state.localFilter}
89 onFilter={filter => this.setState({localFilter: filter})}>
90 <ActivityListItem
91 isHeader={true}
92 activity={{timestamp: 'Date', type: 'Action', comment: 'Comment', user: 'Username', status: 'Status'}}
93 isDes={this.state.sortDescending}
94 onSort={() => this.setState({sortDescending: !this.state.sortDescending})}/>
95 {this.sortActivities(this.filterActivities(), this.state.sortDescending).map(activity => <ActivityListItem key={activity.id} activity={activity}/>)}
96 </ListEditorView>
97 </div>
98 );
99 }
100
101 filterActivities() {
102 let {activities} = this.props;
103 let {localFilter} = this.state;
104 if (localFilter.trim()) {
105 const filter = new RegExp(escape(localFilter), 'i');
106 return activities.filter(({user = '', comment = '', type = ''}) => escape(user).match(filter) || escape(comment).match(filter) || escape(type).match(filter));
107 }
108 else {
109 return activities;
110 }
111 }
112
113 sortActivities(activities) {
114 if (this.state.sortDescending) {
115 return activities.sort((a, b) => a.timestamp - b.timestamp);
116 }
117 else {
118 return activities.reverse();
119 }
120 }
121
122}
123
124export default ActivityLogView;