page.tsx 7.1 KB

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