[SDC-29] rebase continue work to align source
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / attachments / SoftwareProductAttachmentsView.jsx
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  */
16 import React, {Component, PropTypes} from 'react';
17 import Tabs from 'react-bootstrap/lib/Tabs.js';
18 import Tab from 'react-bootstrap/lib/Tab.js';
19 import {tabsMapping} from './SoftwareProductAttachmentsConstants.js';
20 import i18n from 'nfvo-utils/i18n/i18n.js';
21 import Icon from 'nfvo-components/icon/Icon.jsx';
22 import HeatValidation from './validation/HeatValidation.js';
23
24 class HeatScreenView extends Component {
25
26         static propTypes = {
27                 isValidationAvailable: PropTypes.bool,
28                 goToOverview: PropTypes.bool
29         };
30
31         state = {
32                 activeTab: tabsMapping.SETUP
33         };
34
35         render() {
36                 let {isValidationAvailable, isReadOnlyMode, heatDataExist, onDownload, softwareProductId, onProcessAndValidate, heatSetup, HeatSetupComponent, onGoToOverview, version, ...other} = this.props;
37                 return (
38                         <div className='vsp-attachments-view'>
39                                 <div className='attachments-view-controllers'>
40                                         {(this.state.activeTab === tabsMapping.SETUP) &&
41                                                 <Icon
42                                                         iconClassName={heatDataExist ? '' : 'disabled'}
43                                                         className={heatDataExist ? '' : 'disabled'}
44                                                         image='download'
45                                                         label={i18n('Download HEAT')}
46                                                         onClick={heatDataExist ? () => onDownload({heatCandidate: heatSetup, isReadOnlyMode, version}) : undefined}
47                                                         data-test-id='download-heat'/>}
48                                         {(this.state.activeTab === tabsMapping.VALIDATION && softwareProductId) &&
49                                                 <Icon
50                                                         iconClassName={this.props.goToOverview ? '' : 'disabled'}
51                                                         className={`go-to-overview-icon ${this.props.goToOverview ? '' : 'disabled'}`}
52                                                         labelClassName='go-to-overview-label'
53                                                         onClick={this.props.goToOverview ? () => onGoToOverview({version}) : undefined}
54                                                         image='go-to-overview'
55                                                         label={i18n('Go to Overview')}
56                                                         data-test-id='go-to-overview'/>}
57                                         <Icon
58                                                 image='upload'
59                                                 label={i18n('Upload New HEAT')}
60                                                 className={isReadOnlyMode ? 'disabled' : ''}
61                                                 iconClassName={isReadOnlyMode ? 'disabled' : ''}
62                                                 onClick={evt => {this.refs.hiddenImportFileInput.click(evt);}}
63                                                 data-test-id='upload-heat'/>
64                                         <input
65                                                 ref='hiddenImportFileInput'
66                                                 type='file'
67                                                 name='fileInput'
68                                                 accept='.zip'
69                                                 onChange={evt => this.handleImport(evt)}/>
70                                 </div>
71                                 <Tabs id='attachments-tabs' activeKey={this.state.activeTab} onSelect={key => this.handleTabPress(key)}>
72                                         <Tab  eventKey={tabsMapping.SETUP} title='HEAT Setup'>
73                                                 <HeatSetupComponent
74                                                         heatDataExist={heatDataExist}
75                                                         changeAttachmentsTab={tab => this.setState({activeTab: tab})}
76                                                         onProcessAndValidate={onProcessAndValidate}
77                                                         softwareProductId={softwareProductId}
78                                                         isReadOnlyMode={isReadOnlyMode}
79                                                         version={version}/>
80                                         </Tab>
81                                         <Tab eventKey={tabsMapping.VALIDATION} title='Heat Validation' disabled={!isValidationAvailable}>
82                                                 <HeatValidation {...other}/>
83                                         </Tab>
84                                 </Tabs>
85                         </div>
86                 );
87         }
88
89         handleTabPress(key) {
90                 let {heatSetup, heatSetupCache, onProcessAndValidate, isReadOnlyMode, version} = this.props;            
91                 switch (key) {
92                         case tabsMapping.VALIDATION:                            
93                                 onProcessAndValidate({heatData: heatSetup, heatDataCache: heatSetupCache, isReadOnlyMode, version}).then(
94                                         () => this.setState({activeTab: tabsMapping.VALIDATION})
95                                 );
96                                 return;
97                         case tabsMapping.SETUP:
98                                 this.setState({activeTab: tabsMapping.SETUP});
99                                 return;
100                 }
101         }
102
103         handleImport(evt) {
104                 evt.preventDefault();
105                 let {version} = this.props;
106                 let formData = new FormData();
107                 formData.append('upload', this.refs.hiddenImportFileInput.files[0]);
108                 this.refs.hiddenImportFileInput.value = '';
109                 this.props.onUpload(formData, version);
110                 this.setState({activeTab: tabsMapping.SETUP});
111         }
112
113         save() {
114                 return this.props.onSave(this.props.heatSetup, this.props.version);
115         }
116
117 }
118
119 export default HeatScreenView;