blob: 59c4152213a4b239157c3e6df3a8e600860af868 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022import { 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({
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 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 {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 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 };
Michael Landoefa037d2017-02-19 12:57:33 +020040
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 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 }
Michael Landoefa037d2017-02-19 12:57:33 +020099
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 submit() {
101 const { data: licenseModel, usersList } = this.props;
102 this.props.onSubmit(licenseModel, usersList);
103 }
Michael Landoefa037d2017-02-19 12:57:33 +0200104
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200105 validateName(value) {
106 const { data: { id }, VLMNames } = this.props;
107 const isExists = Validator.isItemNameAlreadyExistsInList({
108 itemId: id,
109 itemName: value,
110 list: VLMNames
111 });
Michael Landoefa037d2017-02-19 12:57:33 +0200112
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200113 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 }
AviZi280f8012017-06-09 02:39:56 +0300124
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 validate() {
126 this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME);
127 }
Michael Landoefa037d2017-02-19 12:57:33 +0200128}
129
130export default LicenseModelCreationView;