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'; |
| 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({ |
| 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 { |
| 31 | |
| 32 | static propTypes = { |
| 33 | data: LicenseModelPropType, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 34 | VLMNames: PropTypes.object, |
| 35 | usersList: PropTypes.array, |
| 36 | onDataChanged: PropTypes.func.isRequired, |
| 37 | onSubmit: PropTypes.func.isRequired, |
| 38 | onValidateForm: PropTypes.func.isRequired, |
| 39 | onCancel: PropTypes.func.isRequired |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | render() { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 43 | let {data = {}, onDataChanged, genericFieldInfo} = this.props; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 44 | let {vendorName, description} = data; |
| 45 | return ( |
| 46 | <div> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | {genericFieldInfo && <Form |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 48 | ref='validationForm' |
| 49 | hasButtons={true} |
| 50 | onSubmit={ () => this.submit() } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 51 | submitButtonText={i18n('Create')} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 52 | onReset={ () => this.props.onCancel() } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | labledButtons={true} |
| 54 | isValid={this.props.isFormValid} |
| 55 | formReady={this.props.formReady} |
| 56 | onValidateForm={() => this.validate() }> |
| 57 | <Input |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 58 | value={vendorName} |
| 59 | label={i18n('Vendor Name')} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 60 | data-test-id='vendor-name' |
| 61 | onChange={vendorName => onDataChanged({vendorName}, LICENSE_MODEL_CREATION_FORM_NAME, {vendorName: name => this.validateName(name)})} |
| 62 | isValid={genericFieldInfo.vendorName.isValid} |
| 63 | errorText={genericFieldInfo.vendorName.errorText} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 64 | type='text' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 65 | isRequired={true} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 66 | className='field-section'/> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 67 | <Input |
| 68 | isRequired={true} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 69 | value={description} |
| 70 | label={i18n('Description')} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 71 | data-test-id='vendor-description' |
| 72 | overlayPos='bottom' |
| 73 | onChange={description => onDataChanged({description}, LICENSE_MODEL_CREATION_FORM_NAME)} |
| 74 | isValid={genericFieldInfo.description.isValid} |
| 75 | errorText={genericFieldInfo.description.errorText} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | type='textarea' |
| 77 | className='field-section'/> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | </Form>} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 79 | </div> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | submit() { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 85 | const {data:licenseModel, usersList} = this.props; |
| 86 | this.props.onSubmit(licenseModel, usersList); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 87 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 88 | |
| 89 | validateName(value) { |
| 90 | const {data: {id}, VLMNames} = this.props; |
| 91 | const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: VLMNames}); |
| 92 | |
| 93 | return !isExists ? {isValid: true, errorText: ''} : |
| 94 | {isValid: false, errorText: i18n('License model by the name \'' + value + '\' already exists. License model name must be unique')}; |
| 95 | } |
| 96 | |
| 97 | validate() { |
| 98 | this.props.onValidateForm(LICENSE_MODEL_CREATION_FORM_NAME); |
| 99 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | export default LicenseModelCreationView; |