From 73c791296b260409ba78cff22b4124a91aebc39e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 20 Jun 2023 00:45:41 -0400 Subject: [PATCH] fix(striker-ui): allow delete in server VMs management --- striker-ui/components/Servers.tsx | 83 +++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/striker-ui/components/Servers.tsx b/striker-ui/components/Servers.tsx index f02c9e65..d9d0298d 100644 --- a/striker-ui/components/Servers.tsx +++ b/striker-ui/components/Servers.tsx @@ -1,9 +1,4 @@ -import { - Add as AddIcon, - Check as CheckIcon, - Edit as EditIcon, - MoreVert as MoreVertIcon, -} from '@mui/icons-material'; +import { MoreVert as MoreVertIcon } from '@mui/icons-material'; import { Box, Checkbox, @@ -14,8 +9,9 @@ import { styled, Typography, } 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 { BLUE, DIVIDER, @@ -26,20 +22,22 @@ import { } from '../lib/consts/DEFAULT_THEME'; import serverState from '../lib/consts/SERVERS'; +import api from '../lib/api'; import { AnvilContext } from './AnvilContext'; +import ConfirmDialog from './ConfirmDialog'; +import ContainedButton from './ContainedButton'; import Decorator, { Colours } from './Decorator'; +import handleAPIError from '../lib/handleAPIError'; +import hostsSanitizer from '../lib/sanitizers/hostsSanitizer'; import IconButton from './IconButton'; import MenuItem from './MenuItem'; import { Panel, PanelHeader } from './Panels'; +import periodicFetch from '../lib/fetchers/periodicFetch'; import ProvisionServerDialog from './ProvisionServerDialog'; +import putFetch from '../lib/fetchers/putFetch'; import Spinner from './Spinner'; import { BodyText, HeaderText } from './Text'; - -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'; +import useConfirmDialogProps from '../hooks/useConfirmDialogProps'; const PREFIX = 'Servers'; @@ -152,6 +150,9 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const [isOpenProvisionServerDialog, setIsOpenProvisionServerDialog] = useState(false); + const confirmDialogRef = useRef({}); + const [confirmDialogProps, setConfirmDialogProps] = useConfirmDialogProps(); + const { uuid } = useContext(AnvilContext); const buttonLabels = useRef([]); @@ -212,6 +213,11 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { const filteredHosts = hostsSanitizer(anvil[anvilIndex]?.hosts); + const noneChecked = useMemo( + () => !selected.length, + [selected.length], + ); + return ( <> @@ -221,19 +227,51 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { sx={{ marginBottom: 0 }} > - setIsOpenProvisionServerDialog(true)}> - - - setShowCheckbox(!showCheckbox)}> - {showCheckbox ? : } - + {showCheckbox && ( + { + 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" + /> + )} + setShowCheckbox(!showCheckbox)} + state={String(showCheckbox)} + /> + setIsOpenProvisionServerDialog(true)} + /> {showCheckbox && ( <> } > @@ -377,6 +415,11 @@ const Servers = ({ anvil }: { anvil: AnvilListItem[] }): JSX.Element => { setIsOpenProvisionServerDialog(false); }} /> + ); };