refactor(front-end): add fetcher to set power and refactor power buttons

main
Josue 4 years ago
parent 68765a3408
commit 42d3a637d0
  1. 44
      striker-ui/components/Servers.tsx

@ -33,6 +33,8 @@ import Decorator, { Colours } from './Decorator';
import Spinner from './Spinner'; import Spinner from './Spinner';
import hostsSanitizer from '../lib/sanitizers/hostsSanitizer'; import hostsSanitizer from '../lib/sanitizers/hostsSanitizer';
import putJSON from '../lib/fetchers/putJSON';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
width: '100%', width: '100%',
@ -117,6 +119,10 @@ const selectDecorator = (state: string): Colours => {
} }
}; };
type ButtonLabels = 'on' | 'off';
const buttonLabels: ButtonLabels[] = ['on', 'off'];
const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const [showCheckbox, setShowCheckbox] = useState<boolean>(false); const [showCheckbox, setShowCheckbox] = useState<boolean>(false);
@ -133,8 +139,14 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
setAnchorEl(event.currentTarget); setAnchorEl(event.currentTarget);
}; };
const handleClose = () => { const handlePower = (label: ButtonLabels) => {
setAnchorEl(null); setAnchorEl(null);
if (!selected.length) {
putJSON('/set_power', {
server_uuid: selected,
is_on: label === 'on',
});
}
}; };
const handleChange = (server_uuid: string): void => { const handleChange = (server_uuid: string): void => {
@ -184,22 +196,28 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
anchorEl={anchorEl} anchorEl={anchorEl}
keepMounted keepMounted
open={Boolean(anchorEl)} open={Boolean(anchorEl)}
onClose={handleClose} onClose={handlePower}
> >
<MenuItem onClick={handleClose} className={classes.menuItem}> {buttonLabels.map((label: ButtonLabels) => {
<Typography className={classes.on} variant="subtitle1"> return (
On <MenuItem
</Typography> onClick={() => handlePower(label)}
</MenuItem> className={classes.menuItem}
<MenuItem onClick={handleClose} className={classes.menuItem}> key={label}
<Typography className={classes.off} variant="subtitle1"> >
Off <Typography
</Typography> className={classes[label]}
</MenuItem> variant="subtitle1"
>
{label.replace(/^[a-z]/, (c) => c.toUpperCase())}
</Typography>
</MenuItem>
);
})}
</Menu> </Menu>
</Box> </Box>
</Box> </Box>
<Box className={classes.headerPadding} display="flex"> <Box display="flex">
<Box> <Box>
<Checkbox <Checkbox
style={{ color: TEXT }} style={{ color: TEXT }}

Loading…
Cancel
Save