87abaf497891ac80332ef3da1929f97846eeca04
[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 from 'react';
17 import i18n from 'nfvo-utils/i18n/i18n.js';
18 import Dropzone from 'react-dropzone';
19
20 import DraggableUploadFileBox from 'nfvo-components/fileupload/DraggableUploadFileBox.jsx';
21 import  {optionsInputValues as ComponentProcessesOptionsInputValues} from './SoftwareProductComponentProcessesConstants.js';
22 import Form from 'nfvo-components/input/validation/Form.jsx';
23 import Input from 'nfvo-components/input/validation/Input.jsx';
24 import GridSection from 'nfvo-components/grid/GridSection.jsx';
25 import GridItem from 'nfvo-components/grid/GridItem.jsx';
26
27 const SoftwareProductProcessEditorPropType = React.PropTypes.shape({
28         id: React.PropTypes.string,
29         name: React.PropTypes.string,
30         description: React.PropTypes.string,
31         artifactName: React.PropTypes.string,
32         type: React.PropTypes.string
33 });
34
35
36 class SoftwareProductProcessesEditorView extends React.Component {
37
38         state = {
39                 dragging: false,
40                 files: []
41         };
42
43         static propTypes = {
44                 data: SoftwareProductProcessEditorPropType,
45                 previousData: SoftwareProductProcessEditorPropType,
46                 isReadOnlyMode: React.PropTypes.bool,
47                 onDataChanged: React.PropTypes.func,
48                 onSubmit: React.PropTypes.func,
49                 onCancel: React.PropTypes.func
50         };
51
52         render() {
53                 let {isReadOnlyMode, onCancel, onDataChanged, genericFieldInfo, data = {}} = this.props;
54                 let {name, description, artifactName, type} = data;
55
56                 return (
57                         <div>
58                                 { genericFieldInfo && <Form
59                                         ref='validationForm'
60                                         isReadOnlyMode={isReadOnlyMode}
61                                         hasButtons={true}
62                                         labledButtons={true}
63                                         onSubmit={ () => this.submit() }
64                                         onReset={ () => onCancel() }
65                                         isValid={this.props.isFormValid}
66                                         formReady={this.props.formReady}
67                                         onValidateForm={() => this.props.onValidateForm() }
68                                         className='vsp-processes-editor'>
69                                         <div className={`vsp-processes-editor-data${isReadOnlyMode ? ' disabled' : '' }`}>
70                                                 <Dropzone
71                                                         className={`vsp-process-dropzone-view ${this.state.dragging ? 'active-dragging' : ''}`}
72                                                         onDrop={(acceptedFiles, rejectedFiles) => this.handleImportSubmit(acceptedFiles, rejectedFiles)}
73                                                         onDragEnter={() => this.setState({dragging:true})}
74                                                         onDragLeave={() => this.setState({dragging:false})}
75                                                         multiple={false}
76                                                         disableClick={true}
77                                                         ref='processEditorFileInput'
78                                                         name='processEditorFileInput'>
79                                                         <GridSection>
80                                                                 <GridItem colSpan={2}>
81                                                                         <Input
82                                                                                 onChange={name => onDataChanged({name})}
83                                                                                 isValid={genericFieldInfo.name.isValid}
84                                                                                 isRequired={true}
85                                                                                 data-test-id='name'
86                                                                                 errorText={genericFieldInfo.name.errorText}
87                                                                                 label={i18n('Name')}
88                                                                                 value={name}
89                                                                                 type='text'/>
90                                                                 </GridItem>
91                                                                 <GridItem colSpan={2}>
92                                                                         <DraggableUploadFileBox isReadOnlyMode={isReadOnlyMode} className='file-upload-box' onClick={() => this.refs.processEditorFileInput.open()} />
93                                                                 </GridItem>
94                                                         </GridSection>
95                                                         <GridSection>
96                                                                 <GridItem colSpan={2}>
97                                                                         <Input
98                                                                                 name='vsp-process-description'
99                                                                                 groupClassName='vsp-process-description'
100                                                                                 onChange={description => onDataChanged({description})}
101                                                                                 isValid={genericFieldInfo.description.isValid}
102                                                                                 errorText={genericFieldInfo.description.errorText}
103                                                                                 label={i18n('Notes')}
104                                                                                 value={description}
105                                                                                 data-test-id='vsp-process-description'
106                                                                                 type='textarea'/>
107                                                                 </GridItem>
108                                                                 <GridItem colSpan={2}>
109                                                                         <Input
110                                                                                 label={i18n('Artifacts')}
111                                                                                 data-test-id='artifacts'
112                                                                                 value={artifactName}
113                                                                                 type='text'
114                                                                                 disabled/>
115                                                                         <Input
116                                                                                 onChange={e => {
117                                                                                         // setting the unit to the correct value
118                                                                                         const selectedIndex = e.target.selectedIndex;
119                                                                                         const val = e.target.options[selectedIndex].value;
120                                                                                         onDataChanged({type: val});}
121                                                                                 }
122                                                                                 value={type}
123                                                                                 label={i18n('Process Type')}
124                                                                                 data-test-id='process-type'
125                                                                                 isValid={genericFieldInfo.type.isValid}
126                                                                                 errorText={genericFieldInfo.type.errorText}
127                                                                                 type='select'
128                                                                                 className='input-options-select'
129                                                                                 groupClassName='bootstrap-input-options' >
130                                                                                 {ComponentProcessesOptionsInputValues.PROCESS_TYPE.map(mtype =>
131                                                                                         <option key={mtype.enum} value={mtype.enum}>{`${mtype.title}`}</option>)}
132                                                                         </Input>
133                                                                 </GridItem>
134                                                         </GridSection>
135                                                 </Dropzone>
136                                         </div>
137                                 </Form>}
138                         </div>
139                 );
140         }
141
142         submit() {
143                 const {data: process, previousData: previousProcess} = this.props;
144                 let {files} = this.state;
145                 let formData = new FormData();
146                 if (files.length) {
147                         let file = files[0];
148                         formData.append('upload', file);
149                 }
150
151                 let updatedProcess = {
152                         ...process,
153                         formData: files.length ? formData : false
154                 };
155                 this.props.onSubmit({process: updatedProcess, previousProcess});
156         }
157
158
159         handleImportSubmit(files, rejectedFiles) {
160                 if (files.length > 0) {
161                         let {onDataChanged} = this.props;
162                         this.setState({
163                                 dragging: false,
164                                 complete: '0',
165                                 files
166                         });
167                         onDataChanged({artifactName: files[0].name});
168                 }
169                 else if (rejectedFiles.length > 0) {
170                         this.setState({
171                                 dragging: false
172                         });
173                         if (DEBUG) {
174                                 console.log('file was rejected ' + rejectedFiles[0].name);
175                         }
176                 }
177
178         }
179 }
180
181 export default SoftwareProductProcessesEditorView;