Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / processes / SoftwareProductProcessesEditorForm.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 from 'react';
17 import PropTypes from 'prop-types';
18 import Dropzone from 'react-dropzone';
19
20 import DraggableUploadFileBox from 'nfvo-components/fileupload/DraggableUploadFileBox.jsx';
21 import i18n from 'nfvo-utils/i18n/i18n.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: PropTypes.string,
29         name: PropTypes.string,
30         description: PropTypes.string,
31         artifactName: PropTypes.string,
32         type: PropTypes.string
33 });
34
35
36
37 class SoftwareProductProcessesEditorForm extends React.Component {
38
39
40         static propTypes = {
41                 data: SoftwareProductProcessEditorPropType,
42                 previousData: SoftwareProductProcessEditorPropType,
43                 isReadOnlyMode: React.PropTypes.bool,
44                 onDataChanged: React.PropTypes.func,
45                 onSubmit: React.PropTypes.func,
46                 onCancel: React.PropTypes.func
47         };
48         state = {
49                 dragging: false,
50                 files: []
51         };
52
53         render() {
54                 let {data = {}, isReadOnlyMode, onDataChanged, onCancel, genericFieldInfo, optionsInputValues} = this.props;
55                 let {name, description, artifactName, type} = data;
56
57                 return (
58                         <div>
59                                 {genericFieldInfo && <Form
60                                         ref='validationForm'
61                                         hasButtons={true}
62                                         labledButtons={true}
63                                         isReadOnlyMode={isReadOnlyMode}
64                                         onSubmit={ () => this.submit() }
65                                         onReset={ () => onCancel() }
66                                         isValid={this.props.isFormValid}
67                                         formReady={this.props.formReady}
68                                         onValidateForm={() => this.props.onValidateForm() }
69                                         className='vsp-processes-editor'>
70                                         <div className={`vsp-processes-editor-data${isReadOnlyMode ? ' disabled' : '' }`}>
71                                                 <Dropzone
72                                                         className={`vsp-process-dropzone-view ${this.state.dragging ? 'active-dragging' : ''}`}
73                                                         onDrop={(acceptedFiles, rejectedFiles) => this.handleImportSubmit(acceptedFiles, rejectedFiles)}
74                                                         onDragEnter={() => this.setState({dragging: true})}
75                                                         onDragLeave={() => this.setState({dragging: false})}
76                                                         multiple={false}
77                                                         disableClick={true}
78                                                         ref='processEditorFileInput'
79                                                         name='processEditorFileInput'>
80                                                         <GridSection hasLastColSet={true}>
81                                                                 <GridItem colSpan={2}>
82                                                                         <Input
83                                                                                 onChange={name => onDataChanged({name})}
84                                                                                 isValid={genericFieldInfo.name.isValid}
85                                                                                 isRequired={true}
86                                                                                 data-test-id='name'
87                                                                                 errorText={genericFieldInfo.name.errorText}
88                                                                                 label={i18n('Name')}
89                                                                                 value={name}
90                                                                                 type='text'/>
91                                                                 </GridItem>
92                                                                 <GridItem colSpan={2} lastColInRow={true}>
93                                                                         <label>&nbsp;</label>
94                                                                         <DraggableUploadFileBox className='process-editor-file-box' isReadOnlyMode={isReadOnlyMode} onClick={() => this.refs.processEditorFileInput.open()}/>
95                                                                 </GridItem>
96                                                         </GridSection>
97                                                         <GridSection hasLastColSet={true}>
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} lastColInRow={true}>
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                                                                                 className='process-type'
126                                                                                 data-test-id='process-type'
127                                                                                 isValid={genericFieldInfo.type.isValid}
128                                                                                 errorText={genericFieldInfo.type.errorText}
129                                                                                 type='select'>
130                                                                                 {optionsInputValues.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 = false;
146                 if (files.length) {
147                         let file = files[0];
148                         formData = new FormData();
149                         formData.append('upload', file);
150                 }
151
152                 let updatedProcess = {
153                         ...process,
154                         formData
155                 };
156                 this.props.onSubmit({process: updatedProcess, previousProcess});
157         }
158
159
160         handleImportSubmit(files, rejectedFiles) {
161                 if (files.length > 0) {
162                         let {onDataChanged} = this.props;
163                         this.setState({
164                                 dragging: false,
165                                 complete: '0',
166                                 files
167                         });
168                         onDataChanged({artifactName: files[0].name});
169                 }
170                 else if (rejectedFiles.length > 0) {
171                         this.setState({
172                                 dragging: false
173                         });
174                         if (DEBUG) {
175                                 console.log('file was rejected.' + rejectedFiles[0].name);
176                         }
177                 }
178         }
179 }
180
181 export default SoftwareProductProcessesEditorForm;