refactor: extract useSync hook from page.tsx
This commit is contained in:
@@ -6,6 +6,7 @@ import { inventoryApi } from '@/lib/api';
|
|||||||
import { fetchAndCacheItems, syncOfflineOperations } from '@/lib/sync';
|
import { fetchAndCacheItems, syncOfflineOperations } from '@/lib/sync';
|
||||||
import { useScanner } from '@/hooks/useScanner';
|
import { useScanner } from '@/hooks/useScanner';
|
||||||
import { useStockAdjustment } from '@/hooks/useStockAdjustment';
|
import { useStockAdjustment } from '@/hooks/useStockAdjustment';
|
||||||
|
import { useSync } from '@/hooks/useSync';
|
||||||
import Scanner from '@/components/Scanner';
|
import Scanner from '@/components/Scanner';
|
||||||
import AIOnboarding from '@/components/AIOnboarding';
|
import AIOnboarding from '@/components/AIOnboarding';
|
||||||
import PageShell from '@/components/PageShell';
|
import PageShell from '@/components/PageShell';
|
||||||
@@ -55,7 +56,6 @@ export default function Home() {
|
|||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
const [editedItem, setEditedItem] = useState<Partial<Item>>({});
|
const [editedItem, setEditedItem] = useState<Partial<Item>>({});
|
||||||
const [trashReason, setTrashReason] = useState('Damaged');
|
const [trashReason, setTrashReason] = useState('Damaged');
|
||||||
const [syncing, setSyncing] = useState(false);
|
|
||||||
const [currentUser, setCurrentUser] = useState<any | null>(null);
|
const [currentUser, setCurrentUser] = useState<any | null>(null);
|
||||||
const [categories, setCategories] = useState<any[]>([]);
|
const [categories, setCategories] = useState<any[]>([]);
|
||||||
const [comparisonModal, setComparisonModal] = useState<{ show: boolean, newItem: any, existingItem: any, existingId: number | null }>({ show: false, newItem: null, existingItem: null, existingId: null });
|
const [comparisonModal, setComparisonModal] = useState<{ show: boolean, newItem: any, existingItem: any, existingId: number | null }>({ show: false, newItem: null, existingItem: null, existingId: null });
|
||||||
@@ -106,6 +106,12 @@ export default function Home() {
|
|||||||
onInventoryUpdate: setInventory
|
onInventoryUpdate: setInventory
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { syncing, handleSync } = useSync({
|
||||||
|
isOnline,
|
||||||
|
currentUser,
|
||||||
|
onInventoryUpdate: setInventory
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!localStorage.getItem('inventory_token')) {
|
if (!localStorage.getItem('inventory_token')) {
|
||||||
window.location.href = '/login';
|
window.location.href = '/login';
|
||||||
@@ -248,23 +254,6 @@ export default function Home() {
|
|||||||
loadInventory();
|
loadInventory();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSync = useCallback(async () => {
|
|
||||||
if (!isOnline || !currentUser) return;
|
|
||||||
setSyncing(true);
|
|
||||||
try {
|
|
||||||
const result = await syncOfflineOperations(currentUser.id);
|
|
||||||
if (result.success > 0) {
|
|
||||||
toast.success(`Synced ${result.success} operations!`);
|
|
||||||
}
|
|
||||||
await loadInventory();
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Sync failed", error);
|
|
||||||
} finally {
|
|
||||||
setSyncing(false);
|
|
||||||
}
|
|
||||||
}, [isOnline, currentUser]);
|
|
||||||
|
|
||||||
|
|
||||||
const handleUpdateItem = async () => {
|
const handleUpdateItem = async () => {
|
||||||
if (!selectedItem) return;
|
if (!selectedItem) return;
|
||||||
try {
|
try {
|
||||||
|
|||||||
39
frontend/hooks/useSync.ts
Normal file
39
frontend/hooks/useSync.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { useCallback, useState } from 'react';
|
||||||
|
import { toast } from 'react-hot-toast';
|
||||||
|
import { syncOfflineOperations, fetchAndCacheItems } from '@/lib/sync';
|
||||||
|
|
||||||
|
interface UseSyncOptions {
|
||||||
|
isOnline: boolean;
|
||||||
|
currentUser: any;
|
||||||
|
onInventoryUpdate?: (items: any[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSync(options: UseSyncOptions) {
|
||||||
|
const { isOnline, currentUser, onInventoryUpdate } = options;
|
||||||
|
const [syncing, setSyncing] = useState(false);
|
||||||
|
|
||||||
|
const handleSync = useCallback(async () => {
|
||||||
|
if (!isOnline || !currentUser) return;
|
||||||
|
setSyncing(true);
|
||||||
|
try {
|
||||||
|
const result = await syncOfflineOperations(currentUser.id);
|
||||||
|
if (result.success > 0) {
|
||||||
|
toast.success(`Synced ${result.success} operations!`);
|
||||||
|
}
|
||||||
|
const fresh = await fetchAndCacheItems();
|
||||||
|
if (onInventoryUpdate) {
|
||||||
|
onInventoryUpdate(fresh);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Sync failed", error);
|
||||||
|
toast.error("Sync failed");
|
||||||
|
} finally {
|
||||||
|
setSyncing(false);
|
||||||
|
}
|
||||||
|
}, [isOnline, currentUser, onInventoryUpdate]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
syncing,
|
||||||
|
handleSync
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user