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