CreateImportDialog.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
  8. import { useToast } from '@/hooks/use-toast';
  9. import { createImport, getLayoutConfigurations } from '@/app/actions/imports';
  10. import { uploadFile } from '@/app/actions/file-upload';
  11. interface LayoutConfiguration {
  12. id: number;
  13. name: string;
  14. }
  15. interface CreateImportDialogProps {
  16. open: boolean;
  17. onOpenChange: (open: boolean) => void;
  18. onSuccess?: () => void;
  19. }
  20. export function CreateImportDialog({ open, onOpenChange, onSuccess }: CreateImportDialogProps) {
  21. const [name, setName] = useState('');
  22. const [layoutId, setLayoutId] = useState<string>('');
  23. const [selectedFile, setSelectedFile] = useState<File | null>(null);
  24. const [loading, setLoading] = useState(false);
  25. const [layouts, setLayouts] = useState<LayoutConfiguration[]>([]);
  26. const [loadingLayouts, setLoadingLayouts] = useState(true);
  27. const { toast } = useToast();
  28. useEffect(() => {
  29. async function loadLayouts() {
  30. try {
  31. const result = await getLayoutConfigurations();
  32. if (result.success && result.data) {
  33. setLayouts(result.data);
  34. }
  35. } catch {
  36. toast({
  37. title: 'Error',
  38. description: 'Failed to load layout configurations',
  39. variant: 'destructive',
  40. });
  41. } finally {
  42. setLoadingLayouts(false);
  43. }
  44. }
  45. if (open) {
  46. loadLayouts();
  47. }
  48. }, [open, toast]);
  49. const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  50. const file = e.target.files?.[0];
  51. if (file) {
  52. setSelectedFile(file);
  53. // Auto-fill name from filename if empty
  54. if (!name.trim()) {
  55. setName(file.name.replace(/\.[^/.]+$/, ''));
  56. }
  57. }
  58. };
  59. async function handleSubmit(e: React.FormEvent) {
  60. e.preventDefault();
  61. if (!selectedFile) {
  62. toast({
  63. title: 'Error',
  64. description: 'Please select a file to upload',
  65. variant: 'destructive',
  66. });
  67. return;
  68. }
  69. if (!name.trim()) {
  70. toast({
  71. title: 'Error',
  72. description: 'Please enter an import name',
  73. variant: 'destructive',
  74. });
  75. return;
  76. }
  77. if (!layoutId) {
  78. toast({
  79. title: 'Error',
  80. description: 'Please select a layout configuration',
  81. variant: 'destructive',
  82. });
  83. return;
  84. }
  85. setLoading(true);
  86. try {
  87. // First, upload the file
  88. const uploadResult = await uploadFile(selectedFile);
  89. if (!uploadResult.success) {
  90. toast({
  91. title: 'Error',
  92. description: uploadResult.error || 'Failed to upload file',
  93. variant: 'destructive',
  94. });
  95. return;
  96. }
  97. // Then create the import with the file reference
  98. const result = await createImport({
  99. name: name.trim(),
  100. layoutId: parseInt(layoutId),
  101. fileId: uploadResult.data?.id,
  102. });
  103. if (result.success) {
  104. toast({
  105. title: 'Success',
  106. description: 'Import created successfully',
  107. });
  108. setName('');
  109. setLayoutId('');
  110. setSelectedFile(null);
  111. onOpenChange(false);
  112. onSuccess?.();
  113. } else {
  114. toast({
  115. title: 'Error',
  116. description: result.error || 'Failed to create import',
  117. variant: 'destructive',
  118. });
  119. }
  120. } catch {
  121. toast({
  122. title: 'Error',
  123. description: 'Failed to create import',
  124. variant: 'destructive',
  125. });
  126. } finally {
  127. setLoading(false);
  128. }
  129. }
  130. return (
  131. <Dialog open={open} onOpenChange={onOpenChange}>
  132. <DialogContent className="sm:max-w-[500px]">
  133. <DialogHeader>
  134. <DialogTitle>Create New Import</DialogTitle>
  135. <DialogDescription>
  136. Upload a file and create a new import with a layout configuration.
  137. </DialogDescription>
  138. </DialogHeader>
  139. <form onSubmit={handleSubmit}>
  140. <div className="grid gap-4 py-4">
  141. <div className="grid gap-2">
  142. <Label htmlFor="file">Upload File</Label>
  143. <Input
  144. id="file"
  145. type="file"
  146. onChange={handleFileChange}
  147. accept=".csv,.xlsx,.xls"
  148. disabled={loading}
  149. />
  150. {selectedFile && (
  151. <p className="text-sm text-muted-foreground">
  152. Selected: {selectedFile.name} ({(selectedFile.size / 1024 / 1024).toFixed(2)} MB)
  153. </p>
  154. )}
  155. </div>
  156. <div className="grid gap-2">
  157. <Label htmlFor="name">Import Name</Label>
  158. <Input
  159. id="name"
  160. value={name}
  161. onChange={(e) => setName(e.target.value)}
  162. placeholder="Enter import name"
  163. disabled={loading}
  164. />
  165. </div>
  166. <div className="grid gap-2">
  167. <Label htmlFor="layout">Layout Configuration</Label>
  168. <Select value={layoutId} onValueChange={setLayoutId} disabled={loadingLayouts || loading}>
  169. <SelectTrigger>
  170. <SelectValue placeholder="Select a layout configuration" />
  171. </SelectTrigger>
  172. <SelectContent>
  173. {layouts.map((layout) => (
  174. <SelectItem key={layout.id} value={layout.id.toString()}>
  175. {layout.name}
  176. </SelectItem>
  177. ))}
  178. </SelectContent>
  179. </Select>
  180. </div>
  181. </div>
  182. <DialogFooter>
  183. <Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={loading}>
  184. Cancel
  185. </Button>
  186. <Button
  187. type="submit"
  188. disabled={loading || !selectedFile || !name || !layoutId}
  189. >
  190. {loading ? 'Creating...' : 'Create Import'}
  191. </Button>
  192. </DialogFooter>
  193. </form>
  194. </DialogContent>
  195. </Dialog>
  196. );
  197. }