diff options
| author | Fuwn <[email protected]> | 2024-02-12 12:04:43 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-12 12:04:43 -0800 |
| commit | a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a (patch) | |
| tree | 76c9d34008595e5b39c42e254503241adbf103dd /src/stores/settings.ts | |
| parent | fix(configuration): push updated_at (diff) | |
| download | due.moe-a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a.tar.xz due.moe-a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a.zip | |
feat(settingssync): last push pull times
Diffstat (limited to 'src/stores/settings.ts')
| -rw-r--r-- | src/stores/settings.ts | 34 |
1 files changed, 29 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() })); + }); + } + }); }); }); |