[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / creation / LicenseModelCreationView.jsx
1 /*!
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  */
16 import React from 'react';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
18 import Validator from 'nfvo-utils/Validator.js';
19 import Input from 'nfvo-components/input/validation/Input.jsx';
20 import Form from 'nfvo-components/input/validation/Form.jsx';
21 import {LICENSE_MODEL_CREATION_FORM_NAME} from './LicenseModelCreationConstants.js';
22
23 const LicenseModelPropType = React.PropTypes.shape({
24         id: React.PropTypes.string,
25         vendorName: React.PropTypes.string,
26         description: React.PropTypes.string
27 });
28
29 class LicenseModelCreationView extends React.Component {
30
31         static propTypes = {
32                 data: LicenseModelPropType,
33                 VLMNames: React.PropTypes.object,
34                 onDataChanged: React.PropTypes.func.isRequired,
35                 onSubmit: React.PropTypes.func.isRequired,
36                 onValidateForm: React.PropTypes.func.isRequired,
37                 onCancel: React.PropTypes.func.isRequired
38         };
39
40         render() {
41                 let {data = {}, onDataChanged, genericFieldInfo} = this.props;
42                 let {vendorName, description} = data;
43                 return (
44                         <div>
45                                 {genericFieldInfo && <Form
46                                         ref='validationForm'
47                                         hasButtons={true}
48                                         onSubmit={ () => this.submit() }
49                                         onReset={ () => this.props.onCancel() }
50                                         labledButtons={true}
51                                         isValid={this.props.isFormValid}
52                                         formReady={this.props.formReady}
53                                         onValidateForm={() => this.validate() }>
54                                         <Input
55                                                 value={vendorName}
56                                                 label={i18n('Vendor Name')}
57                                                 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}
61                                                 type='text'
62                                                 isRequired={true}
63                                                 className='field-section'/>
64                                         <Input
65                                                 isRequired={true}
66                                                 value={description}
67                                                 label={i18n('Description')}
68                                                 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}
73                                                 type='textarea'
74                                                 className='field-section'/>
75                                 </Form>}
76                         </div>
77                 );
78         }
79
80
81         submit() {
82                 const {data:licenseModel} = this.props;
83                 this.props.onSubmit(licenseModel);
84         }
85
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         }
97 }
98
99 export default LicenseModelCreationView;