aboutsummaryrefslogtreecommitdiff
path: root/src/stores
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-02-12 12:04:43 -0800
committerFuwn <[email protected]>2024-02-12 12:04:43 -0800
commita3d9db4c029e3a8b6196a9272df0ab2d7c699b0a (patch)
tree76c9d34008595e5b39c42e254503241adbf103dd /src/stores
parentfix(configuration): push updated_at (diff)
downloaddue.moe-a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a.tar.xz
due.moe-a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a.zip
feat(settingssync): last push pull times
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/settings.ts34
-rw-r--r--src/stores/settingsSyncTimes.ts13
2 files changed, 42 insertions, 5 deletions
diff --git a/src/stores/settings.ts b/src/stores/settings.ts
index e92d4150..31b047e2 100644
--- a/src/stores/settings.ts
+++ b/src/stores/settings.ts
@@ -2,6 +2,8 @@ import { browser } from '$app/environment';
import root from '$lib/Utility/root';
import { get, writable } from 'svelte/store';
import settingsSyncPulled from './settingsSyncPulled';
+import settingsSyncTimes from './settingsSyncTimes';
+import identity from './identity';
export interface Settings {
cacheMangaMinutes: number;
@@ -142,11 +144,33 @@ settings.subscribe((value) => {
if (browser) localStorage.setItem('settings', JSON.stringify(value));
if (value.settingsSync && get(settingsSyncPulled) == true)
- fetch(root(`/api/configuration`), {
- method: 'PUT',
- body: JSON.stringify(value)
- }).then((response) => {
- if (response.ok) console.log('Pushed local configuration');
+ fetch(root(`/api/configuration?id=${get(identity).id}`)).then((response) => {
+ if (response.ok)
+ response.json().then((data) => {
+ const isEqualsJson = (object1: Settings, object2: Settings) => {
+ type AnyObject = { [key: string]: unknown };
+
+ return (
+ Object.keys(object1).length === Object.keys(object2).length &&
+ Object.keys(object1).every(
+ (key) =>
+ (object1 as unknown as AnyObject)[key] == (object2 as unknown as AnyObject)[key]
+ )
+ );
+ };
+
+ if (data && data.configuration && !isEqualsJson(data.configuration, value)) {
+ console.log(isEqualsJson(data.configuration, value));
+ fetch(root(`/api/configuration`), {
+ method: 'PUT',
+ body: JSON.stringify(value)
+ }).then((response) => {
+ if (response.ok) console.log('Pushed local configuration');
+
+ settingsSyncTimes.update((times) => ({ ...times, lastPush: new Date() }));
+ });
+ }
+ });
});
});
diff --git a/src/stores/settingsSyncTimes.ts b/src/stores/settingsSyncTimes.ts
new file mode 100644
index 00000000..d9b790d2
--- /dev/null
+++ b/src/stores/settingsSyncTimes.ts
@@ -0,0 +1,13 @@
+import { writable } from 'svelte/store';
+
+interface SettingsSyncTimes {
+ lastPush: Date;
+ lastPull: Date;
+}
+
+const settingsSyncPulled = writable<SettingsSyncTimes>({
+ lastPush: new Date(),
+ lastPull: new Date()
+});
+
+export default settingsSyncPulled;