blob: 948bdc158db74302fc2f783b20bbea0e443b8585 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +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 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import React from 'react';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
Michael Landoefa037d2017-02-19 12:57:33 +020018import i18n from 'nfvo-utils/i18n/i18n.js';
AviZi280f8012017-06-09 02:39:56 +030019import Validator from 'nfvo-utils/Validator.js';
20import Input from 'nfvo-components/input/validation/Input.jsx';
21import Form from 'nfvo-components/input/validation/Form.jsx';
22import {LICENSE_MODEL_CREATION_FORM_NAME} from './LicenseModelCreationConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020023
talig8e9c0652017-12-20 14:30:43 +020024const LicenseModelPropType = PropTypes.shape({
25 id: PropTypes.string,
26 vendorName: PropTypes.string,
27 description: PropTypes.string
Michael Landoefa037d2017-02-19 12:57:33 +020028});
29
30class LicenseModelCreationView extends React.Component {
31
32 static propTypes = {
33 data: LicenseModelPropType,
talig8e9c0652017-12-20 14:30:43 +020034 VLMNames: PropTypes.object,
35 usersList: PropTypes.array,
36 onDataChanged: PropTypes.func.isRequired,
37 onSubmit: PropTypes.func.isRequired,
38 onValidateForm: PropTypes.func.isRequired,
39 onCancel: PropTypes.func.isRequired
Michael Landoefa037d2017-02-19 12:57:33 +020040 };
41
42 render() {
AviZi280f8012017-06-09 02:39:56 +030043 let {data = {}, onDataChanged, genericFieldInfo} = this.props;
Michael Landoefa037d2017-02-19 12:57:33 +020044 let {vendorName, description} = data;
45 return (
46 <div>
AviZi280f8012017-06-09 02:39:56 +030047 {genericFieldInfo && <Form
Michael Landoefa037d2017-02-19 12:57:33 +020048 ref='validationForm'
49 hasButtons={true}
50 onSubmit={ () => this.submit() }
talig8e9c0652017-12-20 14:30:43 +020051 submitButtonText={i18n('Create')}
Michael Landoefa037d2017-02-19 12:57:33 +020052 onReset={ () => this.props.onCancel() }
AviZi280f8012017-06-09 02:39:56 +030053 labledButtons={true}
54 isValid={this.props.isFormValid}
55 formReady={this.props.formReady}
56 onValidateForm={() => this.validate() }>
57 <Input
Michael Landoefa037d2017-02-19 12:57:33 +020058 value={vendorName}
59 label={i18n('Vendor Name')}
AviZi280f8012017-06-09 02:39:56 +030060 data-test-id='vendor-name'
61 onChange={vendorName => onDataChanged({vendorName}, LICENSE_MODEL_CREATION_FORM_NAME, {vendorName: name => this.validateName(name)})}
62 isValid={genericFieldInfo.vendorName.isValid}
63 errorText={genericFieldInfo.vendorName.errorText}
Michael Landoefa037d2017-02-19 12:57:33 +020064 type='text'
AviZi280f8012017-06-09 02:39:56 +030065 isRequired={true}
Michael Landoefa037d2017-02-19 12:57:33 +020066 className='field-section'/>
AviZi280f8012017-06-09 02:39:56 +030067 <Input
68 isRequired={true}
Michael Landoefa037d2017-02-19 12:57:33 +020069 value={description}
70 label={i18n('Description')}
AviZi280f8012017-06-09 02:39:56 +030071 data-test-id='vendor-description'
72 overlayPos='bottom'
73 onChange={description => onDataChanged({description}, LICENSE_MODEL_CREATION_FORM_NAME)}
74 isValid={genericFieldInfo.description.isValid}
75 errorText={genericFieldInfo.description.errorText}
Michael Landoefa037d2017-02-19 12:57:33 +020076 type='textarea'
77 className='field-section'/>
AviZi280f8012017-06-09 02:39:56 +030078 </Form>}
Michael Landoefa037d2017-02-19 12:57:33 +020079 </div>
80 );
81 }
82
83
84 submit() {
talig8e9c0652017-12-20 14:30:43 +020085 const {data:licenseModel, usersList} = this.props;
86 this.props.onSubmit(licenseModel, usersList);
Michael Landoefa037d2017-02-19 12:57:33 +020087 }
AviZi280f8012017-06-09 02:39:56 +030088
89 validateName(value) {
90 const {data: {id}, VLMNames} = this.props;
91 const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: VLMNames});
92
93 return !isExists ? {isValid: true, errorText: ''} :
94 {isValid: false, errorText: i18n('License model by the name \'' + value + '\' already exists. License model name must be unique')};
95 }
96
97 validate() {
98 this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME);
99 }
Michael Landoefa037d2017-02-19 12:57:33 +0200100}
101
102export default LicenseModelCreationView;