fix: resolve onboarding network error by using authenticated user context

Issue: createItem was using hardcoded userId: 1 without checking if currentUser was authenticated. Backend requires Bearer token from authenticated user, not userId parameter.

Changes:
- Use currentUser.id instead of hardcoded 1
- Check currentUser exists before attempting cloud sync
- Add error handling for cloud sync failures
- Graceful fallback to local-only save if not authenticated

Resolves 'Network Error' during onboarding item creation.
This commit is contained in:
2026-04-17 12:28:44 +03:00
parent 26b38ea27c
commit 9baa5612de

View File

@@ -165,11 +165,18 @@ export default function Home() {
await db.items.add(itemData); await db.items.add(itemData);
// 2. If online, try to push to backend immediately // 2. If online, try to push to backend immediately
if (isOnline) { if (isOnline && currentUser) {
await inventoryApi.createItem(1, itemData); try {
toast.success("Item saved to cloud catalog!"); await inventoryApi.createItem(currentUser.id, itemData);
} else { toast.success("Item saved to cloud catalog!");
} catch (err: any) {
console.error("Cloud sync failed:", err.message);
toast.error("Item saved locally. Cloud sync failed.");
}
} else if (!isOnline) {
toast.success("Item saved locally. Will sync when online."); toast.success("Item saved locally. Will sync when online.");
} else {
toast("Please log in to save to cloud.", { icon: '⚠️' });
} }
setShowOnboarding(false); setShowOnboarding(false);