diff --git a/components/incidents/offline-sync.tsx b/components/incidents/offline-sync.tsx index 3ff20c3..b1165ce 100644 --- a/components/incidents/offline-sync.tsx +++ b/components/incidents/offline-sync.tsx @@ -1,10 +1,11 @@ 'use client' -import { useEffect, useState, useCallback } from 'react' +import { useEffect, useState, useCallback, useRef } from 'react' export function OfflineSync() { const [pendingCount, setPendingCount] = useState(0) const [syncing, setSyncing] = useState(false) + const syncingRef = useRef(false) async function checkPending() { const { getPendingCount } = await import('@/lib/offline/db') @@ -12,7 +13,8 @@ export function OfflineSync() { } const syncNow = useCallback(async () => { - if (syncing) return + if (syncingRef.current) return + syncingRef.current = true setSyncing(true) try { const { getPendingReports, removePendingReport } = await import('@/lib/offline/db') @@ -27,7 +29,7 @@ export function OfflineSync() { if (report.medical_status) fd.append('medical_status', report.medical_status) try { - const res = await fetch('/api/incidents', { method: 'POST', body: fd }) + const res = await fetch('/ims/api/incidents', { method: 'POST', body: fd }) if (res.ok && report.id != null) { await removePendingReport(report.id) } @@ -36,10 +38,11 @@ export function OfflineSync() { } } } finally { + syncingRef.current = false await checkPending() setSyncing(false) } - }, [syncing]) + }, []) useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect