2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 import React from 'react';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
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 {SP_CREATION_FORM_NAME} from './SoftwareProductCreationConstants.js';
22 import sortByStringProperty from 'nfvo-utils/sortByStringProperty.js';
24 import SoftwareProductCategoriesHelper from 'sdc-app/onboarding/softwareProduct/SoftwareProductCategoriesHelper.js';
26 const SoftwareProductPropType = React.PropTypes.shape({
27 id: React.PropTypes.string,
28 name: React.PropTypes.string,
29 description: React.PropTypes.string,
30 category: React.PropTypes.string,
31 subCategory: React.PropTypes.string,
32 vendorId: React.PropTypes.string
35 class SoftwareProductCreationView extends React.Component {
38 data: SoftwareProductPropType,
39 finalizedLicenseModelList: React.PropTypes.array,
40 softwareProductCategories: React.PropTypes.array,
41 VSPNames: React.PropTypes.object,
42 onDataChanged: React.PropTypes.func.isRequired,
43 onSubmit: React.PropTypes.func.isRequired,
44 onCancel: React.PropTypes.func.isRequired
48 let {softwareProductCategories, data = {}, onDataChanged, onCancel, genericFieldInfo, disableVendor} = this.props;
49 let {name, description, vendorId, subCategory} = data;
51 const vendorList = this.getVendorList();
53 <div className='software-product-creation-page'>
54 { genericFieldInfo && <Form
55 ref={(validationForm) => this.validationForm = validationForm}
57 onSubmit={() => this.submit() }
58 onReset={() => onCancel() }
60 isValid={this.props.isFormValid}
61 formReady={this.props.formReady}
62 onValidateForm={() => this.validate() }>
63 <div className='software-product-form-row'>
64 <div className='software-product-inline-section'>
69 onChange={name => onDataChanged({name},SP_CREATION_FORM_NAME, {name: name => this.validateName(name)})}
70 isValid={genericFieldInfo.name.isValid}
71 errorText={genericFieldInfo.name.errorText}
73 className='field-section'
74 data-test-id='new-vsp-name' />
76 label={i18n('Vendor')}
80 disabled={disableVendor}
81 onChange={e => this.onSelectVendor(e)}
82 isValid={genericFieldInfo.vendorId.isValid}
83 errorText={genericFieldInfo.vendorId.errorText}
84 className='input-options-select'
85 groupClassName='bootstrap-input-options'
86 data-test-id='new-vsp-vendor' >
87 {vendorList.map(vendor =>
88 <option key={vendor.title} value={vendor.enum}>{vendor.title}</option>)}
91 label={i18n('Category')}
95 onChange={e => this.onSelectSubCategory(e)}
96 isValid={genericFieldInfo.subCategory.isValid}
97 errorText={genericFieldInfo.subCategory.errorText}
98 className='input-options-select'
99 groupClassName='bootstrap-input-options'
100 data-test-id='new-vsp-category' >
101 <option key='' value=''>{i18n('please select…')}</option>
102 {softwareProductCategories.map(category =>
103 category.subcategories &&
106 label={category.name}>{category.subcategories.map(sub =>
107 <option key={sub.uniqueId} value={sub.uniqueId}>{`${sub.name} (${category.name})`}</option>)}
112 <div className='software-product-inline-section'>
115 label={i18n('Description')}
118 onChange={description => onDataChanged({description},SP_CREATION_FORM_NAME)}
119 isValid={genericFieldInfo.description.isValid}
120 errorText={genericFieldInfo.description.errorText}
122 className='field-section'
123 data-test-id='new-vsp-description' />
132 let {finalizedLicenseModelList} = this.props;
134 return [{enum: '', title: i18n('please select...')}].concat(
135 sortByStringProperty(finalizedLicenseModelList, 'vendorName').map(vendor => {
138 title: vendor.vendorName
145 const selectedIndex = e.target.selectedIndex;
146 const vendorId = e.target.options[selectedIndex].value;
147 this.props.onDataChanged({vendorId},SP_CREATION_FORM_NAME);
150 onSelectSubCategory(e) {
151 const selectedIndex = e.target.selectedIndex;
152 const subCategory = e.target.options[selectedIndex].value;
153 let {softwareProductCategories, onDataChanged} = this.props;
154 let category = SoftwareProductCategoriesHelper.getCurrentCategoryOfSubCategory(subCategory, softwareProductCategories);
155 onDataChanged({category, subCategory},SP_CREATION_FORM_NAME);
159 let {data:softwareProduct, finalizedLicenseModelList} = this.props;
160 softwareProduct.vendorName = finalizedLicenseModelList.find(vendor => vendor.id === softwareProduct.vendorId).vendorName;
161 this.props.onSubmit(softwareProduct);
164 validateName(value) {
165 const {data: {id}, VSPNames} = this.props;
166 const isExists = Validator.isItemNameAlreadyExistsInList({itemId: id, itemName: value, list: VSPNames});
168 return !isExists ? {isValid: true, errorText: ''} :
169 {isValid: false, errorText: i18n('Software product by the name \'' + value + '\' already exists. Software product name must be unique')};
173 this.props.onValidateForm(SP_CREATION_FORM_NAME);
177 export default SoftwareProductCreationView;