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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 21 | import { NEW_LIMIT_TEMP_ID, selectValues } from './LimitEditorConstants.js'; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 22 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 23 | const LimitItem = ({ isReadOnlyMode, limit, onDelete, onSelect }) => { |
| 24 | const { |
| 25 | name, |
| 26 | description, |
| 27 | value, |
| 28 | metric, |
| 29 | aggregationFunction = '', |
| 30 | time = '' |
| 31 | } = limit; |
| 32 | const timeLabel = time ? `per ${time}` : ''; |
| 33 | let metricOption = selectValues.METRIC.find(item => item.enum === metric); |
| 34 | metricOption = metricOption ? metricOption.title : metric; |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 35 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 36 | return ( |
| 37 | <ListEditorItemView |
| 38 | onDelete={onDelete} |
| 39 | onSelect={onSelect} |
| 40 | isReadOnlyMode={isReadOnlyMode}> |
| 41 | <div className="list-editor-item-view-field limit-name"> |
| 42 | <div className="text name">{name}</div> |
| 43 | </div> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 44 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 45 | <div className="list-editor-item-view-field limit-description"> |
| 46 | <div className="text description">{description}</div> |
| 47 | </div> |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 48 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 49 | <div className="list-editor-item-view-field limit-metric-details"> |
| 50 | <div className="text description">{`${metricOption} ${value} ${aggregationFunction} ${timeLabel}`}</div> |
| 51 | </div> |
| 52 | </ListEditorItemView> |
| 53 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class Limits extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 57 | state = { |
| 58 | localFilter: '' |
| 59 | }; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 60 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 61 | render() { |
| 62 | const { |
| 63 | isReadOnlyMode = false, |
| 64 | limitEditor, |
| 65 | limitsList = [], |
| 66 | onCloseLimitEditor, |
| 67 | selectedLimit |
| 68 | } = this.props; |
| 69 | let limitsNames = {}; |
| 70 | for (let i = 0; i < limitsList.length; i++) { |
| 71 | limitsNames[limitsList[i].name.toLowerCase()] = limitsList[i].id; |
| 72 | } |
| 73 | return ( |
| 74 | <div className="license-model-limits-view"> |
| 75 | <ListEditorView isReadOnlyMode={isReadOnlyMode}> |
| 76 | {this.props.selectedLimit === NEW_LIMIT_TEMP_ID && |
| 77 | limitEditor.data && ( |
| 78 | <LimitEditor |
| 79 | limitsNames={limitsNames} |
| 80 | onCancel={onCloseLimitEditor} |
| 81 | onSubmit={() => this.submit()} |
| 82 | isReadOnlyMode={isReadOnlyMode} |
| 83 | /> |
| 84 | )} |
| 85 | {limitsList.length === 0 && |
| 86 | !limitEditor.data && ( |
| 87 | <div className="no-limits-text"> |
| 88 | {i18n('There are no limits')} |
| 89 | </div> |
| 90 | )} |
| 91 | {limitsList.map(limit => ( |
| 92 | <div key={limit.id} className="limit-item-wrapper"> |
| 93 | <LimitItem |
| 94 | onDelete={() => this.deleteLimit(limit)} |
| 95 | onSelect={ |
| 96 | selectedLimit |
| 97 | ? undefined |
| 98 | : () => this.props.onSelectLimit(limit) |
| 99 | } |
| 100 | clickable={!selectedLimit} |
| 101 | isReadOnlyMode={isReadOnlyMode} |
| 102 | limit={limit} |
| 103 | /> |
| 104 | {limit.id === selectedLimit && |
| 105 | limitEditor.data && ( |
| 106 | <LimitEditor |
| 107 | limitsNames={limitsNames} |
| 108 | onCancel={onCloseLimitEditor} |
| 109 | onSubmit={() => this.submit()} |
| 110 | isReadOnlyMode={isReadOnlyMode} |
| 111 | /> |
| 112 | )} |
| 113 | </div> |
| 114 | ))} |
| 115 | </ListEditorView> |
| 116 | </div> |
| 117 | ); |
| 118 | } |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 119 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 120 | submit() { |
| 121 | let { |
| 122 | onSubmit, |
| 123 | onCloseLimitEditor, |
| 124 | parent, |
| 125 | limitEditor, |
| 126 | licenseModelId, |
| 127 | version, |
| 128 | limitType |
| 129 | } = this.props; |
| 130 | onSubmit( |
| 131 | { type: limitType, ...limitEditor.data }, |
| 132 | parent, |
| 133 | licenseModelId, |
| 134 | version |
| 135 | ).then(() => onCloseLimitEditor()); |
| 136 | } |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 137 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 138 | deleteLimit(limit) { |
| 139 | let { |
| 140 | onDelete, |
| 141 | parent, |
| 142 | licenseModelId, |
| 143 | version, |
| 144 | onCloseLimitEditor, |
| 145 | selectedLimit |
| 146 | } = this.props; |
| 147 | onDelete({ |
| 148 | limit, |
| 149 | parent, |
| 150 | licenseModelId, |
| 151 | version, |
| 152 | onCloseLimitEditor, |
| 153 | selectedLimit |
| 154 | }); |
| 155 | } |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 156 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 157 | filterList() { |
| 158 | let { limitsList = [] } = this.props; |
| 159 | let { localFilter } = this.state; |
| 160 | if (localFilter.trim()) { |
| 161 | const filter = new RegExp(escape(localFilter), 'i'); |
| 162 | return limitsList.filter(({ name = '', description = '' }) => { |
| 163 | return ( |
| 164 | escape(name).match(filter) || |
| 165 | escape(description).match(filter) |
| 166 | ); |
| 167 | }); |
| 168 | } else { |
| 169 | return limitsList; |
| 170 | } |
| 171 | } |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | export default Limits; |