bbgo_origin/frontend/components/SaveConfigAndRestart.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-02-03 07:59:06 +00:00
import React from 'react';
2021-02-04 12:21:16 +00:00
import { useRouter } from 'next/router';
2021-02-03 07:59:06 +00:00
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import List from '@material-ui/core/List';
import Card from '@material-ui/core/Card';
import CardHeader from '@material-ui/core/CardHeader';
import CardContent from '@material-ui/core/CardContent';
import Avatar from '@material-ui/core/Avatar';
import IconButton from '@material-ui/core/IconButton';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import {makeStyles} from '@material-ui/core/styles';
2021-02-03 09:27:18 +00:00
2021-02-04 12:21:16 +00:00
import {ping, saveConfig, setupRestart} from "../api/bbgo";
import Box from "@material-ui/core/Box";
import Alert from "@material-ui/lab/Alert";
2021-02-03 07:59:06 +00:00
const useStyles = makeStyles((theme) => ({
strategyCard: {
margin: theme.spacing(1),
},
formControl: {
marginTop: theme.spacing(1),
marginBottom: theme.spacing(1),
minWidth: 120,
},
buttons: {
display: 'flex',
justifyContent: 'flex-end',
marginTop: theme.spacing(2),
paddingTop: theme.spacing(2),
paddingBottom: theme.spacing(2),
'& > *': {
marginLeft: theme.spacing(1),
}
},
}));
export default function SaveConfigAndRestart({onBack, onRestarted}) {
const classes = useStyles();
2021-02-04 12:21:16 +00:00
const { push } = useRouter();
2021-02-03 07:59:06 +00:00
const [strategies, setStrategies] = React.useState([]);
2021-02-03 09:27:18 +00:00
const [response, setResponse] = React.useState({});
2021-02-03 07:59:06 +00:00
React.useEffect(() => {
}, [])
const handleRestart = () => {
2021-02-03 09:27:18 +00:00
saveConfig((resp) => {
setResponse(resp);
2021-02-03 07:59:06 +00:00
2021-02-04 12:21:16 +00:00
setupRestart((resp) => {
2021-02-04 12:22:27 +00:00
let t
t = setInterval(function() {
2021-02-04 12:21:16 +00:00
ping(() => {
2021-02-04 12:22:27 +00:00
clearInterval(t)
2021-02-04 12:21:16 +00:00
push("/");
})
}, 1000);
}).catch((err) => {
console.error(err);
setResponse(err.response.data);
})
2021-02-03 09:27:18 +00:00
// call restart here
}).catch((err) => {
console.error(err);
setResponse(err.response.data);
});
2021-02-03 07:59:06 +00:00
};
return (
<React.Fragment>
<Typography variant="h6" gutterBottom>
Save Config and Restart
</Typography>
<Typography variant="body1" gutterBottom>
2021-02-03 09:30:02 +00:00
Click "Save and Restart" to save the configurations to the config file <code>bbgo.yaml</code>,
and save the exchange session credentials to the dotenv file <code>.env.local</code>.
2021-02-03 07:59:06 +00:00
</Typography>
<div className={classes.buttons}>
<Button onClick={() => {
if (onBack) {
onBack()
}
}}>
Back
</Button>
<Button
variant="contained"
color="primary"
onClick={handleRestart}>
2021-02-03 09:30:02 +00:00
Save and Restart
2021-02-03 07:59:06 +00:00
</Button>
</div>
{
response ? response.error ? (
<Box m={2}>
<Alert severity="error">{response.error}</Alert>
</Box>
) : response.success ? (
<Box m={2}>
<Alert severity="success">Config Saved</Alert>
</Box>
) : null : null
}
2021-02-03 07:59:06 +00:00
</React.Fragment>
);
}