From cee7aa34ae9f3b9b567924798620ed663a24dd45 Mon Sep 17 00:00:00 2001 From: Ayush Sehrawat <69469790+AyushSehrawat@users.noreply.github.com> Date: Sat, 3 Feb 2024 00:51:08 +0530 Subject: [PATCH] feat: added some checks to save settings (#196) --- frontend/src/lib/forms/helpers.ts | 4 ++ .../src/routes/settings/about/+page.svelte | 47 ++++++++++++++++++- .../routes/settings/content/+page.server.ts | 18 ++++++- .../routes/settings/general/+page.server.ts | 18 ++++++- .../settings/mediaserver/+page.server.ts | 18 ++++++- .../routes/settings/scrapers/+page.server.ts | 18 ++++++- 6 files changed, 117 insertions(+), 6 deletions(-) diff --git a/frontend/src/lib/forms/helpers.ts b/frontend/src/lib/forms/helpers.ts index c089b4c4..492a18c3 100644 --- a/frontend/src/lib/forms/helpers.ts +++ b/frontend/src/lib/forms/helpers.ts @@ -3,6 +3,7 @@ import { z } from 'zod'; // General Settings ----------------------------------------------------------------------------------- export const generalSettingsToGet: string[] = ['debug', 'log', 'symlink', 'real_debrid']; +export const generalSettingsServices: string[] = ['symlink', 'real_debrid']; export const generalSettingsSchema = z.object({ debug: z.boolean().default(true), @@ -51,6 +52,7 @@ export function generalSettingsToSet(form: SuperValidated // Content Settings ----------------------------------------------------------------------------------- export const contentSettingsToGet: string[] = ['content']; +export const contentSettingsServices: string[] = ['content']; export const contentSettingsSchema = z.object({ overseerr_enabled: z.boolean().default(false), @@ -126,6 +128,7 @@ export function contentSettingsToSet(form: SuperValidated // Media Server Settings ----------------------------------------------------------------------------------- export const mediaServerSettingsToGet: string[] = ['plex']; +export const mediaServerSettingsServices: string[] = ['plex']; export const mediaServerSettingsSchema = z.object({ plex_token: z.string().optional().default(''), @@ -154,6 +157,7 @@ export function mediaServerSettingsToSet(form: SuperValidated @@ -49,6 +67,31 @@

About

Know what you're running.

+
+

+ {formatWords('Version')} +

+
+

+ {version} +

+ +
+
{#each Object.keys(aboutData) as key}
diff --git a/frontend/src/routes/settings/content/+page.server.ts b/frontend/src/routes/settings/content/+page.server.ts index b3bf7981..8ab7dbb2 100644 --- a/frontend/src/routes/settings/content/+page.server.ts +++ b/frontend/src/routes/settings/content/+page.server.ts @@ -1,10 +1,11 @@ import type { PageServerLoad, Actions } from './$types'; import { fail, error, redirect } from '@sveltejs/kit'; import { message, superValidate } from 'sveltekit-superforms/server'; -import { saveSettings } from '$lib/helpers'; +import { saveSettings, formatWords } from '$lib/helpers'; import { contentSettingsSchema, contentSettingsToGet, + contentSettingsServices, contentSettingsToPass, contentSettingsToSet } from '$lib/forms/helpers'; @@ -48,6 +49,21 @@ export const actions: Actions = { }); } + const data = await event.fetch('http://127.0.0.1:8080/services'); + const services = await data.json(); + const allServicesTrue: boolean = contentSettingsServices.every( + (service) => services.data[service] === true + ); + if (!allServicesTrue) { + return message( + form, + `${contentSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`, + { + status: 400 + } + ); + } + if (event.url.searchParams.get('onboarding') === 'true') { redirect(302, '/onboarding/4'); } diff --git a/frontend/src/routes/settings/general/+page.server.ts b/frontend/src/routes/settings/general/+page.server.ts index e4d81722..6e420686 100644 --- a/frontend/src/routes/settings/general/+page.server.ts +++ b/frontend/src/routes/settings/general/+page.server.ts @@ -1,10 +1,11 @@ import type { PageServerLoad, Actions } from './$types'; import { fail, error, redirect } from '@sveltejs/kit'; import { message, superValidate } from 'sveltekit-superforms/server'; -import { saveSettings } from '$lib/helpers'; +import { saveSettings, formatWords } from '$lib/helpers'; import { generalSettingsSchema, generalSettingsToGet, + generalSettingsServices, generalSettingsToPass, generalSettingsToSet } from '$lib/forms/helpers'; @@ -50,6 +51,21 @@ export const actions: Actions = { }); } + const data = await event.fetch('http://127.0.0.1:8080/services'); + const services = await data.json(); + const allServicesTrue: boolean = generalSettingsServices.every( + (service) => services.data[service] === true + ); + if (!allServicesTrue) { + return message( + form, + `${generalSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`, + { + status: 400 + } + ); + } + if (event.url.searchParams.get('onboarding') === 'true') { redirect(302, '/onboarding/2'); } diff --git a/frontend/src/routes/settings/mediaserver/+page.server.ts b/frontend/src/routes/settings/mediaserver/+page.server.ts index 16fbb396..a563079e 100644 --- a/frontend/src/routes/settings/mediaserver/+page.server.ts +++ b/frontend/src/routes/settings/mediaserver/+page.server.ts @@ -1,10 +1,11 @@ import type { PageServerLoad, Actions } from './$types'; import { fail, error, redirect } from '@sveltejs/kit'; import { message, superValidate } from 'sveltekit-superforms/server'; -import { saveSettings } from '$lib/helpers'; +import { saveSettings, formatWords } from '$lib/helpers'; import { mediaServerSettingsSchema, mediaServerSettingsToGet, + mediaServerSettingsServices, mediaServerSettingsToPass, mediaServerSettingsToSet } from '$lib/forms/helpers'; @@ -48,6 +49,21 @@ export const actions: Actions = { }); } + const data = await event.fetch('http://127.0.0.1:8080/services'); + const services = await data.json(); + const allServicesTrue: boolean = mediaServerSettingsServices.every( + (service) => services.data[service] === true + ); + if (!allServicesTrue) { + return message( + form, + `${mediaServerSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`, + { + status: 400 + } + ); + } + if (event.url.searchParams.get('onboarding') === 'true') { redirect(302, '/onboarding/3'); } diff --git a/frontend/src/routes/settings/scrapers/+page.server.ts b/frontend/src/routes/settings/scrapers/+page.server.ts index 216d8577..d3d73054 100644 --- a/frontend/src/routes/settings/scrapers/+page.server.ts +++ b/frontend/src/routes/settings/scrapers/+page.server.ts @@ -1,10 +1,11 @@ import type { PageServerLoad, Actions } from './$types'; import { fail, error, redirect } from '@sveltejs/kit'; import { message, superValidate } from 'sveltekit-superforms/server'; -import { saveSettings } from '$lib/helpers'; +import { saveSettings, formatWords } from '$lib/helpers'; import { scrapersSettingsSchema, scrapersSettingsToGet, + scrapersSettingsServices, scrapersSettingsToPass, scrapersSettingsToSet } from '$lib/forms/helpers'; @@ -48,6 +49,21 @@ export const actions: Actions = { }); } + const data = await event.fetch('http://127.0.0.1:8080/services'); + const services = await data.json(); + const allServicesTrue: boolean = scrapersSettingsServices.every( + (service) => services.data[service] === true + ); + if (!allServicesTrue) { + return message( + form, + `${scrapersSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`, + { + status: 400 + } + ); + } + if (event.url.searchParams.get('onboarding') === 'true') { redirect(302, '/?onboarding=true'); }