1
- import React from "react" ;
1
+ import React , { useState } from "react" ;
2
2
import DropZone from "./dropZone" ;
3
3
import Button from "@material-ui/core/Button" ;
4
4
import Container from "@material-ui/core/Container" ;
5
5
import CloudUploadIcon from "@material-ui/icons/CloudUpload" ;
6
6
import css from "./index.module.less" ;
7
7
8
- const dragsAndDrops = [
9
- { name : "Domain" , fileType : ".pddl" , desc : "or predictes and actions." } ,
10
- {
11
- name : "Problem" ,
12
- fileType : ".pddl" ,
13
- desc : "for objects, initial state and goal." ,
14
- } ,
15
- {
16
- name : "Animation" ,
17
- fileType : ".pddl" ,
18
- desc : "object is representation." ,
19
- } ,
20
- ] ;
21
- class DropAndFetch extends React . Component {
22
- constructor ( props ) {
23
- super ( props ) ;
24
- this . datas = { } ;
25
- }
8
+ /**
9
+ * Three DropZones and Upload button to fetch pddl to server
10
+ * @param {function } props onStore function to send file to pageFour
11
+ * @param {function } props onClick function to go back to home
12
+ * @param {string } props url argument to paas to backend
13
+ * @returns
14
+ */
15
+ export default function DropAndFetch ( { onStore, onClick, newURL } ) {
16
+ const [ dataFiles , setDataFiles ] = useState ( { } ) ;
26
17
27
- componentWillUnmount = ( ) => {
28
- this . setState = ( state , callback ) => {
18
+ const dragsAndDrops = [
19
+ { name : "Domain" , fileType : ".pddl" , desc : "or predictes and actions." } ,
20
+ {
21
+ name : "Problem" ,
22
+ fileType : ".pddl" ,
23
+ desc : "for objects, initial state and goal." ,
24
+ } ,
25
+ {
26
+ name : "Animation" ,
27
+ fileType : ".pddl" ,
28
+ desc : "object is representation." ,
29
+ } ,
30
+ ] ;
31
+
32
+ const componentWillUnmount = ( ) => {
33
+ setDataFiles = ( state , callback ) => {
29
34
return ;
30
35
} ;
31
36
} ;
32
37
33
- uploadPDDL = async ( files ) => {
38
+ const uploadPDDL = async ( files ) => {
34
39
const formData = new FormData ( ) ;
35
40
for ( const name in files ) {
36
41
formData . append ( name , files [ name ] ) ;
@@ -47,69 +52,67 @@ class DropAndFetch extends React.Component {
47
52
48
53
const data = await resp . json ( ) ;
49
54
const txt = JSON . stringify ( data ) ;
50
- this . props . onStore ( txt ) ;
55
+ onStore ( txt ) ;
51
56
} catch ( error ) {
52
57
alert ( error ) ;
53
58
}
54
59
} ;
55
60
56
- handleSubmit = ( ) => {
61
+ const handleSubmit = ( ) => {
57
62
//Control check for files
63
+ if ( newURL . lenght > 1 ) {
64
+ handleFileLoad ( "url" , newURL ) ;
65
+ }
58
66
if (
59
- "domain" in this . datas &&
60
- "problem" in this . datas &&
61
- "animation" in this . datas
67
+ "domain" in dataFiles &&
68
+ "problem" in dataFiles &&
69
+ "animation" in dataFiles
62
70
) {
63
- this . uploadPDDL ( this . datas ) ;
71
+ uploadPDDL ( dataFiles ) ;
64
72
} else {
65
73
console . log ( "Some files are missing" ) ;
66
74
alert ( "Some files are missing" ) ;
67
75
}
68
76
} ;
69
77
70
- handleFileLoad = ( name , file ) => {
71
- this . datas [ name . toLowerCase ( ) ] = file ;
72
- console . log ( this . datas ) ;
78
+ const handleFileLoad = ( name , file ) => {
79
+ dataFiles [ name . toLowerCase ( ) ] = file ;
73
80
} ;
74
81
75
- render ( ) {
76
- return (
77
- < React . Fragment >
78
- < div >
79
- < Container component = "main" className = { css . dropareaBox } >
80
- { dragsAndDrops . map ( ( drag ) => (
81
- < DropZone
82
- key = { drag . name }
83
- name = { drag . name }
84
- desc = { drag . desc }
85
- fileType = { drag . fileType }
86
- onFileLoad = { this . handleFileLoad }
87
- />
88
- ) ) }
89
- </ Container >
90
- < Container maxWidth = "sm" component = "main" >
91
- < div className = { css . buttonBox } >
92
- < Button
93
- variant = "contained"
94
- color = "default"
95
- onClick = { ( ) => this . props . onClick ( ) }
96
- >
97
- Cancel
98
- </ Button >
99
- < Button
100
- variant = "contained"
101
- color = "primary"
102
- startIcon = { < CloudUploadIcon /> }
103
- onClick = { this . handleSubmit }
104
- >
105
- Upload Files
106
- </ Button >
107
- </ div >
108
- </ Container >
109
- </ div >
110
- </ React . Fragment >
111
- ) ;
112
- }
82
+ return (
83
+ < React . Fragment >
84
+ < div >
85
+ < Container component = "main" className = { css . dropareaBox } >
86
+ { dragsAndDrops . map ( ( drag ) => (
87
+ < DropZone
88
+ key = { drag . name }
89
+ name = { drag . name }
90
+ desc = { drag . desc }
91
+ fileType = { drag . fileType }
92
+ onFileLoad = { handleFileLoad }
93
+ />
94
+ ) ) }
95
+ </ Container >
96
+ < Container maxWidth = "sm" component = "main" >
97
+ < div className = { css . buttonBox } >
98
+ < Button
99
+ variant = "contained"
100
+ color = "default"
101
+ onClick = { ( ) => onClick ( ) }
102
+ >
103
+ Cancel
104
+ </ Button >
105
+ < Button
106
+ variant = "contained"
107
+ color = "primary"
108
+ startIcon = { < CloudUploadIcon /> }
109
+ onClick = { handleSubmit }
110
+ >
111
+ Upload Files
112
+ </ Button >
113
+ </ div >
114
+ </ Container >
115
+ </ div >
116
+ </ React . Fragment >
117
+ ) ;
113
118
}
114
-
115
- export default DropAndFetch ;
0 commit comments