page.tsx 6.4 KB

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