EditImportDialog.tsx 3.1 KB

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