Merge pull request #12 from Since-3/feature/delete-dialog-course

Feature/delete dialog course
This commit is contained in:
Rajpreet0 2025-12-13 14:39:53 +01:00 • committed by GitHub
commit 30db138ea5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 226 additions and 7 deletions

View file

@ -0,0 +1,55 @@
import { AlertTriangle } from "lucide-react";
import { Button } from "../ui/button";
import {
Dialog,
DialogHeader,
DialogContent,
DialogTitle,
DialogFooter,
DialogClose,
DialogDescription,
} from "../ui/dialog";
interface DeleteDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
courseName?: string;
onConfirm: () => void;
isLoading?: boolean;
}
const DeleteDialog: React.FC<DeleteDialogProps> = ({
open,
onOpenChange,
courseName,
onConfirm,
isLoading,
}) => {
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex gap-4 items-center text-xl">
<AlertTriangle /> Kurs wirklich löschen?
</DialogTitle>
<DialogDescription className="mt-2 text-gray-600">
Der Kurs <span className="font-semibold text-gray-600">{courseName}</span> wird
dauerhaft gelöscht. Diese Aktion kann nicht rückgängig gemacht werden.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose className="mt-6 flex gap-2" asChild>
<Button variant="outline" onClick={() => onOpenChange(false)}>
Abbrechen
</Button>
</DialogClose>
<Button variant="destructive" className="mt-6" onClick={onConfirm} disabled={isLoading}>
{isLoading ? "Lösche..." : "Kurs löschen"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
export default DeleteDialog;

View file

@ -12,7 +12,7 @@ const buttonVariants = cva(
default:
"bg-blue text-yellow font-bold text-md drop-shadow-lg hover:scale-105 transition-all cursor-pointer",
destructive:
"bg-destructive text-white shadow-xs hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
"bg-destructive cursor-pointer text-white shadow-xs hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
outline:
"bg-transparent text-blue border border-blue cursor-pointer",
secondary:

View file

@ -0,0 +1,143 @@
"use client"
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { XIcon } from "lucide-react"
import { cn } from "@/src/lib/utils"
function Dialog({
...props
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
return <DialogPrimitive.Root data-slot="dialog" {...props} />
}
function DialogTrigger({
...props
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
}
function DialogPortal({
...props
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
}
function DialogClose({
...props
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
}
function DialogOverlay({
className,
...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
return (
<DialogPrimitive.Overlay
data-slot="dialog-overlay"
className={cn(
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
className
)}
{...props}
/>
)
}
function DialogContent({
className,
children,
showCloseButton = true,
...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
showCloseButton?: boolean
}) {
return (
<DialogPortal data-slot="dialog-portal">
<DialogOverlay />
<DialogPrimitive.Content
data-slot="dialog-content"
className={cn(
"bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg duration-200 outline-none sm:max-w-lg",
className
)}
{...props}
>
{children}
{showCloseButton && (
<DialogPrimitive.Close
data-slot="dialog-close"
className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus:ring-2 focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
>
<XIcon />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
)}
</DialogPrimitive.Content>
</DialogPortal>
)
}
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="dialog-header"
className={cn("flex flex-col gap-2 text-center sm:text-left", className)}
{...props}
/>
)
}
function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="dialog-footer"
className={cn(
"flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
className
)}
{...props}
/>
)
}
function DialogTitle({
className,
...props
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
return (
<DialogPrimitive.Title
data-slot="dialog-title"
className={cn("text-lg leading-none font-semibold", className)}
{...props}
/>
)
}
function DialogDescription({
className,
...props
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
return (
<DialogPrimitive.Description
data-slot="dialog-description"
className={cn("text-muted-foreground text-sm", className)}
{...props}
/>
)
}
export {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogOverlay,
DialogPortal,
DialogTitle,
DialogTrigger,
}

View file

@ -6,6 +6,7 @@ import { toast } from "sonner";
import { useRouter } from "next/navigation";
import CourseListItem from "../components/CourseListItem";
import { Filter } from "lucide-react";
import DeleteDialog from "@/src/components/layout/DeleteDialog";
type Course = {
id: string;
@ -37,6 +38,9 @@ const CourseListView = () => {
const [dateFrom, setDateFrom] = useState("");
const [dateTo, setDateTo] = useState("");
const [deleteMode, setDeleteMode] = useState(false);
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const [courseToDelete, setCourseToDelete] = useState<{ id: string; name: string } | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
const loadCourses = async () => {
const result = await getMyCourses();
@ -56,12 +60,22 @@ const CourseListView = () => {
fetchCourses();
}, []);
const handleDelete = async (courseId: string, courseName: string) => {
if (!confirm(`Möchten Sie den Kurs "${courseName}" wirklich löschen?`)) {
return;
}
const requestDelete = (courseId: string, courseName: string) => {
setCourseToDelete({ id: courseId, name: courseName });
setDeleteDialogOpen(true);
};
const confirmDelete = async () => {
if (!courseToDelete) return;
setIsDeleting(true);
const result = await deleteCourse(courseToDelete.id);
setIsDeleting(false);
setDeleteDialogOpen(false);
setCourseToDelete(null);
const result = await deleteCourse(courseId);
if (result.success) {
toast.success(result.message);
loadCourses();
@ -323,7 +337,7 @@ const CourseListView = () => {
status={course.status}
isPast={isPastCourse(course.date)}
showDeleteIcon={deleteMode}
onDelete={() => handleDelete(course.id, course.name)}
onDelete={() => requestDelete(course.id, course.name)}
onEdit={() => router.push(`/courses/edit/${course.id}`)}
/>
))}
@ -333,6 +347,13 @@ const CourseListView = () => {
})}
</div>
)}
<DeleteDialog
open={deleteDialogOpen}
onOpenChange={setDeleteDialogOpen}
courseName={courseToDelete?.name}
onConfirm={confirmDelete}
isLoading={isDeleting}
/>
</div>
);
};