From ef4c533ebacc2cdc97e518f30e9a9350004fcdfb Mon Sep 17 00:00:00 2001 From: dujinkim Date: Mon, 28 Apr 2025 02:13:30 +0000 Subject: ~20250428 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/form-list/repository.ts | 2 + lib/form-list/service.ts | 2 + .../table/formLists-table-toolbar-actions.tsx | 129 +++++++++++++++++---- lib/form-list/table/formLists-table.tsx | 7 +- lib/form-list/table/meta-sheet.tsx | 85 +++++++++----- 5 files changed, 172 insertions(+), 53 deletions(-) (limited to 'lib/form-list') diff --git a/lib/form-list/repository.ts b/lib/form-list/repository.ts index d3c555bf..9c7f6891 100644 --- a/lib/form-list/repository.ts +++ b/lib/form-list/repository.ts @@ -36,6 +36,8 @@ export async function selectFormLists( classLabel: tagTypeClassFormMappings.classLabel, formCode: tagTypeClassFormMappings.formCode, formName: tagTypeClassFormMappings.formName, + ep: tagTypeClassFormMappings.ep, + remark: tagTypeClassFormMappings.remark, createdAt: tagTypeClassFormMappings.createdAt, updatedAt: tagTypeClassFormMappings.updatedAt, // 프로젝트 정보 추가 diff --git a/lib/form-list/service.ts b/lib/form-list/service.ts index 310930be..9887609f 100644 --- a/lib/form-list/service.ts +++ b/lib/form-list/service.ts @@ -74,6 +74,8 @@ export async function getFormLists(input: GetFormListsSchema) { offset, limit: input.perPage, }); + + console.log("dbdata") const total = await countFormLists(tx, where); return { data, total }; diff --git a/lib/form-list/table/formLists-table-toolbar-actions.tsx b/lib/form-list/table/formLists-table-toolbar-actions.tsx index 96494607..cf874985 100644 --- a/lib/form-list/table/formLists-table-toolbar-actions.tsx +++ b/lib/form-list/table/formLists-table-toolbar-actions.tsx @@ -2,71 +2,150 @@ import * as React from "react" import { type Table } from "@tanstack/react-table" -import { Download, RefreshCcw, Upload } from "lucide-react" +import { Download, RefreshCcw } from "lucide-react" import { toast } from "sonner" import { exportTableToExcel } from "@/lib/export" import { Button } from "@/components/ui/button" import { ExtendedFormMappings } from "../validation" - - interface ItemsTableToolbarActionsProps { table: Table } export function FormListsTableToolbarActions({ table }: ItemsTableToolbarActionsProps) { const [isLoading, setIsLoading] = React.useState(false) + const [syncId, setSyncId] = React.useState(null) + const pollingRef = React.useRef(null) + + // Clean up polling on unmount + React.useEffect(() => { + return () => { + if (pollingRef.current) { + clearInterval(pollingRef.current) + } + } + }, []) - const syncForms = async () => { + const startFormSync = async () => { try { setIsLoading(true) - - // API 엔드포인트 호출 - const response = await fetch('/api/cron/forms') - + + // API 엔드포인트 호출 - 작업 시작만 요청 + const response = await fetch('/api/cron/forms/start', { + method: 'POST' + }) + if (!response.ok) { const errorData = await response.json() - throw new Error(errorData.error || 'Failed to sync forms') + throw new Error(errorData.error || 'Failed to start form sync') } - + const data = await response.json() - - // 성공 메시지 표시 - toast.success( - `Forms synced successfully! ${data.result.items} items processed.` - ) - - // 페이지 새로고침으로 테이블 데이터 업데이트 - window.location.reload() + + // 작업 ID 저장 + if (data.syncId) { + setSyncId(data.syncId) + toast.info('Form sync started. This may take a while...') + + // 상태 확인을 위한 폴링 시작 + startPolling(data.syncId) + } else { + throw new Error('No sync ID returned from server') + } } catch (error) { - console.error('Error syncing forms:', error) + console.error('Error starting form sync:', error) toast.error( error instanceof Error ? error.message - : 'An error occurred while syncing forms' + : 'An error occurred while starting form sync' ) - } finally { setIsLoading(false) } } - + + const startPolling = (id: string) => { + // 이전 폴링이 있다면 제거 + if (pollingRef.current) { + clearInterval(pollingRef.current) + } + + // 5초마다 상태 확인 + pollingRef.current = setInterval(async () => { + try { + const response = await fetch(`/api/cron/forms/status?id=${id}`) + + if (!response.ok) { + throw new Error('Failed to get sync status') + } + + const data = await response.json() + + if (data.status === 'completed') { + // 폴링 중지 + if (pollingRef.current) { + clearInterval(pollingRef.current) + pollingRef.current = null + } + + // 상태 초기화 + setIsLoading(false) + setSyncId(null) + + // 성공 메시지 표시 + toast.success( + `Forms synced successfully! ${data.result?.items || 0} items processed.` + ) + + // 테이블 데이터 업데이트 - 전체 페이지 새로고침 대신 데이터만 갱신 + table.resetRowSelection() + // 여기서 테이블 데이터를 다시 불러오는 함수를 호출 + // 예: refetchTableData() + // 또는 SWR/React Query 등을 사용하고 있다면 mutate() 호출 + + // 리로드가 꼭 필요하다면 아래 주석 해제 + // window.location.reload() + } else if (data.status === 'failed') { + // 에러 처리 + if (pollingRef.current) { + clearInterval(pollingRef.current) + pollingRef.current = null + } + + setIsLoading(false) + setSyncId(null) + toast.error(data.error || 'Sync failed') + } else if (data.status === 'processing') { + // 진행 상태 업데이트 (선택적) + if (data.progress) { + toast.info(`Sync in progress: ${data.progress}%`, { + id: `sync-progress-${id}`, + }) + } + } + } catch (error) { + console.error('Error checking sync status:', error) + } + }, 5000) // 5초마다 체크 + } return (
- {/** 4) Export 버튼 */} + {/** Sync Forms 버튼 */} - - {/** 4) Export 버튼 */} + + {/** Export 버튼 */} +
+ ); +}; interface ViewMetasProps { open: boolean @@ -51,7 +102,7 @@ export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { const [loading, setLoading] = React.useState(false) // Group columns by type for better organization - const groupedColumns = useMemo(() => { + const groupedColumns = React.useMemo(() => { if (!metadata?.columns) return {} return metadata.columns.reduce((acc, column) => { @@ -65,7 +116,7 @@ export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { }, [metadata]) // Types for the tabs - const columnTypes = useMemo(() => { + const columnTypes = React.useMemo(() => { return Object.keys(groupedColumns) }, [groupedColumns]) @@ -165,17 +216,7 @@ export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { {column.type} - {column.options ? ( -
- {column.options.map((option) => ( - - {option} - - ))} -
- ) : ( - "-" - )} +
))} @@ -198,7 +239,7 @@ export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { Key Label - {type === "select" && Options} + {type === "LIST" && Options} @@ -206,19 +247,9 @@ export function ViewMetas({ open, onOpenChange, form }: ViewMetasProps) { {column.key} {column.label} - {type === "select" && ( + {type === "LIST" && ( - {column.options ? ( -
- {column.options.map((option) => ( - - {option} - - ))} -
- ) : ( - "-" - )} +
)}
-- cgit v1.2.3