Skip to content

Commit

Permalink
fix/actualbudget#3128: listen to 'sync-event' to update user data whe…
Browse files Browse the repository at this point in the history
…n sync status changed, so 'userData.offline' is up to date and server status displayed is valid
  • Loading branch information
p-payet committed Jan 2, 2025
1 parent 1e65939 commit 3446ebc
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/desktop-client/src/components/LoggedInUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useLocation } from 'react-router-dom';

import { closeBudget, getUserData, signOut } from 'loot-core/client/actions';
import { type State } from 'loot-core/src/client/state-types';
import { listen } from 'loot-core/src/platform/client/fetch';
import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file';

import { useAuth } from '../auth/AuthProvider';
Expand Down Expand Up @@ -52,14 +53,35 @@ export function LoggedInUser({
const currentFile = remoteFiles.find(f => f.cloudFileId === cloudFileId);
const hasSyncedPrefs = useSelector((state: State) => state.prefs.synced);

useEffect(() => {
async function init() {
await dispatch(getUserData());
}
const initializeUserData = async () => {
await dispatch(getUserData());
setLoading(false);
};

init().then(() => setLoading(false));
useEffect(() => {
initializeUserData();
}, []);

useEffect(() => {
return listen('sync-event', ({ type }) => {
if (type === 'start') {
setLoading(true);

return;
}

const shouldReinitialize =
(type === 'success' && userData.offline) ||
(type === 'error' && !userData.offline);

if (shouldReinitialize) {
initializeUserData();
} else {
setLoading(false);
}
});
}, [userData.offline]);

async function onCloseBudget() {
await dispatch(closeBudget());
}
Expand Down

0 comments on commit 3446ebc

Please sign in to comment.