Add collaboration feature
[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} from 'react';
17 import PropTypes from 'prop-types';
18 import accept from 'attr-accept';
19 import {SVGIcon, Tab, Tabs} from 'sdc-ui/lib/react';
20 import {tabsMapping} from './SoftwareProductAttachmentsConstants.js';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import HeatValidation from './validation/HeatValidation.js';
23 import {onboardingOriginTypes} from 'sdc-app/onboarding/softwareProduct/SoftwareProductConstants.js';
24
25 class HeatScreenView extends Component {
26
27         static propTypes = {
28                 isValidationAvailable: PropTypes.bool,
29                 goToOverview: PropTypes.bool,
30                 setActiveTab: PropTypes.func
31         };
32
33         render() {
34                 let {isValidationAvailable, isReadOnlyMode, heatDataExist, onDownload, softwareProductId, onProcessAndValidate,
35                         heatSetup, HeatSetupComponent, onGoToOverview, version, onboardingOrigin, activeTab, setActiveTab, ...other} = this.props;
36
37                 return (
38                         <div className='vsp-attachments-view'>
39                                 <div className='attachments-view-controllers'>
40                                         {(activeTab === tabsMapping.SETUP) &&
41                                                 <SVGIcon
42                                                         disabled={heatDataExist ? false : true}
43                                                         name='download'
44                                                         className='icon-component'
45                                                         label={i18n('Export Validation')}
46                                                         labelPosition='right'
47                                                         color='secondary'
48                                                         onClick={heatDataExist ? () => onDownload({heatCandidate: heatSetup, isReadOnlyMode, version}) : undefined}
49                                                         data-test-id='download-heat'/>}
50                                         {(activeTab === tabsMapping.VALIDATION && softwareProductId) &&
51                                                 <SVGIcon
52                                                         disabled={this.props.goToOverview !== true}
53                                                         onClick={this.props.goToOverview ? () => onGoToOverview({version}) : undefined}
54                                                         name='proceedToOverview'
55                                                         className='icon-component'
56                                                         label={i18n('Go to Overview')}
57                                                         labelPosition='right'
58                                                         color={this.props.goToOverview ? 'primary' : 'secondary'}
59                                                         data-test-id='go-to-overview'/>}
60                                         <SVGIcon
61                                                 name='upload'
62                                                 className='icon-component'
63                                                 label={i18n('Upload New File')}
64                                                 labelPosition='right'
65                                                 color='secondary'
66                                                 disabled={isReadOnlyMode}
67                                                 onClick={isReadOnlyMode ? undefined : evt => this.refs.hiddenImportFileInput.click(evt)}
68                                                 data-test-id='upload-heat'/>
69                                         <input
70                                                 ref='hiddenImportFileInput'
71                                                 type='file'
72                                                 name='fileInput'
73                                                 accept='.zip, .csar'
74                                                 onChange={evt => this.handleImport(evt)}/>
75                                 </div>
76                         <Tabs
77                                 className='attachments-tabs'
78                                 type='header'
79                                 activeTab={activeTab}
80                                 onTabClick={key => this.handleTabPress(key)}>
81                                         <Tab tabId={tabsMapping.SETUP} title='Setup' disabled={onboardingOrigin === onboardingOriginTypes.CSAR}>
82                                                 <HeatSetupComponent
83                                                         heatDataExist={heatDataExist}
84                                                         changeAttachmentsTab={tab => setActiveTab({activeTab: tab})}
85                                                         onProcessAndValidate={onProcessAndValidate}
86                                                         softwareProductId={softwareProductId}
87                                                         isReadOnlyMode={isReadOnlyMode}
88                                                         version={version}/>
89                                         </Tab>
90                                         <Tab tabId={tabsMapping.VALIDATION} title='Validation' disabled={!isValidationAvailable}>
91                                                 <HeatValidation {...other}/>
92                                         </Tab>
93                                 </Tabs>
94                         </div>
95                 );
96         }
97
98         handleTabPress(key) {
99                 let {heatSetup, heatSetupCache, onProcessAndValidate, isReadOnlyMode, version, setActiveTab} = this.props;
100                 switch (key) {
101                         case tabsMapping.VALIDATION:
102                                 onProcessAndValidate({heatData: heatSetup, heatDataCache: heatSetupCache, isReadOnlyMode, version}).then(
103                                         () => setActiveTab({activeTab: tabsMapping.VALIDATION})
104                                 );
105                                 return;
106                         case tabsMapping.SETUP:
107                                 setActiveTab({activeTab: tabsMapping.SETUP});
108                                 return;
109                 }
110         }
111
112         handleImport(evt) {
113                 evt.preventDefault();
114                 let file = this.refs.hiddenImportFileInput.files[0];
115                 if(! (file && file.size && accept(file, ['.zip', '.csar'])) ) {
116                         this.props.onInvalidFileUpload();
117                         return;
118                 }
119                 let {version} = this.props;
120                 let formData = new FormData();
121                 formData.append('upload', file);
122                 this.refs.hiddenImportFileInput.value = '';
123                 this.props.onUpload(formData, version);
124         }
125
126         save() {
127
128                 return this.props.onboardingOrigin === onboardingOriginTypes.ZIP ?
129                         this.props.onSave(this.props.heatSetup, this.props.version) :
130                         Promise.resolve();
131         }
132
133 }
134
135 export default HeatScreenView;