feat: add deleteBackup function and integrate it into BackupDetailDialog

This commit is contained in:
biersoeckli
2025-01-29 14:21:28 +00:00
parent c0e9c9fe3b
commit d9a2b3d6be
3 changed files with 43 additions and 3 deletions

View File

@@ -25,4 +25,20 @@ export const downloadBackup = async (s3TargetId: string, s3Key: string) =>
const fileNameOfDownloadedFile = await backupService.downloadBackupForS3TargetAndKey(validatetData.s3TargetId, validatetData.s3Key);
return new SuccessActionResult(fileNameOfDownloadedFile, 'Starting download...'); // returns the download path on the server
}) as Promise<ServerActionResult<any, string>>;
export const deleteBackup = async (s3TargetId: string, s3Key: string) =>
simpleAction(async () => {
await getAuthUserSession();
const validatetData = z.object({
s3TargetId: z.string(),
s3Key: z.string()
}).parse({
s3TargetId,
s3Key
});
await backupService.deleteBackupFromS3(validatetData.s3TargetId, validatetData.s3Key);
return new SuccessActionResult(undefined, 'Backup will be deleted. Refresh the page to see the changes.');
}) as Promise<ServerActionResult<any, string>>;

View File

@@ -12,10 +12,11 @@ import { ScrollArea } from "@radix-ui/react-scroll-area";
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { KubeSizeConverter } from "@/shared/utils/kubernetes-size-converter.utils";
import { formatDateTime } from "@/frontend/utils/format.utils";
import { downloadBackup } from "./actions";
import { deleteBackup, downloadBackup } from "./actions";
import { Button } from "@/components/ui/button";
import { Download } from "lucide-react";
import { Download, Trash2 } from "lucide-react";
import { Toast } from "@/frontend/utils/toast.utils";
import { useConfirmDialog } from "@/frontend/states/zustand.states";
export function BackupDetailDialog({
backupInfo,
@@ -25,6 +26,7 @@ export function BackupDetailDialog({
children: React.ReactNode;
}) {
const { openConfirmDialog } = useConfirmDialog();
const [isOpen, setIsOpen] = React.useState(false);
const [isLoading, setIsLoading] = React.useState(false);
@@ -41,6 +43,16 @@ export function BackupDetailDialog({
}
}
const asyncDeleteBackup = async (s3Key: string) => {
if (await openConfirmDialog({
title: 'Delete Backup',
description: 'This action deletes the backup from the storage. This action cannot be undone.',
okButton: 'Delete'
})) {
await Toast.fromAction(() => deleteBackup(backupInfo.s3TargetId, s3Key));
}
}
return (
<Dialog open={isOpen} onOpenChange={(isO) => {
setIsOpen(isO);
@@ -72,10 +84,13 @@ export function BackupDetailDialog({
<TableRow key={index}>
<TableCell>{formatDateTime(item.backupDate, true)}</TableCell>
<TableCell>{item.sizeBytes ? KubeSizeConverter.convertBytesToReadableSize(item.sizeBytes) : 'unknown'}</TableCell>
<TableCell className="flex justify-end">
<TableCell className="flex justify-end gap-2">
<Button variant="ghost" size="sm" onClick={() => asyncDownloadPvcData(item.key)} disabled={isLoading}>
<Download />
</Button>
<Button variant="ghost" size="sm" onClick={() => asyncDeleteBackup(item.key)} disabled={isLoading}>
<Trash2 />
</Button>
</TableCell>
</TableRow>
))}

View File

@@ -250,6 +250,15 @@ class BackupService {
await FsUtils.deleteFileIfExists(downloadPath);
}
}
async deleteBackupFromS3(s3TargetId: string, key: string) {
const target = await dataAccess.client.s3Target.findFirstOrThrow({
where: {
id: s3TargetId
}
});
return s3Service.deleteFile(target, key);
}
}
const backupService = new BackupService();