Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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 | * |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 15 | */ |
| 16 | import React from 'react'; |
| 17 | import PropTypes from 'prop-types'; |
| 18 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 19 | import Input from 'nfvo-components/input/validation/Input.jsx'; |
| 20 | import Form from 'nfvo-components/input/validation/Form.jsx'; |
| 21 | |
| 22 | const VersionPropType = PropTypes.shape({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 23 | name: PropTypes.string, |
| 24 | description: PropTypes.string, |
| 25 | creationMethod: PropTypes.string |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 26 | }); |
| 27 | |
| 28 | class VersionsPageCreationView extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | static propTypes = { |
| 30 | data: VersionPropType, |
| 31 | availableMethods: PropTypes.array, |
| 32 | onDataChanged: PropTypes.func.isRequired, |
| 33 | onSubmit: PropTypes.func.isRequired, |
| 34 | onCancel: PropTypes.func.isRequired |
| 35 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 36 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 37 | render() { |
| 38 | let { |
| 39 | data = {}, |
| 40 | genericFieldInfo, |
| 41 | baseVersion, |
| 42 | onDataChanged, |
| 43 | onCancel |
| 44 | } = this.props; |
| 45 | let { additionalInfo: { OptionalCreationMethods } } = baseVersion; |
| 46 | let { description, creationMethod } = data; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 47 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | return ( |
| 49 | <div className="version-creation-page"> |
| 50 | {genericFieldInfo && ( |
| 51 | <Form |
| 52 | ref={validationForm => |
| 53 | (this.validationForm = validationForm) |
| 54 | } |
| 55 | hasButtons={true} |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 56 | btnClassName="sdc-modal__footer" |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 57 | onSubmit={() => this.submit()} |
| 58 | submitButtonText={i18n('Create')} |
| 59 | onReset={() => onCancel()} |
| 60 | labledButtons={true} |
| 61 | isValid={this.props.isFormValid} |
| 62 | formReady={this.props.formReady} |
| 63 | onValidateForm={() => this.validate()}> |
| 64 | <div className="version-form-row"> |
| 65 | <Input |
| 66 | label={i18n('Version Category')} |
| 67 | value={creationMethod} |
| 68 | onChange={e => this.onSelectMethod(e)} |
| 69 | type="select" |
| 70 | overlayPos="bottom" |
| 71 | data-test-id="new-version-category" |
| 72 | isValid={ |
| 73 | genericFieldInfo.creationMethod.isValid |
| 74 | } |
| 75 | errorText={ |
| 76 | genericFieldInfo.creationMethod.errorText |
| 77 | } |
| 78 | isRequired> |
| 79 | <option key="" value=""> |
| 80 | {i18n('Please select…')} |
| 81 | </option> |
| 82 | {OptionalCreationMethods.map(method => ( |
| 83 | <option key={method} value={method}> |
| 84 | {i18n(method)} |
| 85 | </option> |
| 86 | ))} |
| 87 | </Input> |
| 88 | </div> |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 89 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 90 | <div className="version-form-row"> |
| 91 | <Input |
| 92 | label={i18n('Description')} |
| 93 | value={description} |
| 94 | type="text" |
| 95 | overlayPos="bottom" |
| 96 | data-test-id="new-version-description" |
| 97 | isValid={genericFieldInfo.description.isValid} |
| 98 | errorText={ |
| 99 | genericFieldInfo.description.errorText |
| 100 | } |
| 101 | onChange={description => |
| 102 | onDataChanged({ description }) |
| 103 | } |
| 104 | isRequired |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 105 | groupClassName="no-bottom-margin" |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 106 | /> |
| 107 | </div> |
| 108 | </Form> |
| 109 | )} |
| 110 | </div> |
| 111 | ); |
| 112 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 113 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 114 | onSelectMethod(e) { |
| 115 | const selectedIndex = e.target.selectedIndex; |
| 116 | const creationMethod = e.target.options[selectedIndex].value; |
| 117 | this.props.onDataChanged({ creationMethod }); |
| 118 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 119 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 120 | submit() { |
| 121 | let { baseVersion, data: { description, creationMethod } } = this.props; |
| 122 | this.props.onSubmit({ |
| 123 | baseVersion, |
| 124 | payload: { description, creationMethod } |
| 125 | }); |
| 126 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 127 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 128 | validate() { |
| 129 | this.props.onValidateForm(); |
| 130 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | export default VersionsPageCreationView; |