2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 import React from 'react';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import Validator from 'nfvo-utils/Validator.js';
21 import Input from 'nfvo-components/input/validation/Input.jsx';
22 import InputOptions from 'nfvo-components/input/validation/InputOptions.jsx';
23 import Form from 'nfvo-components/input/validation/Form.jsx';
24 import GridSection from 'nfvo-components/grid/GridSection.jsx';
25 import GridItem from 'nfvo-components/grid/GridItem.jsx';
26 import {optionsInputValues as EntitlementPoolsOptionsInputValues, thresholdUnitType, SP_ENTITLEMENT_POOL_FORM} from './EntitlementPoolsConstants.js';
27 import {other as optionInputOther} from 'nfvo-components/input/inputOptions/InputOptions.jsx';
29 const EntitlementPoolPropType = React.PropTypes.shape({
30 id: React.PropTypes.string,
31 name: React.PropTypes.string,
32 description: React.PropTypes.string,
33 manufacturerReferenceNumber: React.PropTypes.string,
34 operationalScope: React.PropTypes.shape({
35 choices: React.PropTypes.array,
36 other: React.PropTypes.string
38 aggregationFunction: React.PropTypes.shape({
39 choice: React.PropTypes.string,
40 other: React.PropTypes.string
42 increments: React.PropTypes.string,
43 time: React.PropTypes.shape({
44 choice: React.PropTypes.string,
45 other: React.PropTypes.string
47 entitlementMetric: React.PropTypes.shape({
48 choice: React.PropTypes.string,
49 other: React.PropTypes.string
53 const EntitlementPoolsFormContent = ({data, genericFieldInfo, onDataChanged, validateName, validateChoiceWithOther, validateTimeOtherValue, thresholdValueValidation}) => {
55 name, description, manufacturerReferenceNumber, operationalScope , aggregationFunction, thresholdUnits, thresholdValue,
56 increments, time, entitlementMetric} = data;
60 <GridItem colSpan={2}>
62 onChange={name => onDataChanged({name}, SP_ENTITLEMENT_POOL_FORM, {name: validateName})}
63 isValid={genericFieldInfo.name.isValid}
65 errorText={genericFieldInfo.name.errorText}
68 data-test-id='create-ep-name'
71 <GridItem colSpan={2}>
73 onInputChange={()=>{}}
77 onEnumChange={operationalScope => onDataChanged({operationalScope:{choices: operationalScope, other: ''}},
78 SP_ENTITLEMENT_POOL_FORM, {operationalScope: validateChoiceWithOther})}
79 onOtherChange={operationalScope => onDataChanged({operationalScope:{choices: [optionInputOther.OTHER],
80 other: operationalScope}}, SP_ENTITLEMENT_POOL_FORM, {operationalScope: validateChoiceWithOther})}
81 label={i18n('Operational Scope')}
82 data-test-id='create-ep-operational-scope'
84 multiSelectedEnum={operationalScope && operationalScope.choices}
85 otherValue={operationalScope && operationalScope.other}
86 values={EntitlementPoolsOptionsInputValues.OPERATIONAL_SCOPE}
87 isValid={genericFieldInfo.operationalScope.isValid}
88 errorText={genericFieldInfo.operationalScope.errorText} />
90 <GridItem colSpan={2} stretch>
92 onChange={description => onDataChanged({description}, SP_ENTITLEMENT_POOL_FORM)}
93 isValid={genericFieldInfo.description.isValid}
94 errorText={genericFieldInfo.description.errorText}
95 label={i18n('Description')}
98 data-test-id='create-ep-description'
101 <GridItem colSpan={2}>
102 <div className='threshold-section'>
106 // setting the unit to the correct value
107 const selectedIndex = e.target.selectedIndex;
108 const val = e.target.options[selectedIndex].value;
109 onDataChanged({thresholdUnits: val}, SP_ENTITLEMENT_POOL_FORM);
110 // TODO make sure that the value is valid too
111 onDataChanged({thresholdValue: thresholdValue}, SP_ENTITLEMENT_POOL_FORM,{thresholdValue : thresholdValueValidation});}
114 value={thresholdUnits}
115 label={i18n('Threshold Units')}
116 data-test-id='create-ep-threshold-units'
117 isValid={genericFieldInfo.thresholdUnits.isValid}
118 errorText={genericFieldInfo.thresholdUnits.errorText}
119 groupClassName='bootstrap-input-options'
120 className='input-options-select'
122 {EntitlementPoolsOptionsInputValues.THRESHOLD_UNITS.map(mtype =>
123 <option key={mtype.enum} value={mtype.enum}>{`${mtype.title}`}</option>)}
127 className='entitlement-pools-form-row-threshold-value'
128 onChange={thresholdValue => onDataChanged({thresholdValue}, SP_ENTITLEMENT_POOL_FORM,
129 {thresholdValue : thresholdValueValidation})}
130 label={i18n('Threshold Value')}
131 isValid={genericFieldInfo.thresholdValue.isValid}
132 errorText={genericFieldInfo.thresholdValue.errorText}
133 data-test-id='create-ep-threshold-value'
134 value={thresholdValue}
139 onInputChange={()=>{}}
140 isMultiSelect={false}
142 onEnumChange={entitlementMetric => onDataChanged({entitlementMetric:{choice: entitlementMetric, other: ''}},
143 SP_ENTITLEMENT_POOL_FORM, {entitlementMetric: validateChoiceWithOther})}
144 onOtherChange={entitlementMetric => onDataChanged({entitlementMetric:{choice: optionInputOther.OTHER,
145 other: entitlementMetric}}, SP_ENTITLEMENT_POOL_FORM, {entitlementMetric: validateChoiceWithOther})}
146 label={i18n('Entitlement Metric')}
147 data-test-id='create-ep-entitlement-metric'
150 selectedEnum={entitlementMetric && entitlementMetric.choice}
151 otherValue={entitlementMetric && entitlementMetric.other}
152 values={EntitlementPoolsOptionsInputValues.ENTITLEMENT_METRIC}
153 isValid={genericFieldInfo.entitlementMetric.isValid}
154 errorText={genericFieldInfo.entitlementMetric.errorText} />
156 onInputChange={()=>{}}
157 isMultiSelect={false}
159 onEnumChange={aggregationFunction => onDataChanged({aggregationFunction:{choice: aggregationFunction, other: ''}},
160 SP_ENTITLEMENT_POOL_FORM, {aggregationFunction: validateChoiceWithOther})}
161 onOtherChange={aggregationFunction => onDataChanged({aggregationFunction:{choice: optionInputOther.OTHER,
162 other: aggregationFunction}}, SP_ENTITLEMENT_POOL_FORM, {aggregationFunction: validateChoiceWithOther})}
163 label={i18n('Aggregate Function')}
164 data-test-id='create-ep-aggregate-function'
167 selectedEnum={aggregationFunction && aggregationFunction.choice}
168 otherValue={aggregationFunction && aggregationFunction.other}
169 values={EntitlementPoolsOptionsInputValues.AGGREGATE_FUNCTION}
170 isValid={genericFieldInfo.aggregationFunction.isValid}
171 errorText={genericFieldInfo.aggregationFunction.errorText} />
173 <GridItem colSpan={2}>
175 onChange={manufacturerReferenceNumber => onDataChanged({manufacturerReferenceNumber}, SP_ENTITLEMENT_POOL_FORM)}
176 label={i18n('Manufacturer Reference Number')}
177 value={manufacturerReferenceNumber}
179 data-test-id='create-ep-reference-number'
182 <GridItem colSpan={2}>
184 onInputChange={()=>{}}
185 isMultiSelect={false}
187 onEnumChange={time => onDataChanged({time:{choice: time, other: ''}},
188 SP_ENTITLEMENT_POOL_FORM, {time: validateChoiceWithOther})}
189 onOtherChange={time => onDataChanged({time:{choice: optionInputOther.OTHER,
190 other: time}}, SP_ENTITLEMENT_POOL_FORM, {time: validateTimeOtherValue})}
192 data-test-id='create-ep-time'
195 selectedEnum={time && time.choice}
196 otherValue={time && time.other}
197 values={EntitlementPoolsOptionsInputValues.TIME}
198 isValid={genericFieldInfo.time.isValid}
199 errorText={genericFieldInfo.time.errorText} />
201 <GridItem colSpan={2}>
203 onChange={increments => onDataChanged({increments}, SP_ENTITLEMENT_POOL_FORM)}
204 label={i18n('Increments')}
206 data-test-id='create-ep-increments'
213 class EntitlementPoolsEditorView extends React.Component {
216 data: EntitlementPoolPropType,
217 previousData: EntitlementPoolPropType,
218 EPNames: React.PropTypes.object,
219 isReadOnlyMode: React.PropTypes.bool,
220 onDataChanged: React.PropTypes.func.isRequired,
221 onSubmit: React.PropTypes.func.isRequired,
222 onCancel: React.PropTypes.func.isRequired
225 static defaultProps = {
230 let {data = {}, onDataChanged, isReadOnlyMode, genericFieldInfo} = this.props;
236 genericFieldInfo && <Form
239 onSubmit={ () => this.submit() }
240 onReset={ () => this.props.onCancel() }
242 isReadOnlyMode={isReadOnlyMode}
243 isValid={this.props.isFormValid}
244 formReady={this.props.formReady}
245 onValidateForm={() => this.props.onValidateForm(SP_ENTITLEMENT_POOL_FORM) }
246 className='entitlement-pools-form'>
247 <EntitlementPoolsFormContent
249 genericFieldInfo={genericFieldInfo}
250 onDataChanged={onDataChanged}
251 validateName={(value)=> this.validateName(value)}
252 validateTimeOtherValue ={(value)=> this.validateTimeOtherValue(value)}
253 validateChoiceWithOther={(value)=> this.validateChoiceWithOther(value)}
254 thresholdValueValidation={(value, state)=> this.thresholdValueValidation(value, state)}/>
262 const {data: entitlementPool, previousData: previousEntitlementPool} = this.props;
263 this.props.onSubmit({entitlementPool, previousEntitlementPool});
266 validateName(value) {
267 const {data: {id}, EPNames} = this.props;
268 const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: EPNames});
270 return !isExists ? {isValid: true, errorText: ''} :
271 {isValid: false, errorText: i18n('Entitlement pool by the name \'' + value + '\' already exists. Entitlement pool name must be unique')};
274 validateTimeOtherValue(value) {
275 return Validator.validate('time', value.other, [{type: 'required', data: true}, {type: 'numeric', data: true}]);
278 validateChoiceWithOther(value) {
279 let chosen = value.choice;
280 // if we have an empty multiple select we have a problem since it's required
282 if (value.choices.length === 0) {
283 return Validator.validate('field', '', [{type: 'required', data: true}]);
285 // continuing validation with the first chosen value in case we have the 'Other' field
286 chosen = value.choices[0];
289 if (chosen !== optionInputOther.OTHER) {
290 return Validator.validate('field', chosen, [{type: 'required', data: true}]);
291 } else { // when 'Other' was chosen, validate other value
292 return Validator.validate('field', value.other, [{type: 'required', data: true}]);
296 thresholdValueValidation(value, state) {
298 let unit = state.data.thresholdUnits;
299 if (unit === thresholdUnitType.PERCENTAGE) {
300 return Validator.validate('thresholdValue', value, [
301 {type: 'required', data: true},
302 {type: 'numeric', data: true},
303 {type: 'maximum', data: 100},
304 {type: 'minimum', data: 0}]);
306 return Validator.validate('thresholdValue', value, [
307 {type: 'numeric', data: true},
308 {type: 'required', data: true}]);
314 export default EntitlementPoolsEditorView;