Fix: DataSync listener bug preventing updates from client to server
This commit is contained in:
@@ -4,34 +4,52 @@ import { useEffect, useRef } from 'react';
|
||||
import { useFinanzasStore } from '@/lib/store';
|
||||
|
||||
export function DataSync() {
|
||||
// const isHydrated = useFinanzasStore(state => state._hasHydrated);
|
||||
const store = useFinanzasStore();
|
||||
const initialized = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
async function init() {
|
||||
// Prevent double init in StrictMode
|
||||
if (initialized.current) return;
|
||||
initialized.current = true;
|
||||
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/sync');
|
||||
// If 401 (unauthorized), stop here. User needs to login.
|
||||
if (res.status === 401) return;
|
||||
if (!res.ok) return;
|
||||
|
||||
const serverData = await res.json();
|
||||
|
||||
// Simple logic: if server has data (debts or cards or something), trust server.
|
||||
const hasServerData = serverData.fixedDebts?.length > 0 || serverData.creditCards?.length > 0;
|
||||
// Comprehensive check if server has ANY data
|
||||
const hasServerData =
|
||||
(serverData.fixedDebts?.length ?? 0) > 0 ||
|
||||
(serverData.variableDebts?.length ?? 0) > 0 ||
|
||||
(serverData.creditCards?.length ?? 0) > 0 ||
|
||||
(serverData.incomes?.length ?? 0) > 0 ||
|
||||
(serverData.serviceBills?.length ?? 0) > 0;
|
||||
|
||||
if (hasServerData) {
|
||||
console.log("Sync: Hydrating from Server");
|
||||
useFinanzasStore.setState(serverData);
|
||||
} else {
|
||||
// Server is empty, but we might have local data.
|
||||
// Push local data to server to initialize it.
|
||||
console.log("Sync: Initializing Server with Local Data");
|
||||
syncToServer(useFinanzasStore.getState());
|
||||
// Server is empty. If we have local data, push it.
|
||||
// But verify we actually have local data worth pushing to avoid overwriting with empty defaults unnecessarily
|
||||
const localState = useFinanzasStore.getState();
|
||||
const hasLocalData =
|
||||
localState.fixedDebts.length > 0 ||
|
||||
localState.variableDebts.length > 0 ||
|
||||
localState.creditCards.length > 0 ||
|
||||
localState.incomes.length > 0;
|
||||
|
||||
if (hasLocalData) {
|
||||
console.log("Sync: Server empty, pushing Local Data");
|
||||
syncToServer(localState);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Sync init error", e);
|
||||
} finally {
|
||||
// Mark as initialized so subsequent changes trigger sync
|
||||
initialized.current = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,10 +58,11 @@ export function DataSync() {
|
||||
|
||||
// Sync on change
|
||||
useEffect(() => {
|
||||
if (!initialized.current) return;
|
||||
|
||||
const unsub = useFinanzasStore.subscribe((state) => {
|
||||
syncToServer(state);
|
||||
// Only sync to server if we have finished initialization/hydration
|
||||
if (initialized.current) {
|
||||
syncToServer(state);
|
||||
}
|
||||
});
|
||||
return () => unsub();
|
||||
}, []);
|
||||
|
||||
12
data/db_renato97.json
Normal file
12
data/db_renato97.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"fixedDebts": [],
|
||||
"variableDebts": [],
|
||||
"creditCards": [],
|
||||
"cardPayments": [],
|
||||
"monthlyBudgets": [],
|
||||
"currentMonth": 1,
|
||||
"currentYear": 2026,
|
||||
"alerts": [],
|
||||
"serviceBills": [],
|
||||
"incomes": []
|
||||
}
|
||||
Reference in New Issue
Block a user