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