Adding Prettier and fixing up eslint version
[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 PropTypes from 'prop-types';
18 import i18n from 'nfvo-utils/i18n/i18n.js';
19 import Validator from 'nfvo-utils/Validator.js';
20 import Input from 'nfvo-components/input/validation/Input.jsx';
21 import Form from 'nfvo-components/input/validation/Form.jsx';
22 import { LICENSE_MODEL_CREATION_FORM_NAME } from './LicenseModelCreationConstants.js';
23
24 const LicenseModelPropType = PropTypes.shape({
25     id: PropTypes.string,
26     vendorName: PropTypes.string,
27     description: PropTypes.string
28 });
29
30 class LicenseModelCreationView extends React.Component {
31     static propTypes = {
32         data: LicenseModelPropType,
33         VLMNames: PropTypes.object,
34         usersList: PropTypes.array,
35         onDataChanged: PropTypes.func.isRequired,
36         onSubmit: PropTypes.func.isRequired,
37         onValidateForm: PropTypes.func.isRequired,
38         onCancel: PropTypes.func.isRequired
39     };
40
41     render() {
42         let { data = {}, onDataChanged, genericFieldInfo } = this.props;
43         let { vendorName, description } = data;
44         return (
45             <div>
46                 {genericFieldInfo && (
47                     <Form
48                         ref="validationForm"
49                         hasButtons={true}
50                         onSubmit={() => this.submit()}
51                         submitButtonText={i18n('Create')}
52                         onReset={() => this.props.onCancel()}
53                         labledButtons={true}
54                         isValid={this.props.isFormValid}
55                         formReady={this.props.formReady}
56                         onValidateForm={() => this.validate()}>
57                         <Input
58                             value={vendorName}
59                             label={i18n('Vendor Name')}
60                             data-test-id="vendor-name"
61                             onChange={vendorName =>
62                                 onDataChanged(
63                                     { vendorName },
64                                     LICENSE_MODEL_CREATION_FORM_NAME,
65                                     {
66                                         vendorName: name =>
67                                             this.validateName(name)
68                                     }
69                                 )
70                             }
71                             isValid={genericFieldInfo.vendorName.isValid}
72                             errorText={genericFieldInfo.vendorName.errorText}
73                             type="text"
74                             isRequired={true}
75                             className="field-section"
76                         />
77                         <Input
78                             isRequired={true}
79                             value={description}
80                             label={i18n('Description')}
81                             data-test-id="vendor-description"
82                             overlayPos="bottom"
83                             onChange={description =>
84                                 onDataChanged(
85                                     { description },
86                                     LICENSE_MODEL_CREATION_FORM_NAME
87                                 )
88                             }
89                             isValid={genericFieldInfo.description.isValid}
90                             errorText={genericFieldInfo.description.errorText}
91                             type="textarea"
92                             className="field-section"
93                         />
94                     </Form>
95                 )}
96             </div>
97         );
98     }
99
100     submit() {
101         const { data: licenseModel, usersList } = this.props;
102         this.props.onSubmit(licenseModel, usersList);
103     }
104
105     validateName(value) {
106         const { data: { id }, VLMNames } = this.props;
107         const isExists = Validator.isItemNameAlreadyExistsInList({
108             itemId: id,
109             itemName: value,
110             list: VLMNames
111         });
112
113         return !isExists
114             ? { isValid: true, errorText: '' }
115             : {
116                   isValid: false,
117                   errorText: i18n(
118                       "License model by the name '" +
119                           value +
120                           "' already exists. License model name must be unique"
121                   )
122               };
123     }
124
125     validate() {
126         this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME);
127     }
128 }
129
130 export default LicenseModelCreationView;