[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / monitoring / SoftwareProductComponentsMonitoringView.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 Dropzone from 'react-dropzone';
18 import ButtonGroup from 'react-bootstrap/lib/ButtonGroup.js';
19 import ButtonToolbar from 'react-bootstrap/lib/ButtonToolbar.js';
20 import Button from 'react-bootstrap/lib/Button.js';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import {fileTypes, type2Title, type2Name} from './SoftwareProductComponentsMonitoringConstants.js';
23
24
25
26 class SoftwareProductComponentsMonitoringView extends Component {
27
28         static propTypes = {
29                 isReadOnlyMode: PropTypes.bool,
30                 filenames: PropTypes.object,
31                 softwareProductId: PropTypes.string,
32
33                 onDropMibFileToUpload: PropTypes.func,
34                 onDeleteSnmpFile: PropTypes.func
35         };
36
37         state = {
38                 dragging: false
39         };
40
41
42
43
44         render() {
45                 return (
46                         <div className='vsp-component-monitoring'>
47                                 {this.renderDropzoneWithType(fileTypes.VES_EVENT)}
48                                 {this.renderDropzoneWithType(fileTypes.SNMP_TRAP)}
49                                 {this.renderDropzoneWithType(fileTypes.SNMP_POLL)}
50                         </div>
51                 );
52         }
53
54         renderDropzoneWithType(type) {
55                 let {isReadOnlyMode, filenames} = this.props;
56                 let fileByType = type2Name[type];
57                 let fileName = (filenames) ? filenames[fileByType] : undefined;
58                 let refAndName = `fileInput${type.toString()}`;
59                 let typeDisplayName = type2Title[type];
60                 return (
61                         <Dropzone
62                                 className={`snmp-dropzone ${this.state.dragging ? 'active-dragging' : ''}`}
63                                 onDrop={(acceptedFiles, rejectedFiles) => this.handleImport(acceptedFiles, rejectedFiles, {isReadOnlyMode, type, refAndName})}
64                                 onDragEnter={() => this.handleOnDragEnter(isReadOnlyMode)}
65                                 onDragLeave={() => this.setState({dragging:false})}
66                                 multiple={false}
67                                 disableClick={true}
68                                 ref={refAndName}
69                                 name={refAndName}
70                                 accept='.zip'
71                                 disabled>
72                                 <div className='draggable-wrapper'>
73                                         <div className='section-title'>{typeDisplayName}</div>
74                                         {fileName ? this.renderUploadedFileName(fileName, type) : this.renderUploadButton(refAndName)}
75                                 </div>
76                         </Dropzone>
77                 );
78         }
79
80         renderUploadButton(refAndName) {
81                 let {isReadOnlyMode} = this.props;
82                 return (
83                         <div
84                                 className={`software-product-landing-view-top-block-col-upl${isReadOnlyMode ? ' disabled' : ''}`}>
85                                 <div className='drag-text'>{i18n('Drag & drop for upload')}</div>
86                                 <div className='or-text'>{i18n('or')}</div>
87                                 <div className='upload-btn primary-btn' data-test-id={`monitoring-upload-${refAndName}`} onClick={() => this.refs[refAndName].open()}>
88                                         <span className='primary-btn-text'>{i18n('Select file')}</span>
89                                 </div>
90                         </div>
91                 );
92         }
93
94         renderUploadedFileName(filename, type) {
95                 return (
96                         <ButtonToolbar>
97                                 <ButtonGroup>
98                                         <Button disabled>{filename}</Button>
99                                         <Button className='delete-button' onClick={()=>this.props.onDeleteFile(type)}>X</Button>
100                                 </ButtonGroup>
101                         </ButtonToolbar>
102                 );
103         }
104
105
106         handleOnDragEnter(isReadOnlyMode) {
107                 if (!isReadOnlyMode) {
108                         this.setState({dragging: true});
109                 }
110         }
111
112         handleImport(files, rejectedFiles, {isReadOnlyMode, type, refAndName}) {
113                 if (isReadOnlyMode) {
114                         return;
115                 }
116                 if (files.length > 0) {
117                         this.setState({dragging: false});
118                         let file = files[0];
119                         let formData = new FormData();
120                         formData.append('upload', file);
121                         this.refs[refAndName].value = '';
122                         this.props.onDropMibFileToUpload(formData, type);
123                 } else if (rejectedFiles.length > 0) {
124                         this.setState({dragging: false});
125                         this.props.onFileUploadError();
126                 }
127         }
128 }
129
130 export default SoftwareProductComponentsMonitoringView;