2d5a965c40761b29443bad4be0ac82e62091d2f5
[sdc.git] /
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 Button from 'sdc-ui/lib/react/Button.js';
19 import DraggableUploadFileBox from 'nfvo-components/fileupload/DraggableUploadFileBox.jsx';
20 import {fileTypes, type2Title, type2Name} from './SoftwareProductComponentsMonitoringConstants.js';
21
22
23
24 class SoftwareProductComponentsMonitoringView extends Component {
25
26         static propTypes = {
27                 isReadOnlyMode: PropTypes.bool,
28                 filenames: PropTypes.object,
29                 softwareProductId: PropTypes.string,
30
31                 onDropMibFileToUpload: PropTypes.func,
32                 onDeleteSnmpFile: PropTypes.func
33         };
34
35         state = {
36                 dragging: false
37         };
38
39
40
41
42         render() {
43                 return (
44                         <div className='vsp-component-monitoring'>
45                                 {this.renderDropzoneWithType(fileTypes.VES_EVENT)}
46                                 {this.renderDropzoneWithType(fileTypes.SNMP_TRAP)}
47                                 {this.renderDropzoneWithType(fileTypes.SNMP_POLL)}
48                         </div>
49                 );
50         }
51
52         renderDropzoneWithType(type) {
53                 let {isReadOnlyMode, filenames} = this.props;
54                 let fileByType = type2Name[type];
55                 let fileName = (filenames) ? filenames[fileByType] : undefined;
56                 let refAndName = `fileInput${type.toString()}`;
57                 let typeDisplayName = type2Title[type];
58                 return (
59                         <Dropzone
60                                 className={`dropzone ${this.state.dragging ? 'active-dragging' : ''}`}
61                                 onDrop={(acceptedFiles, rejectedFiles) => this.handleImport(acceptedFiles, rejectedFiles, {isReadOnlyMode, type, refAndName})}
62                                 onDragEnter={() => this.handleOnDragEnter(isReadOnlyMode)}
63                                 onDragLeave={() => this.setState({dragging:false})}
64                                 multiple={false}
65                                 disableClick={true}
66                                 ref={refAndName}
67                                 name={refAndName}
68                                 accept='.zip'
69                                 disabled>
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;