From a3d9db4c029e3a8b6196a9272df0ab2d7c699b0a Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 12 Feb 2024 12:04:43 -0800 Subject: feat(settingssync): last push pull times --- src/lib/Settings/Categories/SettingSync.svelte | 7 ++++++ src/routes/+layout.svelte | 5 ++++ src/stores/settings.ts | 34 ++++++++++++++++++++++---- src/stores/settingsSyncTimes.ts | 13 ++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/stores/settingsSyncTimes.ts diff --git a/src/lib/Settings/Categories/SettingSync.svelte b/src/lib/Settings/Categories/SettingSync.svelte index 54962b44..61fc6221 100644 --- a/src/lib/Settings/Categories/SettingSync.svelte +++ b/src/lib/Settings/Categories/SettingSync.svelte @@ -6,6 +6,7 @@ import { getNotificationsContext } from 'svelte-notifications'; import SettingHint from '../SettingHint.svelte'; import locale from '$stores/locale'; + import settingsSyncTimes from '$stores/settingsSyncTimes'; const { addNotification } = getNotificationsContext(); @@ -87,4 +88,10 @@ > {$locale().settings.settingsSync.buttons.disable} + +

+ + Last Push: {$locale().dateFormatter($settingsSyncTimes.lastPush)} +
+ Last Pull: {$locale().dateFormatter($settingsSyncTimes.lastPull)} {/if} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 64f3d28d..dc3f371b 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -24,6 +24,7 @@ import { injectSpeedInsights } from '@vercel/speed-insights/sveltekit'; import subtitles from '$lib/Data/Static/subtitles.json'; import settingsSyncPulled from '$stores/settingsSyncPulled'; + import settingsSyncTimes from '$stores/settingsSyncTimes'; injectSpeedInsights(); @@ -82,6 +83,10 @@ console.log('Pulled remote configuration'); settings.set(data.configuration); settingsSyncPulled.set(true); + settingsSyncTimes.set({ + lastPull: new Date(), + lastPush: new Date(data.updated_at + 'Z') + }); } }); }); 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({ + lastPush: new Date(), + lastPull: new Date() +}); + +export default settingsSyncPulled; -- cgit v1.2.3