ImportDetailDialog.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. 'use client';
  2. import { useState, useEffect, useCallback } from 'react';
  3. import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  4. import { Button } from '@/components/ui/button';
  5. import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
  6. import { format } from 'date-fns';
  7. import { useToast } from '@/hooks/use-toast';
  8. import { getImportById, calculateCintasSummaries, triggerImportProcess } from '@/app/actions/imports';
  9. interface CintasSummary {
  10. id: number;
  11. week: string;
  12. trrTotal: number;
  13. fourWkAverages: number;
  14. trrPlus4Wk: number;
  15. powerAdds: number;
  16. weekId: number;
  17. importId: number;
  18. }
  19. interface ImportDetail {
  20. id: number;
  21. name: string;
  22. importDate: Date;
  23. layout: {
  24. id: number;
  25. name: string;
  26. sections: Array<{
  27. id: number;
  28. name: string;
  29. tableName: string;
  30. fields: Array<{
  31. id: number;
  32. name: string;
  33. createdAt: Date;
  34. updatedAt: Date;
  35. layoutSectionId: number;
  36. cellPosition: string;
  37. dataType: string;
  38. dataTypeFormat: string | null;
  39. importTableColumnName: string;
  40. importColumnOrderNumber: number;
  41. }>;
  42. }>;
  43. };
  44. cintasSummaries: CintasSummary[];
  45. file?: {
  46. id: string;
  47. filename: string;
  48. size: number;
  49. contentType: string;
  50. };
  51. }
  52. interface ImportDetailDialogProps {
  53. open: boolean;
  54. onOpenChange: (open: boolean) => void;
  55. importId: number;
  56. }
  57. export function ImportDetailDialog({ open, onOpenChange, importId }: ImportDetailDialogProps) {
  58. const [importDetail, setImportDetail] = useState<ImportDetail | null>(null);
  59. const [loading, setLoading] = useState(true);
  60. const [calculating, setCalculating] = useState(false);
  61. const [processing, setProcessing] = useState(false);
  62. const [importStatus, setImportStatus] = useState<'idle' | 'processing' | 'completed' | 'failed'>('idle');
  63. const { toast } = useToast();
  64. const loadImportDetail = useCallback(async () => {
  65. try {
  66. const result = await getImportById(importId);
  67. if (result.success && result.data) {
  68. setImportDetail(result.data);
  69. } else {
  70. toast({
  71. title: 'Error',
  72. description: result.error || 'Failed to load import details',
  73. variant: 'destructive',
  74. });
  75. setImportDetail(null);
  76. }
  77. } catch {
  78. toast({
  79. title: 'Error',
  80. description: 'Failed to load import details',
  81. variant: 'destructive',
  82. });
  83. setImportDetail(null);
  84. } finally {
  85. setLoading(false);
  86. }
  87. }, [importId, toast]);
  88. useEffect(() => {
  89. if (open && importId) {
  90. loadImportDetail();
  91. }
  92. }, [open, importId, loadImportDetail]);
  93. async function handleCalculateSummaries() {
  94. setCalculating(true);
  95. try {
  96. const result = await calculateCintasSummaries(importId);
  97. if (result.success) {
  98. toast({
  99. title: 'Success',
  100. description: 'Cintas summaries calculated successfully',
  101. });
  102. loadImportDetail();
  103. } else {
  104. toast({
  105. title: 'Error',
  106. description: result.error || 'Failed to calculate summaries',
  107. variant: 'destructive',
  108. });
  109. }
  110. } catch {
  111. toast({
  112. title: 'Error',
  113. description: 'Failed to calculate summaries',
  114. variant: 'destructive',
  115. });
  116. } finally {
  117. setCalculating(false);
  118. }
  119. }
  120. async function handleTriggerImport() {
  121. if (!importDetail?.file) {
  122. toast({
  123. title: 'Error',
  124. description: 'No file attached to this import',
  125. variant: 'destructive',
  126. });
  127. return;
  128. }
  129. setProcessing(true);
  130. setImportStatus('processing');
  131. try {
  132. const result = await triggerImportProcess(importId);
  133. if (result.success) {
  134. toast({
  135. title: 'Success',
  136. description: result.message || 'Import process started successfully',
  137. });
  138. // For now, we'll simulate the processing completion
  139. // In a real implementation, you might use polling or WebSocket
  140. setTimeout(() => {
  141. setImportStatus('completed');
  142. setProcessing(false);
  143. toast({
  144. title: 'Import Complete',
  145. description: 'Import process completed successfully',
  146. });
  147. loadImportDetail();
  148. }, 2000);
  149. } else {
  150. toast({
  151. title: 'Error',
  152. description: result.error || 'Failed to start import process',
  153. variant: 'destructive',
  154. });
  155. setProcessing(false);
  156. setImportStatus('failed');
  157. }
  158. } catch {
  159. toast({
  160. title: 'Error',
  161. description: 'Failed to trigger import process',
  162. variant: 'destructive',
  163. });
  164. setProcessing(false);
  165. setImportStatus('failed');
  166. }
  167. }
  168. if (loading) {
  169. return (
  170. <Dialog open={open} onOpenChange={onOpenChange}>
  171. <DialogContent className="max-w-4xl max-h-[80vh]">
  172. <DialogHeader>
  173. <DialogTitle>Loading Import Details</DialogTitle>
  174. <DialogDescription>
  175. Please wait while we load the import information...
  176. </DialogDescription>
  177. </DialogHeader>
  178. <div className="flex justify-center py-8">
  179. <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
  180. </div>
  181. </DialogContent>
  182. </Dialog>
  183. );
  184. }
  185. if (!importDetail) return null;
  186. return (
  187. <Dialog open={open} onOpenChange={onOpenChange}>
  188. <DialogContent className="max-w-4xl max-h-[80vh] overflow-y-auto">
  189. <DialogHeader>
  190. <DialogTitle>Import Details</DialogTitle>
  191. <DialogDescription>
  192. View detailed information about this import
  193. </DialogDescription>
  194. </DialogHeader>
  195. <div className="space-y-6">
  196. <Card>
  197. <CardHeader>
  198. <CardTitle>Import Information</CardTitle>
  199. </CardHeader>
  200. <CardContent className="space-y-2">
  201. <div className="flex justify-between">
  202. <span className="font-medium">Name:</span>
  203. <span>{importDetail.name}</span>
  204. </div>
  205. <div className="flex justify-between">
  206. <span className="font-medium">Layout:</span>
  207. <span>{importDetail.layout.name}</span>
  208. </div>
  209. <div className="flex justify-between">
  210. <span className="font-medium">Import Date:</span>
  211. <span>{format(importDetail.importDate, 'PPpp')}</span>
  212. </div>
  213. {importDetail.file && (
  214. <div className="flex justify-between">
  215. <span className="font-medium">File:</span>
  216. <span>{importDetail.file.filename}</span>
  217. </div>
  218. )}
  219. </CardContent>
  220. </Card>
  221. <Card>
  222. <CardHeader>
  223. <CardTitle>Layout Configuration</CardTitle>
  224. </CardHeader>
  225. <CardContent>
  226. <div className="space-y-4">
  227. {importDetail.layout.sections.map((section) => (
  228. <div key={section.id} className="border rounded-lg p-4">
  229. <h4 className="font-medium mb-2">{section.name}</h4>
  230. <p className="text-sm text-muted-foreground mb-2">
  231. Table: {section.tableName}
  232. </p>
  233. <div className="grid gap-1">
  234. {section.fields.map((field) => (
  235. <div key={field.importTableColumnName} className="text-sm">
  236. <span className="font-medium">{field.name}:</span>
  237. <span className="ml-2 text-muted-foreground">
  238. {field.importTableColumnName}
  239. </span>
  240. </div>
  241. ))}
  242. </div>
  243. </div>
  244. ))}
  245. </div>
  246. </CardContent>
  247. </Card>
  248. <Card>
  249. <CardHeader>
  250. <div className="flex justify-between items-center">
  251. <CardTitle>Import Actions</CardTitle>
  252. <div className="flex gap-2">
  253. <Button
  254. onClick={handleTriggerImport}
  255. disabled={processing || !importDetail.file}
  256. size="sm"
  257. variant="default"
  258. >
  259. {processing ? 'Processing...' : 'Start Import'}
  260. </Button>
  261. <Button
  262. onClick={handleCalculateSummaries}
  263. disabled={calculating || processing}
  264. size="sm"
  265. variant="secondary"
  266. >
  267. {calculating ? 'Calculating...' : 'Calculate Summaries'}
  268. </Button>
  269. </div>
  270. </div>
  271. </CardHeader>
  272. <CardContent>
  273. {importStatus === 'processing' && (
  274. <div className="flex items-center gap-2 text-sm text-blue-600">
  275. <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"></div>
  276. Import is currently processing...
  277. </div>
  278. )}
  279. {importStatus === 'completed' && (
  280. <div className="text-sm text-green-600">
  281. Import completed successfully!
  282. </div>
  283. )}
  284. {importStatus === 'failed' && (
  285. <div className="text-sm text-red-600">
  286. Import failed. Please check the logs for details.
  287. </div>
  288. )}
  289. {!importDetail.file && (
  290. <div className="text-sm text-yellow-600">
  291. No file attached. Please upload a file before starting import.
  292. </div>
  293. )}
  294. </CardContent>
  295. </Card>
  296. <Card>
  297. <CardHeader>
  298. <CardTitle>Cintas Summaries</CardTitle>
  299. </CardHeader>
  300. <CardContent>
  301. {importDetail.cintasSummaries.length === 0 ? (
  302. <p className="text-muted-foreground">No summaries calculated yet</p>
  303. ) : (
  304. <div className="overflow-x-auto">
  305. <table className="w-full text-sm">
  306. <thead>
  307. <tr className="border-b">
  308. <th className="text-left py-2">Week</th>
  309. <th className="text-right py-2">TRR Total</th>
  310. <th className="text-right py-2">4WK Averages</th>
  311. <th className="text-right py-2">TRR + 4WK</th>
  312. <th className="text-right py-2">Power Adds</th>
  313. </tr>
  314. </thead>
  315. <tbody>
  316. {importDetail.cintasSummaries.map((summary) => (
  317. <tr key={summary.id} className="border-b">
  318. <td className="py-2">{summary.week}</td>
  319. <td className="text-right py-2">{summary.trrTotal}</td>
  320. <td className="text-right py-2">{summary.fourWkAverages}</td>
  321. <td className="text-right py-2">{summary.trrPlus4Wk}</td>
  322. <td className="text-right py-2">{summary.powerAdds}</td>
  323. </tr>
  324. ))}
  325. </tbody>
  326. </table>
  327. </div>
  328. )}
  329. </CardContent>
  330. </Card>
  331. </div>
  332. <div className="flex justify-end gap-2 mt-6">
  333. <Button onClick={() => onOpenChange(false)}>Close</Button>
  334. </div>
  335. </DialogContent>
  336. </Dialog>
  337. );
  338. }