CreateImportDialog.tsx 6.2 KB

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