Newer
Older
import React, { useState } from "react";
import { makeStyles } from "@material-ui/core/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Paper from "@material-ui/core/Paper";
import Stepper from "@material-ui/core/Stepper";
import Step from "@material-ui/core/Step";
import StepLabel from "@material-ui/core/StepLabel";
import Button from "@material-ui/core/Button";
import Link from "@material-ui/core/Link";
import Typography from "@material-ui/core/Typography";
import GlobalConfig from "./GlobalConfig";
import { useConfig } from "../../hooks/useConfig";
import { useAuth } from "../../hooks/useAuth";
import ComparisonForm from "../Comparisons/ComparisonForm";
import { useHistory } from "react-router-dom";
import CircularProgress from "@material-ui/core/CircularProgress";
import Snackbar from "@material-ui/core/Snackbar";
import MuiAlert from "@material-ui/lab/Alert";
function Alert(props) {
return <MuiAlert elevation={6} variant="filled" {...props} />;
}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function Copyright() {
return (
<Typography variant="body2" color="textSecondary" align="center">
{"Copyright © "}
<Link color="inherit" href="https://material-ui.com/">
Your Website
</Link>{" "}
{new Date().getFullYear()}
{"."}
</Typography>
);
}
const useStyles = makeStyles((theme) => ({
appBar: {
position: "relative",
},
layout: {
width: "auto",
marginLeft: theme.spacing(2),
marginRight: theme.spacing(2),
[theme.breakpoints.up(600 + theme.spacing(2) * 2)]: {
width: 1200,
marginLeft: "auto",
marginRight: "auto",
},
},
paper: {
marginTop: theme.spacing(3),
marginBottom: theme.spacing(3),
padding: theme.spacing(2),
[theme.breakpoints.up(600 + theme.spacing(3) * 2)]: {
marginTop: theme.spacing(6),
marginBottom: theme.spacing(6),
padding: theme.spacing(3),
},
},
stepper: {
padding: theme.spacing(3, 0, 5),
},
buttons: {
display: "flex",
justifyContent: "flex-end",
},
button: {
marginTop: theme.spacing(3),
marginLeft: theme.spacing(1),
},
}));
const steps = ["Global configuration", "Expermiental design"];
export default function RunForm() {
Skander Hatira
committed
const {
compState,
setCompState,
initialComp,
comparisons,
setComparisons,
remotecomparisons,
setRemoteComparisons,
} = useConfig();
const { user } = useAuth();
const [response, setResponse] = useState({});
const classes = useStyles();
const [activeStep, setActiveStep] = React.useState(0);
const history = useHistory();
const [loading, setLoading] = useState(false);
const [errors, setErrors] = useState();
const [open, setOpen] = useState(false);
const handleClose = (event, reason) => {
if (reason === "clickaway") {
return;
}
setOpen(false);
};
const handleClick = () => {
setOpen(true);
};
const handleNext = () => {
setActiveStep(activeStep + 1);
};
const handleBack = () => {
setActiveStep(activeStep - 1);
};
function validate(compState, comparisons) {
// we are going to store errors for all fields
// in a signle array
const errors = [];
if (compState.genome.length === 0) {
errors.push("You have to specify a genome");
}
if (comparisons.length === 0) {
errors.push(
"You have to specify at least one comparison to be performed"
);
}
const isFullyDesigned = comparisons.every(
Skander Hatira
committed
(comp) =>
comp.id &&
(comp.control || comp.customControl) &&
(comp.treatment || comp.customTreatment)
);
if (!isFullyDesigned) {
errors.push("You have to correctly fill fields for each comparison");
}
if (compState.remote) {
if (compState.remoteDir.length === 0) {
errors.push(
"You have to specify a remote output directory when using a remote machine"
);
}
}
return errors;
}
function getStepContent(step) {
switch (step) {
case 0:
return <GlobalConfig />;
case 1:
return <ComparisonForm />;
default:
throw new Error("Unknown step");
}
}
const handleComparisonSubmit = () => {
setLoading(true);
const errors = validate(compState, comparisons);
if (errors.length > 0) {
console.log(errors);
setErrors(errors);
handleClick(true);
setLoading(false);
return errors;
}
Skander Hatira
committed
remotecomparisons,
Skander Hatira
committed
email: user.user.email,
const token = sessionStorage.jwtToken;
const options = {
method: "POST",
path: "http://localhost/api/comparisons/comparison",
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
socketPath: sessionStorage.Sock,
hostname: "unix",
port: null,
headers: {
"Content-Type": "application/json",
Authorization: token,
},
};
const req = http.request(options, function (res) {
const chunks = [];
console.log("STATUS: " + res.statusCode);
console.log("HEADERS: " + JSON.stringify(res.headers));
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("error", (err) => console.log(err));
res.on("end", function () {
const body = Buffer.concat(chunks).toString();
const jsbody = JSON.parse(body);
if (res.statusCode !== 200) {
console.log("failed post request");
} else {
console.log("successful post request");
setResponse(jsbody);
setLoading(false);
}
});
});
req.on("error", (err) => console.log(err));
req.write(JSON.stringify(request));
req.end();
};
const handleReset = () => {
setComparisons([]);
setRemoteComparisons([]);
};
return (
<React.Fragment>
<Snackbar open={open} autoHideDuration={10000} onClose={handleClose}>
<Alert
onClose={handleClose}
severity={errors && errors.length > 0 ? "error" : "success"}
>
{errors && errors.length > 0
? `You can't submit because : ${errors}`
: "Your Run has been submitted"}
</Alert>
</Snackbar>
<CssBaseline />
<AppBar position="absolute" color="default" className={classes.appBar}>
<Toolbar>
<Typography variant="h6" color="inherit" noWrap>
New Comparison
</Typography>
</Toolbar>
</AppBar>
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{loading ? (
<div
style={{
position: "absolute",
left: "50%",
top: "50%",
transform: "translate(-50%, -50%)",
}}
>
<CircularProgress size={80} />
</div>
) : (
<main className={classes.layout}>
<Paper className={classes.paper}>
<Typography component="h1" variant="h4" align="center">
Parameters
</Typography>
<Stepper activeStep={activeStep} className={classes.stepper}>
{steps.map((label) => (
<Step key={label}>
<StepLabel>{label}</StepLabel>
</Step>
))}
</Stepper>
<React.Fragment>
{activeStep === steps.length ? (
""
) : (
<React.Fragment>
{getStepContent(activeStep)}
<div className={classes.buttons}>
{activeStep !== 0 && (
<Button onClick={handleBack} className={classes.button}>
Back
</Button>
)}
<Button
variant="contained"
color="primary"
onClick={
activeStep === steps.length - 1
? handleComparisonSubmit
: handleNext
}
className={classes.button}
>
{activeStep === steps.length - 1
? "Launch comparison"
: "Next"}
</Button>
<Button
variant="contained"
color="secondary"
onClick={handleReset}
className={classes.button}
>
reset
</div>
</React.Fragment>
)}
</React.Fragment>
</Paper>
<Copyright />
</main>
)}
</React.Fragment>
);
}