blob: 80040460c0e467497496ef16e1f7d17ddd95877e [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';
17import i18n from 'nfvo-utils/i18n/i18n.js';
AviZi280f8012017-06-09 02:39:56 +030018import Validator from 'nfvo-utils/Validator.js';
19import Input from 'nfvo-components/input/validation/Input.jsx';
20import Form from 'nfvo-components/input/validation/Form.jsx';
21import {LICENSE_MODEL_CREATION_FORM_NAME} from './LicenseModelCreationConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022
23const LicenseModelPropType = React.PropTypes.shape({
24 id: React.PropTypes.string,
25 vendorName: React.PropTypes.string,
26 description: React.PropTypes.string
27});
28
29class LicenseModelCreationView extends React.Component {
30
31 static propTypes = {
32 data: LicenseModelPropType,
AviZi280f8012017-06-09 02:39:56 +030033 VLMNames: React.PropTypes.object,
Michael Landoefa037d2017-02-19 12:57:33 +020034 onDataChanged: React.PropTypes.func.isRequired,
35 onSubmit: React.PropTypes.func.isRequired,
AviZi280f8012017-06-09 02:39:56 +030036 onValidateForm: React.PropTypes.func.isRequired,
Michael Landoefa037d2017-02-19 12:57:33 +020037 onCancel: React.PropTypes.func.isRequired
38 };
39
40 render() {
AviZi280f8012017-06-09 02:39:56 +030041 let {data = {}, onDataChanged, genericFieldInfo} = this.props;
Michael Landoefa037d2017-02-19 12:57:33 +020042 let {vendorName, description} = data;
43 return (
44 <div>
AviZi280f8012017-06-09 02:39:56 +030045 {genericFieldInfo && <Form
Michael Landoefa037d2017-02-19 12:57:33 +020046 ref='validationForm'
47 hasButtons={true}
48 onSubmit={ () => this.submit() }
49 onReset={ () => this.props.onCancel() }
AviZi280f8012017-06-09 02:39:56 +030050 labledButtons={true}
51 isValid={this.props.isFormValid}
52 formReady={this.props.formReady}
53 onValidateForm={() => this.validate() }>
54 <Input
Michael Landoefa037d2017-02-19 12:57:33 +020055 value={vendorName}
56 label={i18n('Vendor Name')}
AviZi280f8012017-06-09 02:39:56 +030057 data-test-id='vendor-name'
58 onChange={vendorName => onDataChanged({vendorName}, LICENSE_MODEL_CREATION_FORM_NAME, {vendorName: name => this.validateName(name)})}
59 isValid={genericFieldInfo.vendorName.isValid}
60 errorText={genericFieldInfo.vendorName.errorText}
Michael Landoefa037d2017-02-19 12:57:33 +020061 type='text'
AviZi280f8012017-06-09 02:39:56 +030062 isRequired={true}
Michael Landoefa037d2017-02-19 12:57:33 +020063 className='field-section'/>
AviZi280f8012017-06-09 02:39:56 +030064 <Input
65 isRequired={true}
Michael Landoefa037d2017-02-19 12:57:33 +020066 value={description}
67 label={i18n('Description')}
AviZi280f8012017-06-09 02:39:56 +030068 data-test-id='vendor-description'
69 overlayPos='bottom'
70 onChange={description => onDataChanged({description}, LICENSE_MODEL_CREATION_FORM_NAME)}
71 isValid={genericFieldInfo.description.isValid}
72 errorText={genericFieldInfo.description.errorText}
Michael Landoefa037d2017-02-19 12:57:33 +020073 type='textarea'
74 className='field-section'/>
AviZi280f8012017-06-09 02:39:56 +030075 </Form>}
Michael Landoefa037d2017-02-19 12:57:33 +020076 </div>
77 );
78 }
79
80
81 submit() {
82 const {data:licenseModel} = this.props;
83 this.props.onSubmit(licenseModel);
84 }
AviZi280f8012017-06-09 02:39:56 +030085
86 validateName(value) {
87 const {data: {id}, VLMNames} = this.props;
88 const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: VLMNames});
89
90 return !isExists ? {isValid: true, errorText: ''} :
91 {isValid: false, errorText: i18n('License model by the name \'' + value + '\' already exists. License model name must be unique')};
92 }
93
94 validate() {
95 this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME);
96 }
Michael Landoefa037d2017-02-19 12:57:33 +020097}
98
99export default LicenseModelCreationView;