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