Create new VSP, onboard from TOSCA file - UI
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / landingPage / SoftwareProductLandingPageView.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 from 'react';
17 import classnames from 'classnames';
18 import Dropzone from 'react-dropzone';
19
20
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import DraggableUploadFileBox from 'nfvo-components/fileupload/DraggableUploadFileBox.jsx';
23
24 import SVGIcon from 'sdc-ui/lib/react/SVGIcon.js';
25 import SoftwareProductComponentsList from '../components/SoftwareProductComponentsList.js';
26
27 const SoftwareProductPropType = React.PropTypes.shape({
28         name: React.PropTypes.string,
29         description: React.PropTypes.string,
30         version: React.PropTypes.object,
31         id: React.PropTypes.string,
32         categoryId: React.PropTypes.string,
33         vendorId: React.PropTypes.string,
34         status: React.PropTypes.string,
35         licensingData: React.PropTypes.object,
36         validationData: React.PropTypes.object
37 });
38
39 const ComponentPropType = React.PropTypes.shape({
40         id: React.PropTypes.string,
41         name: React.PropTypes.string,
42         displayName: React.PropTypes.string,
43         description: React.PropTypes.string
44 });
45
46 class SoftwareProductLandingPageView extends React.Component {
47
48         state = {
49
50                 fileName: '',
51                 dragging: false,
52                 files: []
53         };
54
55         static propTypes = {
56                 currentSoftwareProduct: SoftwareProductPropType,
57                 isReadOnlyMode: React.PropTypes.bool,
58                 componentsList: React.PropTypes.arrayOf(ComponentPropType),
59                 onDetailsSelect: React.PropTypes.func,
60                 onUpload: React.PropTypes.func,
61                 onUploadConfirmation: React.PropTypes.func,
62                 onInvalidFileSizeUpload: React.PropTypes.func,
63                 onComponentSelect: React.PropTypes.func,
64                 onAddComponent: React.PropTypes.func
65         };
66
67         render() {
68                 let {currentSoftwareProduct, isReadOnlyMode, isManual, onDetailsSelect, componentsList} =  this.props;
69                 return (
70                         <div className='software-product-landing-wrapper'>
71                                 <Dropzone
72                                         className={classnames('software-product-landing-view', {'active-dragging': this.state.dragging})}
73                                         onDrop={files => this.handleImportSubmit(files, isReadOnlyMode, isManual)}
74                                         onDragEnter={() => this.handleOnDragEnter(isReadOnlyMode, isManual)}
75                                         onDragLeave={() => this.setState({dragging:false})}
76                                         multiple={false}
77                                         disableClick={true}
78                                         ref='fileInput'
79                                         name='fileInput'
80                                         accept='.zip, .csar'
81                                         disabled>
82                                         <div className='draggable-wrapper'>
83                                                 <div className='software-product-landing-view-top'>
84                                                         <div className='row'>
85                                                                 <ProductSummary currentSoftwareProduct={currentSoftwareProduct} onDetailsSelect={onDetailsSelect} />
86                                                                 {this.renderProductDetails(isManual, isReadOnlyMode)}
87                                                         </div>
88                                                 </div>
89                                         </div>
90                                 </Dropzone>
91                                 <SoftwareProductComponentsList
92                                         isReadOnlyMode={isReadOnlyMode}
93                                         componentsList={componentsList}
94                                         isManual={isManual}
95                                         currentSoftwareProduct={currentSoftwareProduct}/>
96                         </div>
97                 );
98         }
99
100         handleOnDragEnter(isReadOnlyMode, isManual) {
101                 if (!isReadOnlyMode && !isManual) {
102                         this.setState({dragging: true});
103                 }
104         }
105
106         renderProductDetails(isManual, isReadOnlyMode) {
107                 return (
108                         <div className='details-panel'>
109                                 { !isManual && <div>
110                                         <div className='software-product-landing-view-heading-title'>{i18n('Software Product Attachments')}</div>
111                                                 <DraggableUploadFileBox
112                                                         dataTestId='upload-btn'
113                                                         isReadOnlyMode={isReadOnlyMode}
114                                                         className={classnames('software-product-landing-view-top-block-col-upl', {'disabled': isReadOnlyMode})}
115                                                         onClick={() => this.refs.fileInput.open()}/>
116                                         </div>
117                                 }
118                         </div>
119                 );
120         }
121
122         handleImportSubmit(files, isReadOnlyMode, isManual) {
123                 if (isReadOnlyMode || isManual) {
124                         return;
125                 }
126                 if (files[0] && files[0].size) {
127                         this.setState({
128                                 fileName: files[0].name,
129                                 dragging: false,
130                                 complete: '0',
131                         });
132                         this.startUploading(files);
133                 }
134                 else {
135                         this.setState({
136                                 dragging: false
137                         });
138                         this.props.onInvalidFileSizeUpload();
139                 }
140
141         }
142
143         startUploading(files) {
144                 let {onUpload, currentSoftwareProduct, onUploadConfirmation} = this.props;
145
146                 let {validationData} = currentSoftwareProduct;
147
148                 if (!(files && files.length)) {
149                         return;
150                 }
151                 let file = files[0];
152                 let formData = new FormData();
153                 formData.append('upload', file);
154                 this.refs.fileInput.value = '';
155
156                 if (validationData) {
157                         onUploadConfirmation(currentSoftwareProduct.id, formData);
158                 }else {
159                         onUpload(currentSoftwareProduct.id, formData);
160                 }
161
162         }
163 }
164
165 const ProductSummary = ({currentSoftwareProduct, onDetailsSelect}) => {
166         let {name = '', description = '', vendorName = '', fullCategoryDisplayName = '', licenseAgreementName = ''}  = currentSoftwareProduct;
167         return (
168                 <div className='details-panel'>
169                         <div className='software-product-landing-view-heading-title'>{i18n('Software Product Details')}</div>
170                         <div
171                                 className='software-product-landing-view-top-block clickable'
172                                 onClick={() => onDetailsSelect(currentSoftwareProduct)}>
173                                 <div className='details-container'>
174                                         <div className='single-detail-section title-section'>
175                                                 <div className='single-detail-section title-text'>
176                                                         {name}
177                                                 </div>
178                                         </div>
179                                         <div className='details-section'>
180                                                 <div className='multiple-details-section'>
181                                                         <div className='detail-col' >
182                                                                 <div className='title'>{i18n('Vendor')}</div>
183                                                                 <div className='description'>{vendorName}</div>
184                                                         </div>
185                                                         <div className='detail-col'>
186                                                                 <div className='title'>{i18n('Category')}</div>
187                                                                 <div className='description'>{fullCategoryDisplayName}</div>
188                                                         </div>
189                                                         <div className='detail-col'>
190                                                                 <div className='title extra-large'>{i18n('License Agreement')}</div>
191                                                                 <div className='description'>
192                                                                         <LicenseAgreement licenseAgreementName={licenseAgreementName}/>
193                                                                 </div>
194                                                         </div>
195                                                 </div>
196                                                 <div className='single-detail-section'>
197                                                         <div className='title'>{i18n('Description')}</div>
198                                                         <div className='description'>{description}</div>
199                                                 </div>
200                                         </div>
201                                 </div>
202                         </div>
203                 </div>
204         );
205 };
206
207
208 const LicenseAgreement = ({licenseAgreementName}) => {
209         if (!licenseAgreementName) {
210                 return (<div className='missing-license'><SVGIcon color='warning' name='exclamationTriangleFull'/><div className='warning-text'>{i18n('Missing')}</div></div>);
211         }
212         return <div>{licenseAgreementName}</div>;
213 };
214
215 export default SoftwareProductLandingPageView;