page.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use client';
  2. import { useState, useEffect } from 'react';
  3. import { Plus, FileText, Calendar, Trash2, Edit } from 'lucide-react';
  4. import { Button } from '@/components/ui/button';
  5. import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
  6. import { Badge } from '@/components/ui/badge';
  7. import { format } from 'date-fns';
  8. import { getImports, deleteImport } from '@/app/actions/imports';
  9. import { CreateImportDialog } from '@/app/components/imports/CreateImportDialog';
  10. import { EditImportDialog } from '@/app/components/imports/EditImportDialog';
  11. import { ImportDetailDialog } from '@/app/components/imports/ImportDetailDialog';
  12. import { useToast } from '@/hooks/use-toast';
  13. interface Import {
  14. id: number;
  15. name: string;
  16. importDate: string;
  17. layoutId: number;
  18. layout: {
  19. id: number;
  20. name: string;
  21. };
  22. }
  23. export default function ImportsPage() {
  24. const [imports, setImports] = useState<Import[]>([]);
  25. const [loading, setLoading] = useState(true);
  26. const [createDialogOpen, setCreateDialogOpen] = useState(false);
  27. const [editDialogOpen, setEditDialogOpen] = useState(false);
  28. const [detailDialogOpen, setDetailDialogOpen] = useState(false);
  29. const [selectedImport, setSelectedImport] = useState<Import | null>(null);
  30. const { toast } = useToast();
  31. useEffect(() => {
  32. loadImports();
  33. }, []);
  34. async function loadImports() {
  35. try {
  36. const result = await getImports();
  37. if (result.success && result.data) {
  38. // Transform the data to match our Import interface
  39. const transformedImports = result.data.map(item => ({
  40. id: item.id,
  41. name: item.name,
  42. importDate: item.importDate instanceof Date
  43. ? item.importDate.toISOString()
  44. : String(item.importDate),
  45. layoutId: item.layoutId,
  46. layout: {
  47. id: item.layout.id,
  48. name: item.layout.name,
  49. },
  50. }));
  51. setImports(transformedImports);
  52. } else {
  53. toast({
  54. title: 'Error',
  55. description: result.error || 'Failed to load imports',
  56. variant: 'destructive',
  57. });
  58. setImports([]);
  59. }
  60. } catch {
  61. toast({
  62. title: 'Error',
  63. description: 'Failed to load imports',
  64. variant: 'destructive',
  65. });
  66. } finally {
  67. setLoading(false);
  68. }
  69. }
  70. async function handleDeleteImport(id: number) {
  71. if (!confirm('Are you sure you want to delete this import?')) return;
  72. try {
  73. const result = await deleteImport(id);
  74. if (result.success) {
  75. toast({
  76. title: 'Success',
  77. description: 'Import deleted successfully',
  78. });
  79. loadImports();
  80. } else {
  81. toast({
  82. title: 'Error',
  83. description: result.error || 'Failed to delete import',
  84. variant: 'destructive',
  85. });
  86. }
  87. } catch {
  88. toast({
  89. title: 'Error',
  90. description: 'Failed to delete import',
  91. variant: 'destructive',
  92. });
  93. }
  94. }
  95. function handleEditImport(importRecord: Import) {
  96. setSelectedImport(importRecord);
  97. setEditDialogOpen(true);
  98. }
  99. function handleViewImport(importRecord: Import) {
  100. setSelectedImport(importRecord);
  101. setDetailDialogOpen(true);
  102. }
  103. if (loading) {
  104. return (
  105. <div className="container mx-auto py-8">
  106. <div className="flex justify-center">
  107. <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
  108. </div>
  109. </div>
  110. );
  111. }
  112. return (
  113. <div className="container mx-auto py-8">
  114. <div className="flex justify-between items-center mb-6">
  115. <div>
  116. <h1 className="text-3xl font-bold">Import Management</h1>
  117. <p className="text-muted-foreground">Manage your data imports and configurations</p>
  118. </div>
  119. <Button onClick={() => setCreateDialogOpen(true)}>
  120. <Plus className="mr-2 h-4 w-4" />
  121. Create Import
  122. </Button>
  123. </div>
  124. {imports.length === 0 ? (
  125. <Card>
  126. <CardContent className="flex flex-col items-center justify-center py-12">
  127. <FileText className="h-12 w-12 text-muted-foreground mb-4" />
  128. <h3 className="text-lg font-semibold mb-2">No imports yet</h3>
  129. <p className="text-muted-foreground mb-4">
  130. Get started by creating your first import
  131. </p>
  132. <Button onClick={() => setCreateDialogOpen(true)}>
  133. <Plus className="mr-2 h-4 w-4" />
  134. Create Import
  135. </Button>
  136. </CardContent>
  137. </Card>
  138. ) : (
  139. <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
  140. {imports.map((importRecord) => (
  141. <Card key={importRecord.id} className="hover:shadow-lg transition-shadow">
  142. <CardHeader>
  143. <div className="flex justify-between items-start">
  144. <div>
  145. <CardTitle className="text-lg">{importRecord.name}</CardTitle>
  146. <CardDescription>
  147. Layout: {importRecord.layout.name}
  148. </CardDescription>
  149. </div>
  150. <Badge variant="outline">
  151. <Calendar className="mr-1 h-3 w-3" />
  152. {format(new Date(importRecord.importDate), 'MMM d, yyyy')}
  153. </Badge>
  154. </div>
  155. </CardHeader>
  156. <CardContent>
  157. <div className="flex justify-between items-center">
  158. <div className="flex gap-2">
  159. <Button
  160. variant="ghost"
  161. size="sm"
  162. onClick={() => handleViewImport(importRecord)}
  163. >
  164. <FileText className="h-4 w-4" />
  165. </Button>
  166. <Button
  167. variant="ghost"
  168. size="sm"
  169. onClick={() => handleEditImport(importRecord)}
  170. >
  171. <Edit className="h-4 w-4" />
  172. </Button>
  173. <Button
  174. variant="ghost"
  175. size="sm"
  176. onClick={() => handleDeleteImport(importRecord.id)}
  177. >
  178. <Trash2 className="h-4 w-4" />
  179. </Button>
  180. </div>
  181. </div>
  182. </CardContent>
  183. </Card>
  184. ))}
  185. </div>
  186. )}
  187. <CreateImportDialog
  188. open={createDialogOpen}
  189. onOpenChange={setCreateDialogOpen}
  190. onSuccess={loadImports}
  191. />
  192. <EditImportDialog
  193. open={editDialogOpen}
  194. onOpenChange={setEditDialogOpen}
  195. importRecord={selectedImport}
  196. onSuccess={loadImports}
  197. />
  198. <ImportDetailDialog
  199. open={detailDialogOpen}
  200. onOpenChange={setDetailDialogOpen}
  201. importId={selectedImport?.id || 0}
  202. />
  203. </div>
  204. );
  205. }