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';
|
import { useFinanzasStore } from '@/lib/store';
|
||||||
|
|
||||||
export function DataSync() {
|
export function DataSync() {
|
||||||
// const isHydrated = useFinanzasStore(state => state._hasHydrated);
|
|
||||||
const store = useFinanzasStore();
|
|
||||||
const initialized = useRef(false);
|
const initialized = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function init() {
|
async function init() {
|
||||||
|
// Prevent double init in StrictMode
|
||||||
if (initialized.current) return;
|
if (initialized.current) return;
|
||||||
initialized.current = true;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/sync');
|
const res = await fetch('/api/sync');
|
||||||
|
// If 401 (unauthorized), stop here. User needs to login.
|
||||||
|
if (res.status === 401) return;
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
|
|
||||||
const serverData = await res.json();
|
const serverData = await res.json();
|
||||||
|
|
||||||
// Simple logic: if server has data (debts or cards or something), trust server.
|
// Comprehensive check if server has ANY data
|
||||||
const hasServerData = serverData.fixedDebts?.length > 0 || serverData.creditCards?.length > 0;
|
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) {
|
if (hasServerData) {
|
||||||
console.log("Sync: Hydrating from Server");
|
console.log("Sync: Hydrating from Server");
|
||||||
useFinanzasStore.setState(serverData);
|
useFinanzasStore.setState(serverData);
|
||||||
} else {
|
} else {
|
||||||
// Server is empty, but we might have local data.
|
// Server is empty. If we have local data, push it.
|
||||||
// Push local data to server to initialize it.
|
// But verify we actually have local data worth pushing to avoid overwriting with empty defaults unnecessarily
|
||||||
console.log("Sync: Initializing Server with Local Data");
|
const localState = useFinanzasStore.getState();
|
||||||
syncToServer(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) {
|
} catch (e) {
|
||||||
console.error("Sync init error", 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
|
// Sync on change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!initialized.current) return;
|
|
||||||
|
|
||||||
const unsub = useFinanzasStore.subscribe((state) => {
|
const unsub = useFinanzasStore.subscribe((state) => {
|
||||||
syncToServer(state);
|
// Only sync to server if we have finished initialization/hydration
|
||||||
|
if (initialized.current) {
|
||||||
|
syncToServer(state);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return () => unsub();
|
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