DeleteSectionDialog.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use client";
  2. import { useState } from "react";
  3. import { Button } from "@/components/ui/button";
  4. import {
  5. Dialog,
  6. DialogContent,
  7. DialogDescription,
  8. DialogFooter,
  9. DialogHeader,
  10. DialogTitle,
  11. } from "@/components/ui/dialog";
  12. import { deleteLayoutSection } from "@/app/actions/layout-configurations";
  13. import { useToast } from "@/hooks/use-toast";
  14. interface DeleteSectionDialogProps {
  15. open: boolean;
  16. onOpenChange: (open: boolean) => void;
  17. section: {
  18. id: number;
  19. name: string;
  20. };
  21. onSuccess?: () => void;
  22. }
  23. export function DeleteSectionDialog({
  24. open,
  25. onOpenChange,
  26. section,
  27. onSuccess,
  28. }: DeleteSectionDialogProps) {
  29. const [isDeleting, setIsDeleting] = useState(false);
  30. const { toast } = useToast();
  31. const handleDelete = async () => {
  32. setIsDeleting(true);
  33. try {
  34. const result = await deleteLayoutSection(section.id);
  35. if (result.success) {
  36. toast({
  37. title: "Section deleted",
  38. description: `Successfully deleted section "${section.name}"`,
  39. });
  40. onOpenChange(false);
  41. onSuccess?.();
  42. } else {
  43. toast({
  44. title: "Error",
  45. description: result.error || "Failed to delete section",
  46. variant: "destructive",
  47. });
  48. }
  49. } catch {
  50. toast({
  51. title: "Error",
  52. description: "An unexpected error occurred while deleting the section",
  53. variant: "destructive",
  54. });
  55. } finally {
  56. setIsDeleting(false);
  57. }
  58. };
  59. return (
  60. <Dialog open={open} onOpenChange={onOpenChange}>
  61. <DialogContent>
  62. <DialogHeader>
  63. <DialogTitle>Delete Section</DialogTitle>
  64. <DialogDescription>
  65. Are you sure you want to delete the section &quot;{section.name}&quot;? This action
  66. cannot be undone. All fields within this section will also be deleted.
  67. </DialogDescription>
  68. </DialogHeader>
  69. <DialogFooter>
  70. <Button
  71. variant="outline"
  72. onClick={() => onOpenChange(false)}
  73. disabled={isDeleting}
  74. >
  75. Cancel
  76. </Button>
  77. <Button
  78. variant="destructive"
  79. onClick={handleDelete}
  80. disabled={isDeleting}
  81. >
  82. {isDeleting ? "Deleting..." : "Delete"}
  83. </Button>
  84. </DialogFooter>
  85. </DialogContent>
  86. </Dialog>
  87. );
  88. }