DeleteFieldDialog.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 { deleteLayoutSectionField } from "@/app/actions/layout-configurations";
  13. interface LayoutSectionField {
  14. id: number;
  15. name: string;
  16. dataType: string;
  17. cellPosition: string;
  18. }
  19. interface DeleteFieldDialogProps {
  20. field: LayoutSectionField | null;
  21. open: boolean;
  22. onOpenChange: (open: boolean) => void;
  23. onSuccess: () => void;
  24. }
  25. export function DeleteFieldDialog({
  26. field,
  27. open,
  28. onOpenChange,
  29. onSuccess,
  30. }: DeleteFieldDialogProps) {
  31. const [loading, setLoading] = useState(false);
  32. const handleDelete = async () => {
  33. if (!field) return;
  34. setLoading(true);
  35. try {
  36. const result = await deleteLayoutSectionField(field.id);
  37. if (result.success) {
  38. onSuccess();
  39. }
  40. } catch (error) {
  41. console.error("Error deleting field:", error);
  42. } finally {
  43. setLoading(false);
  44. }
  45. };
  46. if (!field) return null;
  47. return (
  48. <Dialog open={open} onOpenChange={onOpenChange}>
  49. <DialogContent>
  50. <DialogHeader>
  51. <DialogTitle>Delete Field</DialogTitle>
  52. <DialogDescription>
  53. Are you sure you want to delete the field &quot;{field.name}&quot;? This action cannot be undone.
  54. </DialogDescription>
  55. </DialogHeader>
  56. <DialogFooter>
  57. <Button variant="outline" onClick={() => onOpenChange(false)}>
  58. Cancel
  59. </Button>
  60. <Button variant="destructive" onClick={handleDelete} disabled={loading}>
  61. {loading ? "Deleting..." : "Delete Field"}
  62. </Button>
  63. </DialogFooter>
  64. </DialogContent>
  65. </Dialog>
  66. );
  67. }