Skip to content

Commit b411cdd

Browse files
author
Pippo Ramos
authored
Merge pull request #19 from visual-heuristics/feat_fetch_problem_file
Turn class components to functional components, implement change URL …
2 parents 51c6367 + ea077ab commit b411cdd

File tree

4 files changed

+136
-364
lines changed

4 files changed

+136
-364
lines changed

src/pages/PageOne/dropAndFetch.jsx

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import DropZone from "./dropZone";
33
import Button from "@material-ui/core/Button";
44
import Container from "@material-ui/core/Container";
55
import CloudUploadIcon from "@material-ui/icons/CloudUpload";
66
import css from "./index.module.less";
77

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({});
2617

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) => {
2934
return;
3035
};
3136
};
3237

33-
uploadPDDL = async (files) => {
38+
const uploadPDDL = async (files) => {
3439
const formData = new FormData();
3540
for (const name in files) {
3641
formData.append(name, files[name]);
@@ -47,69 +52,67 @@ class DropAndFetch extends React.Component {
4752

4853
const data = await resp.json();
4954
const txt = JSON.stringify(data);
50-
this.props.onStore(txt);
55+
onStore(txt);
5156
} catch (error) {
5257
alert(error);
5358
}
5459
};
5560

56-
handleSubmit = () => {
61+
const handleSubmit = () => {
5762
//Control check for files
63+
if (newURL.lenght > 1) {
64+
handleFileLoad("url", newURL);
65+
}
5866
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
6270
) {
63-
this.uploadPDDL(this.datas);
71+
uploadPDDL(dataFiles);
6472
} else {
6573
console.log("Some files are missing");
6674
alert("Some files are missing");
6775
}
6876
};
6977

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;
7380
};
7481

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+
);
113118
}
114-
115-
export default DropAndFetch;

src/pages/PageOne/dropZone.jsx

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,56 @@ import React from "react";
22
import css from "./index.module.less";
33
import { DropzoneArea } from "material-ui-dropzone";
44
import Typography from "@material-ui/core/Typography";
5+
import { useState } from "react";
56

6-
class DropZone extends React.Component {
7-
state = {
8-
files: [],
9-
};
7+
/**
8+
* Wrapper for DropzoneArea
9+
* @param {string} props name filename id
10+
* @param {string} props desc text description
11+
* @param {string} props fileType document extension
12+
* @param {function} props onFileLoad callback
13+
* @returns Dropzone with title and inner text
14+
*/
15+
export default function DropZone({ name, desc, fileType, onFileLoad }) {
16+
const [files, setFile] = useState([]);
1017

11-
onDrop = (file) => {
18+
const onDrop = (file) => {
19+
//Format chekcs
1220
if (file.length !== 1) {
1321
alert("More than one file, retry");
1422
}
1523
console.log(file);
1624

17-
if (file[0].name.endsWith(this.props.fileType)) {
18-
//console.log(this.state.files);
19-
this.setState({ files: file });
20-
//console.log(this.state.files);
25+
if (file[0].name.endsWith(fileType)) {
26+
setFile([file]);
2127
file[0].text().then((result) => {
22-
this.props.onFileLoad(this.props.name, result);
28+
onFileLoad(name, result);
2329
});
2430
} else {
2531
console.log("Wrong file type");
2632
alert("Wrong file type");
2733
}
2834
};
35+
// To change the inner tet when dorpping a file
36+
let dropText =
37+
files.length > 0
38+
? files[0].name
39+
: "Drag and drop " + name?.toLowerCase() + fileType ??
40+
"--" + " here or click";
2941

30-
render() {
31-
let dropText =
32-
this.state.files.length > 0
33-
? this.state.files[0].name
34-
: "Drag and drop " +
35-
this.props?.name?.toLowerCase() +
36-
this.props?.fileType?? '--' +
37-
" here or click";
38-
39-
return (
40-
<div className={css.dropzoneBox}>
41-
<Typography
42-
variant="h6"
43-
align="center"
44-
color="textPrimary"
45-
component="p"
46-
>
47-
<b>{this.props.name} File </b>
48-
<br />
49-
{this.props.desc}
50-
</Typography>
51-
<DropzoneArea
52-
acceptedFiles={[".pddl"]}
53-
filesLimit={1}
54-
onDrop={(file) => this.onDrop(file)}
55-
dropzoneText={dropText}
56-
/>
57-
</div>
58-
);
59-
}
42+
return (
43+
<div className={css.dropzoneBox}>
44+
<Typography variant="h6" align="center" color="textPrimary" component="p">
45+
<b>{name} File </b>
46+
<br />
47+
{desc}
48+
</Typography>
49+
<DropzoneArea
50+
acceptedFiles={[".pddl"]}
51+
filesLimit={1}
52+
onDrop={(file) => onDrop(file)}
53+
dropzoneText={dropText}
54+
/>
55+
</div>
56+
);
6057
}
61-
62-
export default DropZone;

0 commit comments

Comments
 (0)