Skip to content

Commit

Permalink
feat: added some checks to save settings (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushSehrawat authored Feb 2, 2024
1 parent b2b49ab commit cee7aa3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 6 deletions.
4 changes: 4 additions & 0 deletions frontend/src/lib/forms/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -51,6 +52,7 @@ export function generalSettingsToSet(form: SuperValidated<GeneralSettingsSchema>

// Content Settings -----------------------------------------------------------------------------------
export const contentSettingsToGet: string[] = ['content'];
export const contentSettingsServices: string[] = ['content'];

export const contentSettingsSchema = z.object({
overseerr_enabled: z.boolean().default(false),
Expand Down Expand Up @@ -126,6 +128,7 @@ export function contentSettingsToSet(form: SuperValidated<ContentSettingsSchema>

// Media Server Settings -----------------------------------------------------------------------------------
export const mediaServerSettingsToGet: string[] = ['plex'];
export const mediaServerSettingsServices: string[] = ['plex'];

export const mediaServerSettingsSchema = z.object({
plex_token: z.string().optional().default(''),
Expand Down Expand Up @@ -154,6 +157,7 @@ export function mediaServerSettingsToSet(form: SuperValidated<MediaServerSetting

// Scrapers Settings -----------------------------------------------------------------------------------
export const scrapersSettingsToGet: string[] = ['scraping'];
export const scrapersSettingsServices: string[] = ['scraping'];

export const scrapersSettingsSchema = z.object({
after_2: z.number().nonnegative().default(0.5),
Expand Down
47 changes: 45 additions & 2 deletions frontend/src/routes/settings/about/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import { Separator } from '$lib/components/ui/separator';
import { formatWords } from '$lib/helpers';
import * as Alert from '$lib/components/ui/alert';
import { Button } from '$lib/components/ui/button';
import { Loader2, MoveUpRight } from 'lucide-svelte';
import { toast } from 'svelte-sonner';
export let data: PageData;
Expand All @@ -12,7 +15,6 @@
interface AboutData {
[key: string]: any;
version: string;
host_path: string;
container_path: string;
}
Expand All @@ -24,14 +26,30 @@
};
const aboutData: AboutData = {
version,
host_path,
container_path
};
const supportData: SupportData = {
github: 'https://github.com/dreulavelle/iceberg',
discord: 'https://discord.gg/wDgVdH8vNM'
};
let updateLoading = false;
async function getLatestVersion() {
updateLoading = true;
const data = await fetch(
'https://raw.githubusercontent.com/dreulavelle/iceberg/main/backend/utils/default_settings.json'
);
const json = await data.json();
updateLoading = false;
if (json.version !== version) {
toast.warning('A new version is available! Checkout the changelog on GitHub.');
} else {
toast.success('You are running the latest version.');
}
}
</script>

<svelte:head>
Expand All @@ -49,6 +67,31 @@
<h2 class="text-xl md:text-2xl font-semibold mt-2">About</h2>
<p class="text-sm md:text-base text-muted-foreground mb-2">Know what you're running.</p>
<div class="flex flex-col gap-4 w-full">
<div class="flex flex-col md:flex-row items-start md:items-center mb-2">
<h3 class="text-sm md:text-base font-semibold w-48 min-w-48 text-muted-foreground">
{formatWords('Version')}
</h3>
<div class="flex flex-wrap gap-2 w-full">
<p class="text-xs md:text-sm break-words p-2 rounded-md bg-secondary">
{version}
</p>
<Button
on:click={async () => {
await getLatestVersion();
}}
disabled={updateLoading}
variant="outline"
size="sm"
>
{#if updateLoading}
<Loader2 class="w-4 h-4 mr-1 animate-spin" />
{:else}
<MoveUpRight class="w-4 h-4 mr-1" />
{/if}
Check for updates
</Button>
</div>
</div>
{#each Object.keys(aboutData) as key}
<Separator />
<div class="flex flex-col md:flex-row items-start md:items-center mb-2">
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/routes/settings/content/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/routes/settings/general/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/routes/settings/mediaserver/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/routes/settings/scrapers/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
Expand Down

0 comments on commit cee7aa3

Please sign in to comment.