fix(striker-ui): allow delete in server VMs management

main
Tsu-ba-me 2 years ago
parent 4e03e62dd4
commit 73c791296b
  1. 83
      striker-ui/components/Servers.tsx

@ -1,9 +1,4 @@
import { import { MoreVert as MoreVertIcon } from '@mui/icons-material';
Add as AddIcon,
Check as CheckIcon,
Edit as EditIcon,
MoreVert as MoreVertIcon,
} from '@mui/icons-material';
import { import {
Box, Box,
Checkbox, Checkbox,
@ -14,8 +9,9 @@ import {
styled, styled,
Typography, Typography,
} from '@mui/material'; } from '@mui/material';
import { useState, useContext, useRef } from 'react'; import { useState, useContext, useRef, useMemo } from 'react';
import API_BASE_URL from '../lib/consts/API_BASE_URL';
import { import {
BLUE, BLUE,
DIVIDER, DIVIDER,
@ -26,20 +22,22 @@ import {
} from '../lib/consts/DEFAULT_THEME'; } from '../lib/consts/DEFAULT_THEME';
import serverState from '../lib/consts/SERVERS'; import serverState from '../lib/consts/SERVERS';
import api from '../lib/api';
import { AnvilContext } from './AnvilContext'; import { AnvilContext } from './AnvilContext';
import ConfirmDialog from './ConfirmDialog';
import ContainedButton from './ContainedButton';
import Decorator, { Colours } from './Decorator'; import Decorator, { Colours } from './Decorator';
import handleAPIError from '../lib/handleAPIError';
import hostsSanitizer from '../lib/sanitizers/hostsSanitizer';
import IconButton from './IconButton'; import IconButton from './IconButton';
import MenuItem from './MenuItem'; import MenuItem from './MenuItem';
import { Panel, PanelHeader } from './Panels'; import { Panel, PanelHeader } from './Panels';
import periodicFetch from '../lib/fetchers/periodicFetch';
import ProvisionServerDialog from './ProvisionServerDialog'; import ProvisionServerDialog from './ProvisionServerDialog';
import putFetch from '../lib/fetchers/putFetch';
import Spinner from './Spinner'; import Spinner from './Spinner';
import { BodyText, HeaderText } from './Text'; import { BodyText, HeaderText } from './Text';
import useConfirmDialogProps from '../hooks/useConfirmDialogProps';
import hostsSanitizer from '../lib/sanitizers/hostsSanitizer';
import periodicFetch from '../lib/fetchers/periodicFetch';
import putFetch from '../lib/fetchers/putFetch';
import API_BASE_URL from '../lib/consts/API_BASE_URL';
import ContainedButton from './ContainedButton';
const PREFIX = 'Servers'; const PREFIX = 'Servers';
@ -152,6 +150,9 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
const [isOpenProvisionServerDialog, setIsOpenProvisionServerDialog] = const [isOpenProvisionServerDialog, setIsOpenProvisionServerDialog] =
useState<boolean>(false); useState<boolean>(false);
const confirmDialogRef = useRef<ConfirmDialogForwardedRefContent>({});
const [confirmDialogProps, setConfirmDialogProps] = useConfirmDialogProps();
const { uuid } = useContext(AnvilContext); const { uuid } = useContext(AnvilContext);
const buttonLabels = useRef<ButtonLabels[]>([]); const buttonLabels = useRef<ButtonLabels[]>([]);
@ -212,6 +213,11 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
const filteredHosts = hostsSanitizer(anvil[anvilIndex]?.hosts); const filteredHosts = hostsSanitizer(anvil[anvilIndex]?.hosts);
const noneChecked = useMemo<boolean>(
() => !selected.length,
[selected.length],
);
return ( return (
<> <>
<Panel> <Panel>
@ -221,19 +227,51 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
sx={{ marginBottom: 0 }} sx={{ marginBottom: 0 }}
> >
<HeaderText text="Servers" /> <HeaderText text="Servers" />
<IconButton onClick={() => setIsOpenProvisionServerDialog(true)}> {showCheckbox && (
<AddIcon /> <IconButton
</IconButton> disabled={noneChecked}
<IconButton onClick={() => setShowCheckbox(!showCheckbox)}> mapPreset="delete"
{showCheckbox ? <CheckIcon sx={{ color: BLUE }} /> : <EditIcon />} onClick={() => {
</IconButton> setConfirmDialogProps({
actionProceedText: 'Delete',
content: `Are you sure you want to delete the selected server(s)? This action is not revertable.`,
onProceedAppend: () => {
api
.request({
data: { serverUuids: selected },
method: 'delete',
url: '/server',
})
.catch((error) => {
// TODO: find a place to display the error
handleAPIError(error);
});
},
proceedColour: 'red',
titleText: `Delete ${selected.length} server(s)?`,
});
confirmDialogRef.current.setOpen?.call(null, true);
}}
variant="redcontained"
/>
)}
<IconButton
mapPreset="edit"
onClick={() => setShowCheckbox(!showCheckbox)}
state={String(showCheckbox)}
/>
<IconButton
mapPreset="add"
onClick={() => setIsOpenProvisionServerDialog(true)}
/>
</PanelHeader> </PanelHeader>
{showCheckbox && ( {showCheckbox && (
<> <>
<Box className={classes.headerPadding} display="flex"> <Box className={classes.headerPadding} display="flex">
<Box flexGrow={1} className={classes.dropdown}> <Box flexGrow={1} className={classes.dropdown}>
<ContainedButton <ContainedButton
disabled={!selected.length} disabled={noneChecked}
onClick={handleClick} onClick={handleClick}
startIcon={<MoreVertIcon />} startIcon={<MoreVertIcon />}
> >
@ -377,6 +415,11 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => {
setIsOpenProvisionServerDialog(false); setIsOpenProvisionServerDialog(false);
}} }}
/> />
<ConfirmDialog
closeOnProceed
{...confirmDialogProps}
ref={confirmDialogRef}
/>
</> </>
); );
}; };

Loading…
Cancel
Save