AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 17 | import PropTypes from 'prop-types'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 18 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | import { LICENSE_MODEL_CREATION_FORM_NAME } from './LicenseModelCreationConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 24 | const LicenseModelPropType = PropTypes.shape({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | id: PropTypes.string, |
| 26 | vendorName: PropTypes.string, |
| 27 | description: PropTypes.string |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 28 | }); |
| 29 | |
| 30 | class LicenseModelCreationView extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 40 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 100 | submit() { |
| 101 | const { data: licenseModel, usersList } = this.props; |
| 102 | this.props.onSubmit(licenseModel, usersList); |
| 103 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 104 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 112 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 124 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 125 | validate() { |
| 126 | this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME); |
| 127 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | export default LicenseModelCreationView; |