Skip to content

Commit 1a9bbd4

Browse files
committed
Fix initial sync state after login
Now restores the correct state when logging in on a new device.
1 parent 8b27e0d commit 1a9bbd4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/components/SyncToggle/SyncToggle.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import { useEffect, useState, useRef } from 'react';
22
import ToggleButton from 'react-toggle-button';
3-
import { getDb, setRemoteUser, enableSync, disableSync, teardownReplication } from 'trace-search';
3+
import { getRemoteDb, closeRemoteDb, setRemoteUser, enableSync, disableSync, teardownReplication } from 'trace-search';
44
import Auth from "@aws-amplify/auth";
55
import NotificationAlert from "react-notification-alert";
66

7-
const getSyncEnabled = async () => {
8-
const db = await getDb();
9-
const settings = await db.get('settings');
10-
return settings.syncEnabled;
11-
}
12-
137
const setInitialSync = async (setIsLoggedIn, setSync) => {
148
try {
15-
await Auth.currentUserPoolUser();
9+
const user = await Auth.currentUserPoolUser();
1610

1711
setIsLoggedIn(true);
18-
setSync(await getSyncEnabled());
12+
13+
try {
14+
setRemoteUser(user);
15+
const db = await getRemoteDb();
16+
const settings = await db.get('settings');
17+
18+
if (!settings.syncEnabled) {
19+
await closeRemoteDb();
20+
}
21+
22+
setSync(settings.syncEnabled);
23+
} catch(e) {
24+
console.debug('Could not get sync status from remote DB!')
25+
console.debug(e);
26+
}
1927
}
2028
catch {
2129
setIsLoggedIn(false);

0 commit comments

Comments
 (0)