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