Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +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 from 'react'; |
| 17 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 18 | import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx'; |
| 19 | import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx'; |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 20 | import LimitEditor from './LimitEditor.js'; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 21 | import {NEW_LIMIT_TEMP_ID, selectValues} from './LimitEditorConstants.js'; |
| 22 | |
| 23 | const LimitItem = ({isReadOnlyMode, limit, onDelete, onSelect}) => { |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 24 | const {name, description, value, metric, aggregationFunction = '', time = ''} = limit; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 25 | const timeLabel = time ? `per ${time}` : ''; |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 26 | let metricOption = selectValues.METRIC.find(item => item.enum === metric); |
| 27 | metricOption = metricOption ? metricOption.title : metric; |
| 28 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 29 | return ( |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 30 | <ListEditorItemView |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 31 | onDelete={onDelete} |
| 32 | onSelect={onSelect} |
| 33 | isReadOnlyMode={isReadOnlyMode}> |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 34 | <div className='list-editor-item-view-field limit-name'> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 35 | <div className='text name'>{name}</div> |
| 36 | </div> |
| 37 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 38 | <div className='list-editor-item-view-field limit-description'> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 39 | <div className='text description'>{description}</div> |
| 40 | </div> |
| 41 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 42 | <div className='list-editor-item-view-field limit-metric-details'> |
| 43 | <div className='text description'>{`${metricOption} ${value} ${aggregationFunction} ${timeLabel}`}</div> |
| 44 | </div> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 45 | </ListEditorItemView> |
| 46 | ); |
| 47 | }; |
| 48 | |
| 49 | class Limits extends React.Component { |
| 50 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 51 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 52 | state = { |
| 53 | localFilter: '' |
| 54 | }; |
| 55 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 56 | render() { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 57 | const {isReadOnlyMode = false, limitEditor, limitsList = [], onCloseLimitEditor, selectedLimit} = this.props; |
| 58 | let limitsNames = {}; |
| 59 | for (let i = 0; i < limitsList.length; i++) { |
| 60 | limitsNames[limitsList[i].name.toLowerCase()] = limitsList[i].id; |
| 61 | } |
| 62 | return ( |
| 63 | <div className='license-model-limits-view'> |
ilanap | bf13de9 | 2017-10-23 15:21:22 +0300 | [diff] [blame] | 64 | <ListEditorView isReadOnlyMode={isReadOnlyMode}> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 65 | {this.props.selectedLimit === NEW_LIMIT_TEMP_ID && limitEditor.data && |
ilanap | bf13de9 | 2017-10-23 15:21:22 +0300 | [diff] [blame] | 66 | <LimitEditor limitsNames={limitsNames} onCancel={onCloseLimitEditor} onSubmit={ () => this.submit()} isReadOnlyMode={isReadOnlyMode}/> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 67 | } |
| 68 | {limitsList.length === 0 && !limitEditor.data && <div className='no-limits-text'>{i18n('There are no limits')}</div>} |
| 69 | {limitsList.map(limit => |
| 70 | <div key={limit.id} className='limit-item-wrapper'> |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 71 | <LimitItem |
| 72 | onDelete={() => this.deleteLimit(limit)} |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 73 | onSelect={selectedLimit ? undefined : () => this.props.onSelectLimit(limit)} |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 74 | clickable={!selectedLimit} |
| 75 | isReadOnlyMode={isReadOnlyMode} |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 76 | limit={limit}/> |
ilanap | bf13de9 | 2017-10-23 15:21:22 +0300 | [diff] [blame] | 77 | {limit.id === selectedLimit && limitEditor.data && |
| 78 | <LimitEditor |
| 79 | limitsNames={limitsNames} |
| 80 | onCancel={onCloseLimitEditor} |
| 81 | onSubmit={ () => this.submit()} |
| 82 | isReadOnlyMode={isReadOnlyMode} /> |
| 83 | } |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 84 | </div> )} |
| 85 | </ListEditorView> |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 86 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 87 | </div> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | submit() { |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 92 | let {onSubmit, onCloseLimitEditor, parent, limitEditor, licenseModelId, version, limitType} = this.props; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 93 | onSubmit({type: limitType, ...limitEditor.data}, parent, licenseModelId, version).then(() => onCloseLimitEditor()); |
| 94 | } |
| 95 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 96 | deleteLimit(limit) { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 97 | let {onDelete, parent, licenseModelId, version, onCloseLimitEditor, selectedLimit} = this.props; |
| 98 | onDelete({limit, parent, licenseModelId, version, onCloseLimitEditor, selectedLimit}); |
| 99 | } |
| 100 | |
| 101 | filterList() { |
| 102 | let {limitsList = []} = this.props; |
| 103 | let {localFilter} = this.state; |
| 104 | if (localFilter.trim()) { |
| 105 | const filter = new RegExp(escape(localFilter), 'i'); |
| 106 | return limitsList.filter(({name = '', description = ''}) => { |
| 107 | return escape(name).match(filter) || escape(description).match(filter); |
| 108 | }); |
| 109 | } |
| 110 | else { |
| 111 | return limitsList; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | export default Limits; |