95d1e4e2833520306f23885c81ce29704d68e49e
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import React, {Component} from 'react';
17 import PropTypes from 'prop-types';
18 import Dropzone from 'react-dropzone';
19 import Button from 'sdc-ui/lib/react/Button.js';
20 import DraggableUploadFileBox from 'nfvo-components/fileupload/DraggableUploadFileBox.jsx';
21 import {fileTypes, type2Title, type2Name} from './SoftwareProductComponentsMonitoringConstants.js';
22
23
24
25 class SoftwareProductComponentsMonitoringView extends Component {
26
27         static propTypes = {
28                 isReadOnlyMode: PropTypes.bool,
29                 filenames: PropTypes.object,
30                 softwareProductId: PropTypes.string,
31
32                 onDropMibFileToUpload: PropTypes.func,
33                 onDeleteSnmpFile: PropTypes.func
34         };
35
36         state = {
37                 dragging: false
38         };
39
40
41
42
43         render() {
44                 return (
45                         <div className='vsp-component-monitoring'>
46                                 {this.renderDropzoneWithType(fileTypes.VES_EVENT)}
47                                 {this.renderDropzoneWithType(fileTypes.SNMP_TRAP)}
48                                 {this.renderDropzoneWithType(fileTypes.SNMP_POLL)}
49                         </div>
50                 );
51         }
52
53         renderDropzoneWithType(type) {
54                 let {isReadOnlyMode, filenames} = this.props;
55                 let fileByType = type2Name[type];
56                 let fileName = (filenames) ? filenames[fileByType] : undefined;
57                 let refAndName = `fileInput${type.toString()}`;
58                 let typeDisplayName = type2Title[type];
59                 return (
60                         <Dropzone
61                                 className={`dropzone ${this.state.dragging ? 'active-dragging' : ''}`}
62                                 onDrop={(acceptedFiles, rejectedFiles) => this.handleImport(acceptedFiles, rejectedFiles, {isReadOnlyMode, type, refAndName})}
63                                 onDragEnter={() => this.handleOnDragEnter(isReadOnlyMode)}
64                                 onDragLeave={() => this.setState({dragging:false})}
65                                 multiple={false}
66                                 disableClick={true}
67                                 ref={refAndName}
68                                 name={refAndName}
69                                 accept='.zip'>
70                                 <div className='draggable-wrapper'>
71                                         <div className='section-title'>{typeDisplayName}</div>
72                                         {fileName ? this.renderUploadedFileName(fileName, type, isReadOnlyMode) : this.renderUploadButton(refAndName)}
73                                 </div>
74                         </Dropzone>
75                 );
76         }
77
78         renderUploadButton(refAndName) {
79                 let {isReadOnlyMode} = this.props;
80                 return (
81                         <DraggableUploadFileBox
82                                 dataTestId={`monitoring-upload-${refAndName}`}
83                                 className='software-product-landing-view-top-block-col-upl'
84                                 isReadOnlyMode={isReadOnlyMode}
85                                 onClick={() => this.refs[refAndName].open()}/>
86                 );
87         }
88
89         renderUploadedFileName(filename, type, isReadOnlyMode) {
90                 return (
91                                 <div className='monitoring-file'>
92                                         <Button
93                                                 color='white'
94                                                 disabled={true}
95                                                 className='filename'>
96                                                 {filename}
97                                         </Button>
98
99                                         <Button
100                                                 color='gray'
101                                                 data-test-id={`monitoring-delete-${type}`}
102                                                 disabled={isReadOnlyMode}
103                                                 onClick={()=>this.props.onDeleteFile(type)}
104                                                 iconName='close'
105                                                 className='delete'/>
106                                 </div>
107                 );
108         }
109
110
111         handleOnDragEnter(isReadOnlyMode) {
112                 if (!isReadOnlyMode) {
113                         this.setState({dragging: true});
114                 }
115         }
116
117         handleImport(files, rejectedFiles, {isReadOnlyMode, type, refAndName}) {
118                 if (isReadOnlyMode) {
119                         return;
120                 }
121                 if (files.length > 0) {
122                         this.setState({dragging: false});
123                         let file = files[0];
124                         let formData = new FormData();
125                         formData.append('upload', file);
126                         this.refs[refAndName].value = '';
127                         this.props.onDropMibFileToUpload(formData, type);
128                 } else if (rejectedFiles.length > 0) {
129                         this.setState({dragging: false});
130                         this.props.onFileUploadError();
131                 }
132         }
133 }
134
135 export default SoftwareProductComponentsMonitoringView;