blob: 4841d0335ce346a0ad84460f6770a40fead079ee [file] [log] [blame]
Avi Ziv61070c92017-07-26 17:37:57 +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 from 'react';
17import i18n from 'nfvo-utils/i18n/i18n.js';
18import ListEditorView from 'nfvo-components/listEditor/ListEditorView.jsx';
19import ListEditorItemView from 'nfvo-components/listEditor/ListEditorItemView.jsx';
az2497644017c2017-08-10 17:49:40 +030020import LimitEditor from './LimitEditor.js';
Avi Ziv61070c92017-07-26 17:37:57 +030021import {NEW_LIMIT_TEMP_ID, selectValues} from './LimitEditorConstants.js';
22
23const LimitItem = ({isReadOnlyMode, limit, onDelete, onSelect}) => {
az2497644017c2017-08-10 17:49:40 +030024 const {name, description, value, metric, aggregationFunction = '', time = ''} = limit;
Avi Ziv61070c92017-07-26 17:37:57 +030025 const timeLabel = time ? `per ${time}` : '';
az2497644017c2017-08-10 17:49:40 +030026 let metricOption = selectValues.METRIC.find(item => item.enum === metric);
27 metricOption = metricOption ? metricOption.title : metric;
28
Avi Ziv61070c92017-07-26 17:37:57 +030029 return (
az2497644017c2017-08-10 17:49:40 +030030 <ListEditorItemView
Avi Ziv61070c92017-07-26 17:37:57 +030031 onDelete={onDelete}
32 onSelect={onSelect}
33 isReadOnlyMode={isReadOnlyMode}>
az2497644017c2017-08-10 17:49:40 +030034 <div className='list-editor-item-view-field limit-name'>
Avi Ziv61070c92017-07-26 17:37:57 +030035 <div className='text name'>{name}</div>
36 </div>
37
az2497644017c2017-08-10 17:49:40 +030038 <div className='list-editor-item-view-field limit-description'>
Avi Ziv61070c92017-07-26 17:37:57 +030039 <div className='text description'>{description}</div>
40 </div>
41
az2497644017c2017-08-10 17:49:40 +030042 <div className='list-editor-item-view-field limit-metric-details'>
43 <div className='text description'>{`${metricOption} ${value} ${aggregationFunction} ${timeLabel}`}</div>
44 </div>
Avi Ziv61070c92017-07-26 17:37:57 +030045 </ListEditorItemView>
46 );
47};
48
49class Limits extends React.Component {
50
az2497644017c2017-08-10 17:49:40 +030051
Avi Ziv61070c92017-07-26 17:37:57 +030052 state = {
53 localFilter: ''
54 };
55
az2497644017c2017-08-10 17:49:40 +030056 render() {
Avi Ziv61070c92017-07-26 17:37:57 +030057 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'>
ilanapbf13de92017-10-23 15:21:22 +030064 <ListEditorView isReadOnlyMode={isReadOnlyMode}>
Avi Ziv61070c92017-07-26 17:37:57 +030065 {this.props.selectedLimit === NEW_LIMIT_TEMP_ID && limitEditor.data &&
ilanapbf13de92017-10-23 15:21:22 +030066 <LimitEditor limitsNames={limitsNames} onCancel={onCloseLimitEditor} onSubmit={ () => this.submit()} isReadOnlyMode={isReadOnlyMode}/>
Avi Ziv61070c92017-07-26 17:37:57 +030067 }
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'>
az2497644017c2017-08-10 17:49:40 +030071 <LimitItem
72 onDelete={() => this.deleteLimit(limit)}
Avi Ziv61070c92017-07-26 17:37:57 +030073 onSelect={selectedLimit ? undefined : () => this.props.onSelectLimit(limit)}
az2497644017c2017-08-10 17:49:40 +030074 clickable={!selectedLimit}
75 isReadOnlyMode={isReadOnlyMode}
Avi Ziv61070c92017-07-26 17:37:57 +030076 limit={limit}/>
ilanapbf13de92017-10-23 15:21:22 +030077 {limit.id === selectedLimit && limitEditor.data &&
78 <LimitEditor
79 limitsNames={limitsNames}
80 onCancel={onCloseLimitEditor}
81 onSubmit={ () => this.submit()}
82 isReadOnlyMode={isReadOnlyMode} />
83 }
Avi Ziv61070c92017-07-26 17:37:57 +030084 </div> )}
85 </ListEditorView>
az2497644017c2017-08-10 17:49:40 +030086
Avi Ziv61070c92017-07-26 17:37:57 +030087 </div>
88 );
89 }
90
91 submit() {
az2497644017c2017-08-10 17:49:40 +030092 let {onSubmit, onCloseLimitEditor, parent, limitEditor, licenseModelId, version, limitType} = this.props;
Avi Ziv61070c92017-07-26 17:37:57 +030093 onSubmit({type: limitType, ...limitEditor.data}, parent, licenseModelId, version).then(() => onCloseLimitEditor());
94 }
95
az2497644017c2017-08-10 17:49:40 +030096 deleteLimit(limit) {
Avi Ziv61070c92017-07-26 17:37:57 +030097 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
116export default Limits;