EditImportDialog.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  4. import { Button } from '@/components/ui/button';
  5. import { Input } from '@/components/ui/input';
  6. import { Label } from '@/components/ui/label';
  7. import { useToast } from '@/hooks/use-toast';
  8. import { updateImport } from '@/app/actions/imports';
  9. interface Import {
  10. id: number;
  11. name: string;
  12. importDate: string;
  13. layoutId: number;
  14. layout: {
  15. id: number;
  16. name: string;
  17. };
  18. }
  19. interface EditImportDialogProps {
  20. open: boolean;
  21. onOpenChange: (open: boolean) => void;
  22. importRecord: Import | null;
  23. onSuccess?: () => void;
  24. }
  25. export function EditImportDialog({ open, onOpenChange, importRecord, onSuccess }: EditImportDialogProps) {
  26. const [name, setName] = useState('');
  27. const [loading, setLoading] = useState(false);
  28. const { toast } = useToast();
  29. useEffect(() => {
  30. if (importRecord) {
  31. setName(importRecord.name);
  32. }
  33. }, [importRecord]);
  34. async function handleSubmit(e: React.FormEvent) {
  35. e.preventDefault();
  36. if (!importRecord || !name.trim()) {
  37. toast({
  38. title: 'Error',
  39. description: 'Please enter a valid import name',
  40. variant: 'destructive',
  41. });
  42. return;
  43. }
  44. setLoading(true);
  45. try {
  46. const result = await updateImport({
  47. id: importRecord.id,
  48. name: name.trim(),
  49. });
  50. if (result.success) {
  51. toast({
  52. title: 'Success',
  53. description: 'Import updated successfully',
  54. });
  55. onOpenChange(false);
  56. onSuccess?.();
  57. } else {
  58. toast({
  59. title: 'Error',
  60. description: result.error || 'Failed to update import',
  61. variant: 'destructive',
  62. });
  63. }
  64. } catch {
  65. toast({
  66. title: 'Error',
  67. description: 'Failed to update import',
  68. variant: 'destructive',
  69. });
  70. } finally {
  71. setLoading(false);
  72. }
  73. }
  74. if (!importRecord) return null;
  75. return (
  76. <Dialog open={open} onOpenChange={onOpenChange}>
  77. <DialogContent className="sm:max-w-[425px]">
  78. <DialogHeader>
  79. <DialogTitle>Edit Import</DialogTitle>
  80. <DialogDescription>
  81. Update the name of the import record.
  82. </DialogDescription>
  83. </DialogHeader>
  84. <form onSubmit={handleSubmit}>
  85. <div className="grid gap-4 py-4">
  86. <div className="grid gap-2">
  87. <Label htmlFor="edit-name">Import Name</Label>
  88. <Input
  89. id="edit-name"
  90. value={name}
  91. onChange={(e) => setName(e.target.value)}
  92. placeholder="Enter import name"
  93. disabled={loading}
  94. />
  95. </div>
  96. </div>
  97. <DialogFooter>
  98. <Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={loading}>
  99. Cancel
  100. </Button>
  101. <Button type="submit" disabled={loading || !name.trim()}>
  102. {loading ? 'Updating...' : 'Update Import'}
  103. </Button>
  104. </DialogFooter>
  105. </form>
  106. </DialogContent>
  107. </Dialog>
  108. );
  109. }