From 0fbeaaf6237b62d5220da0418d05cae4487f29bf Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Mon, 12 Feb 2024 08:55:11 -0800
Subject: feat(settings): settings sync

---
 src/routes/settings/+page.svelte | 52 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

(limited to 'src/routes/settings/+page.svelte')

diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte
index 03dcedde..342e73a3 100644
--- a/src/routes/settings/+page.svelte
+++ b/src/routes/settings/+page.svelte
@@ -16,6 +16,8 @@
 	import locale from '$stores/locale.js';
 	import settings from '$stores/settings';
 	import LogInRestricted from '$lib/Error/LogInRestricted.svelte';
+	import root from '$lib/Utility/root';
+	import identity from '$stores/identity.js';
 
 	export let data;
 
@@ -66,6 +68,56 @@
 {#if data.user === undefined}
 	<LogInRestricted />
 {:else}
+	<button
+		on:click={() => {
+			if (!$settings.settingsSync) {
+				$settings.settingsSync = true;
+
+				fetch(root(`/api/configuration?id=${$identity.id}`)).then((response) => {
+					if (response.ok) {
+						response.json().then((data) => {
+							if (data && data.configuration) {
+								$settings = data.configuration;
+
+								addNotification(
+									options({
+										heading: 'Pulled remote configuration'
+									})
+								);
+							} else {
+								fetch(root(`/api/configuration`), {
+									method: 'PUT',
+									body: JSON.stringify($settings)
+								}).then((response) => {
+									if (response.ok)
+										addNotification(
+											options({
+												heading: 'Created remote configuration'
+											})
+										);
+								});
+							}
+						});
+					}
+				});
+			} else {
+				$settings.settingsSync = false;
+
+				addNotification(
+					options({
+						heading: 'Disabled settings sync'
+					})
+				);
+			}
+		}}
+	>
+		{$settings.settingsSync ? 'Disable' : 'Enable'} Settings Sync &amp; {$settings.settingsSync
+			? 'Keep Local Configuration'
+			: 'Pull Remote Configuration'}
+	</button>
+
+	<p />
+
 	<Category title={$locale().settings.rssFeeds.title} id="feeds">
 		<button
 			on:click={() => {
-- 
cgit v1.2.3

