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'; |
| 17 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 33 | VLMNames: React.PropTypes.object, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | onDataChanged: React.PropTypes.func.isRequired, |
| 35 | onSubmit: React.PropTypes.func.isRequired, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 36 | onValidateForm: React.PropTypes.func.isRequired, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | onCancel: React.PropTypes.func.isRequired |
| 38 | }; |
| 39 | |
| 40 | render() { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 41 | let {data = {}, onDataChanged, genericFieldInfo} = this.props; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 42 | let {vendorName, description} = data; |
| 43 | return ( |
| 44 | <div> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 45 | {genericFieldInfo && <Form |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | ref='validationForm' |
| 47 | hasButtons={true} |
| 48 | onSubmit={ () => this.submit() } |
| 49 | onReset={ () => this.props.onCancel() } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 50 | labledButtons={true} |
| 51 | isValid={this.props.isFormValid} |
| 52 | formReady={this.props.formReady} |
| 53 | onValidateForm={() => this.validate() }> |
| 54 | <Input |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 55 | value={vendorName} |
| 56 | label={i18n('Vendor Name')} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 61 | type='text' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 62 | isRequired={true} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 63 | className='field-section'/> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 64 | <Input |
| 65 | isRequired={true} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 66 | value={description} |
| 67 | label={i18n('Description')} |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 73 | type='textarea' |
| 74 | className='field-section'/> |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 75 | </Form>} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | </div> |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | submit() { |
| 82 | const {data:licenseModel} = this.props; |
| 83 | this.props.onSubmit(licenseModel); |
| 84 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | export default LicenseModelCreationView; |