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';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
18 import Validator from 'nfvo-utils/Validator.js';
20 import Form from 'nfvo-components/input/validation/Form.jsx';
21 import Input from 'nfvo-components/input/validation/Input.jsx';
22 import GridSection from 'nfvo-components/grid/GridSection.jsx';
23 import GridItem from 'nfvo-components/grid/GridItem.jsx';
24 import {optionsInputValues as licenseKeyGroupOptionsInputValues, LKG_FORM_NAME} from './LicenseKeyGroupsConstants.js';
25 import {other as optionInputOther} from 'nfvo-components/input/validation/InputOptions.jsx';
26 import InputOptions from 'nfvo-components/input/validation/InputOptions.jsx';
28 const LicenseKeyGroupPropType = React.PropTypes.shape({
29 id: React.PropTypes.string,
30 name: React.PropTypes.string,
31 description: React.PropTypes.string,
32 operationalScope: React.PropTypes.shape({
33 choices: React.PropTypes.array,
34 other: React.PropTypes.string
36 type: React.PropTypes.string
39 const LicenseKeyGroupFormContent = ({data, onDataChanged, genericFieldInfo, validateName, validateOperationalScope}) => {
40 let {name, description, operationalScope, type} = data;
43 <GridItem colSpan={2}>
45 onChange={name => onDataChanged({name}, LKG_FORM_NAME, {name: validateName})}
47 data-test-id='create-lkg-name'
49 isValid={genericFieldInfo.name.isValid}
50 errorText={genericFieldInfo.name.errorText}
54 <GridItem colSpan={2}>
56 onInputChange={()=>{}}
59 onEnumChange={operationalScope => onDataChanged({operationalScope:{choices: operationalScope, other: ''}},
60 LKG_FORM_NAME, {operationalScope: validateOperationalScope})}
61 onOtherChange={operationalScope => onDataChanged({operationalScope:{choices: [optionInputOther.OTHER],
62 other: operationalScope}}, LKG_FORM_NAME, {operationalScope: validateOperationalScope})}
63 label={i18n('Operational Scope')}
64 data-test-id='create-lkg-operational-scope'
66 multiSelectedEnum={operationalScope && operationalScope.choices}
67 otherValue={operationalScope && operationalScope.other}
68 values={licenseKeyGroupOptionsInputValues.OPERATIONAL_SCOPE}
69 isValid={genericFieldInfo.operationalScope.isValid}
70 errorText={genericFieldInfo.operationalScope.errorText} />
72 <GridItem colSpan={2}>
74 onChange={description => onDataChanged({description}, LKG_FORM_NAME)}
75 label={i18n('Description')}
76 data-test-id='create-lkg-description'
78 isValid={genericFieldInfo.description.isValid}
79 errorText={genericFieldInfo.description.errorText}
82 overlayPos='bottom' />
84 <GridItem colSpan={2}>
87 onChange={e => { const selectedIndex = e.target.selectedIndex;
88 const val = e.target.options[selectedIndex].value;
89 onDataChanged({type: val}, LKG_FORM_NAME);}}
92 data-test-id='create-lkg-type'
93 isValid={genericFieldInfo.type.isValid}
94 errorText={genericFieldInfo.type.errorText}
95 groupClassName='bootstrap-input-options'
96 className='input-options-select'
99 licenseKeyGroupOptionsInputValues.TYPE.map(type =>
100 (<option key={type.enum} value={type.enum}>{type.title}</option>))
108 class LicenseKeyGroupsEditorView extends React.Component {
110 data: LicenseKeyGroupPropType,
111 previousData: LicenseKeyGroupPropType,
112 LKGNames: React.PropTypes.object,
113 isReadOnlyMode: React.PropTypes.bool,
114 onDataChanged: React.PropTypes.func.isRequired,
115 onSubmit: React.PropTypes.func.isRequired,
116 onCancel: React.PropTypes.func.isRequired
119 static defaultProps = {
124 let {data = {}, onDataChanged, isReadOnlyMode, genericFieldInfo} = this.props;
127 { genericFieldInfo &&
131 onSubmit={ () => this.submit() }
132 onReset={ () => this.props.onCancel() }
133 isValid={this.props.isFormValid}
134 formReady={this.props.formReady}
135 onValidateForm={() => this.props.onValidateForm(LKG_FORM_NAME) }
137 isReadOnlyMode={isReadOnlyMode}
138 className='license-key-groups-form'>
139 <LicenseKeyGroupFormContent
141 onDataChanged={onDataChanged}
142 genericFieldInfo={genericFieldInfo}
143 validateName={(value)=> this.validateName(value)}
144 validateOperationalScope={this.validateOperationalScope}/>
151 const {data: licenseKeyGroup, previousData: previousLicenseKeyGroup} = this.props;
152 this.props.onSubmit({licenseKeyGroup, previousLicenseKeyGroup});
155 validateName(value) {
156 const {data: {id}, LKGNames} = this.props;
157 const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: LKGNames});
159 return !isExists ? {isValid: true, errorText: ''} :
160 {isValid: false, errorText: i18n('License key group by the name \'' + value + '\' already exists. License key group name must be unique')};
163 validateOperationalScope(value) {
164 if (value && value.choices && value.choices.length > 0) {
165 if (value.choices[0] !== optionInputOther.OTHER)
182 errorText: 'Field is required'
187 export default LicenseKeyGroupsEditorView;