Skip to content

Commit

Permalink
fix: better form validations and improvements (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushSehrawat authored Feb 4, 2024
1 parent 4dd9c5f commit 848c79b
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 116 deletions.
60 changes: 60 additions & 0 deletions frontend/src/lib/forms/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
import { type SuperValidated } from 'sveltekit-superforms';
import { z } from 'zod';

/**
* Sets the settings in memory in the backend.
*
* @param fetch - The fetch function used to make the request.
* @param toSet - The settings to be set.
* @param toCheck - The services to check.
* @returns An object containing the settings data and a boolean indicating if all the given services are true or not.
*/
export async function setSettings(fetch: any, toSet: any, toCheck: string[]) {
const settings = await fetch('http://127.0.0.1:8080/settings/set', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(toSet)
});
const settingsData = await settings.json();

const services = await fetch('http://127.0.0.1:8080/services');
const data = await services.json();
const allServicesTrue: boolean = toCheck.every((service) => data.data[service] === true);

return {
data: settingsData,
allServicesTrue: allServicesTrue
};
}

/**
* Saves the settings from memory to the json file in the backend.
* @param fetch - The fetch function used to make the request.
* @returns A promise that resolves to an object containing the response data.
*/
export async function saveSettings(fetch: any) {
const data = await fetch('http://127.0.0.1:8080/settings/save', {
method: 'POST'
});
const response = await data.json();

return {
data: response
};
}

/**
* Loads settings from the json to memory in backend.
* @param fetch - The fetch function used to make the HTTP request.
* @returns A promise that resolves to an object containing the loaded settings.
*/
export async function loadSettings(fetch: any) {
const data = await fetch('http://127.0.0.1:8080/settings/load', {
method: 'GET'
});
const response = await data.json();

return {
data: response
};
}

// General Settings -----------------------------------------------------------------------------------
export const generalSettingsToGet: string[] = ['debug', 'log', 'symlink', 'real_debrid'];
export const generalSettingsServices: string[] = ['symlink', 'real_debrid'];
Expand Down
24 changes: 0 additions & 24 deletions frontend/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,3 @@ export function convertIcebergItemsToObject(items: IcebergItem[]) {

return result;
}

export async function saveSettings(fetch: any, toSet: any) {
const data = await fetch('http://127.0.0.1:8080/settings/set', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(toSet)
});

const saveSettings = await fetch('http://127.0.0.1:8080/settings/save', {
method: 'POST'
});

const loadSettings = await fetch('http://127.0.0.1:8080/settings/load', {
method: 'GET'
});

return {
data,
saveSettings,
loadSettings
};
}
46 changes: 23 additions & 23 deletions frontend/src/routes/settings/content/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { PageServerLoad, Actions } from './$types';
import { fail, error, redirect } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms/server';
import { saveSettings, formatWords } from '$lib/helpers';
import { formatWords } from '$lib/helpers';
import {
setSettings,
saveSettings,
loadSettings,
contentSettingsSchema,
contentSettingsToGet,
contentSettingsServices,
Expand Down Expand Up @@ -41,33 +44,30 @@ export const actions: Actions = {
const toSet = contentSettingsToSet(form);

try {
const data = await saveSettings(event.fetch, toSet);
const data = await setSettings(event.fetch, toSet, contentSettingsServices);
if (!data.allServicesTrue) {
return message(
form,
`${contentSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`,
{
status: 400
}
);
}

const save = await saveSettings(event.fetch);
const load = await loadSettings(event.fetch);

if (event.url.searchParams.get('onboarding') === 'true') {
redirect(302, '/onboarding/4');
}

return message(form, 'Settings saved!');
} catch (e) {
console.error(e);
return message(form, 'Unable to save settings. API is down.', {
status: 400
});
}

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');
}

return message(form, 'Settings saved!');
}
};
46 changes: 23 additions & 23 deletions frontend/src/routes/settings/general/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { PageServerLoad, Actions } from './$types';
import { fail, error, redirect } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms/server';
import { saveSettings, formatWords } from '$lib/helpers';
import { formatWords } from '$lib/helpers';
import {
setSettings,
saveSettings,
loadSettings,
generalSettingsSchema,
generalSettingsToGet,
generalSettingsServices,
Expand Down Expand Up @@ -43,33 +46,30 @@ export const actions: Actions = {
const toSet = generalSettingsToSet(form);

try {
const data = await saveSettings(event.fetch, toSet);
const data = await setSettings(event.fetch, toSet, generalSettingsServices);
if (!data.allServicesTrue) {
return message(
form,
`${generalSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`,
{
status: 400
}
);
}

const save = await saveSettings(event.fetch);
const load = await loadSettings(event.fetch);

if (event.url.searchParams.get('onboarding') === 'true') {
redirect(302, '/onboarding/2');
}

return message(form, 'Settings saved!');
} catch (e) {
console.error(e);
return message(form, 'Unable to save settings. API is down.', {
status: 400
});
}

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');
}

return message(form, 'Settings saved!');
}
};
46 changes: 23 additions & 23 deletions frontend/src/routes/settings/mediaserver/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { PageServerLoad, Actions } from './$types';
import { fail, error, redirect } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms/server';
import { saveSettings, formatWords } from '$lib/helpers';
import { formatWords } from '$lib/helpers';
import {
setSettings,
saveSettings,
loadSettings,
mediaServerSettingsSchema,
mediaServerSettingsToGet,
mediaServerSettingsServices,
Expand Down Expand Up @@ -41,33 +44,30 @@ export const actions: Actions = {
const toSet = mediaServerSettingsToSet(form);

try {
const data = await saveSettings(event.fetch, toSet);
const data = await setSettings(event.fetch, toSet, mediaServerSettingsServices);
if (!data.allServicesTrue) {
return message(
form,
`${mediaServerSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`,
{
status: 400
}
);
}

const save = await saveSettings(event.fetch);
const load = await loadSettings(event.fetch);

if (event.url.searchParams.get('onboarding') === 'true') {
redirect(302, '/onboarding/3');
}

return message(form, 'Settings saved!');
} catch (e) {
console.error(e);
return message(form, 'Unable to save settings. API is down.', {
status: 400
});
}

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');
}

return message(form, 'Settings saved!');
}
};
46 changes: 23 additions & 23 deletions frontend/src/routes/settings/scrapers/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { PageServerLoad, Actions } from './$types';
import { fail, error, redirect } from '@sveltejs/kit';
import { message, superValidate } from 'sveltekit-superforms/server';
import { saveSettings, formatWords } from '$lib/helpers';
import { formatWords } from '$lib/helpers';
import {
setSettings,
saveSettings,
loadSettings,
scrapersSettingsSchema,
scrapersSettingsToGet,
scrapersSettingsServices,
Expand Down Expand Up @@ -41,33 +44,30 @@ export const actions: Actions = {
const toSet = scrapersSettingsToSet(form);

try {
const data = await saveSettings(event.fetch, toSet);
const data = await setSettings(event.fetch, toSet, scrapersSettingsServices);
if (!data.allServicesTrue) {
return message(
form,
`${scrapersSettingsServices.map(formatWords).join(', ')} service(s) failed to initialize. Please check your settings.`,
{
status: 400
}
);
}

const save = await saveSettings(event.fetch);
const load = await loadSettings(event.fetch);

if (event.url.searchParams.get('onboarding') === 'true') {
redirect(302, '/?onboarding=true');
}

return message(form, 'Settings saved!');
} catch (e) {
console.error(e);
return message(form, 'Unable to save settings. API is down.', {
status: 400
});
}

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');
}

return message(form, 'Settings saved!');
}
};

0 comments on commit 848c79b

Please sign in to comment.