CreateImportDialog.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use client';
  2. import { useState } 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 { useEffect } from 'react';
  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 [loading, setLoading] = useState(false);
  24. const [layouts, setLayouts] = useState<LayoutConfiguration[]>([]);
  25. const [loadingLayouts, setLoadingLayouts] = useState(true);
  26. const { toast } = useToast();
  27. useEffect(() => {
  28. if (open) {
  29. loadLayouts();
  30. }
  31. }, [open]);
  32. async function loadLayouts() {
  33. try {
  34. const result = await getLayoutConfigurations();
  35. if (result.success && result.data) {
  36. setLayouts(result.data);
  37. }
  38. } catch (error) {
  39. toast({
  40. title: 'Error',
  41. description: 'Failed to load layout configurations',
  42. variant: 'destructive',
  43. });
  44. } finally {
  45. setLoadingLayouts(false);
  46. }
  47. }
  48. async function handleSubmit(e: React.FormEvent) {
  49. e.preventDefault();
  50. if (!name.trim()) {
  51. toast({
  52. title: 'Error',
  53. description: 'Please enter an import name',
  54. variant: 'destructive',
  55. });
  56. return;
  57. }
  58. if (!layoutId) {
  59. toast({
  60. title: 'Error',
  61. description: 'Please select a layout configuration',
  62. variant: 'destructive',
  63. });
  64. return;
  65. }
  66. setLoading(true);
  67. try {
  68. const result = await createImport({
  69. name: name.trim(),
  70. layoutId: parseInt(layoutId),
  71. });
  72. if (result.success) {
  73. toast({
  74. title: 'Success',
  75. description: 'Import created successfully',
  76. });
  77. setName('');
  78. setLayoutId('');
  79. onOpenChange(false);
  80. onSuccess?.();
  81. } else {
  82. toast({
  83. title: 'Error',
  84. description: result.error || 'Failed to create import',
  85. variant: 'destructive',
  86. });
  87. }
  88. } catch (error) {
  89. toast({
  90. title: 'Error',
  91. description: 'Failed to create import',
  92. variant: 'destructive',
  93. });
  94. } finally {
  95. setLoading(false);
  96. }
  97. }
  98. return (
  99. <Dialog open={open} onOpenChange={onOpenChange}>
  100. <DialogContent className="sm:max-w-[425px]">
  101. <DialogHeader>
  102. <DialogTitle>Create New Import</DialogTitle>
  103. <DialogDescription>
  104. Create a new import with a layout configuration and file name.
  105. </DialogDescription>
  106. </DialogHeader>
  107. <form onSubmit={handleSubmit}>
  108. <div className="grid gap-4 py-4">
  109. <div className="grid gap-2">
  110. <Label htmlFor="name">Import Name</Label>
  111. <Input
  112. id="name"
  113. value={name}
  114. onChange={(e) => setName(e.target.value)}
  115. placeholder="Enter import name"
  116. disabled={loading}
  117. />
  118. </div>
  119. <div className="grid gap-2">
  120. <Label htmlFor="layout">Layout Configuration</Label>
  121. <Select value={layoutId} onValueChange={setLayoutId} disabled={loadingLayouts || loading}>
  122. <SelectTrigger>
  123. <SelectValue placeholder="Select a layout configuration" />
  124. </SelectTrigger>
  125. <SelectContent>
  126. {layouts.map((layout) => (
  127. <SelectItem key={layout.id} value={layout.id.toString()}>
  128. {layout.name}
  129. </SelectItem>
  130. ))}
  131. </SelectContent>
  132. </Select>
  133. </div>
  134. </div>
  135. <DialogFooter>
  136. <Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={loading}>
  137. Cancel
  138. </Button>
  139. <Button type="submit" disabled={loading || !name || !layoutId}>
  140. {loading ? 'Creating...' : 'Create Import'}
  141. </Button>
  142. </DialogFooter>
  143. </form>
  144. </DialogContent>
  145. </Dialog>
  146. );
  147. }