summaryrefslogtreecommitdiff
path: root/hooks/use-sync-api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'hooks/use-sync-api.ts')
-rw-r--r--hooks/use-sync-api.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/hooks/use-sync-api.ts b/hooks/use-sync-api.ts
new file mode 100644
index 00000000..18421209
--- /dev/null
+++ b/hooks/use-sync-api.ts
@@ -0,0 +1,76 @@
+// hooks/use-sync-api.ts (API 호출 전용 훅들)
+import useSWR from 'swr'
+import useSWRMutation from 'swr/mutation'
+import { mutate } from 'swr'
+import { ApiClient } from '@/lib/api-utils'
+
+// 동기화 상태 API 훅
+export function useSyncStatusApi(contractId: number, targetSystem: string = 'SHI') {
+ const key = contractId ? `sync-status-${contractId}-${targetSystem}` : null
+
+ const { data, error, isLoading } = useSWR(
+ key,
+ async () => ApiClient.get('/sync/status', { contractId, targetSystem }),
+ {
+ refreshInterval: 30000,
+ revalidateOnFocus: true,
+ errorRetryCount: 1,
+ errorRetryInterval: 5000,
+ }
+ )
+
+ const refetch = () => {
+ if (key) mutate(key)
+ }
+
+ return {
+ syncStatus: data,
+ isLoading,
+ error,
+ refetch
+ }
+}
+
+// 동기화 트리거 API 훅
+export function useTriggerSyncApi() {
+ const { trigger, isMutating, error } = useSWRMutation(
+ 'trigger-sync',
+ async (key: string, { arg }: { arg: { contractId: number; targetSystem?: string } }) => {
+ return ApiClient.post('/sync/trigger', arg)
+ },
+ {
+ onSuccess: (data, key, config) => {
+ const { contractId, targetSystem = 'SHI' } = config.arg
+ // 관련 캐시 무효화
+ mutate(`sync-status-${contractId}-${targetSystem}`)
+ mutate(`sync-batches-${contractId}-${targetSystem}`)
+ },
+ }
+ )
+
+ return {
+ triggerSync: trigger,
+ isLoading: isMutating,
+ error
+ }
+}
+
+// 동기화 배치 API 훅
+export function useSyncBatchesApi(contractId: number, targetSystem: string = 'SHI') {
+ const key = contractId ? `sync-batches-${contractId}-${targetSystem}` : null
+
+ const { data, error, isLoading } = useSWR(
+ key,
+ async () => ApiClient.get('/sync/batches', { contractId, targetSystem }),
+ {
+ revalidateOnFocus: false,
+ errorRetryCount: 1,
+ }
+ )
+
+ return {
+ syncBatches: data,
+ isLoading,
+ error
+ }
+} \ No newline at end of file