blob: 26675609353fb602cb875ad45c77a3867004cc05 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020021import { NEW_LIMIT_TEMP_ID, selectValues } from './LimitEditorConstants.js';
Avi Ziv61070c92017-07-26 17:37:57 +030022
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023const 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;
az2497644017c2017-08-10 17:49:40 +030035
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 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 Ziv61070c92017-07-26 17:37:57 +030044
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 <div className="list-editor-item-view-field limit-description">
46 <div className="text description">{description}</div>
47 </div>
Avi Ziv61070c92017-07-26 17:37:57 +030048
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 <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 Ziv61070c92017-07-26 17:37:57 +030054};
55
56class Limits extends React.Component {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020057 state = {
58 localFilter: ''
59 };
Avi Ziv61070c92017-07-26 17:37:57 +030060
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020061 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 }
az2497644017c2017-08-10 17:49:40 +0300119
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200120 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 Ziv61070c92017-07-26 17:37:57 +0300137
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 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 }
az2497644017c2017-08-10 17:49:40 +0300156
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200157 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 Ziv61070c92017-07-26 17:37:57 +0300172}
173
174export default Limits;