dynamic form updater
This commit is contained in:
@@ -4,7 +4,8 @@ import { checkAuth } from '$api/authService';
|
||||
import { getTracks } from '$api/lookupService';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { configFile, type Session } from '$models/config';
|
||||
import { configFile, type Config, type Session } from '$models/config';
|
||||
import { set } from 'lodash-es';
|
||||
|
||||
export const load = async (event: RequestEvent) => {
|
||||
const isAuth = await checkAuth(event);
|
||||
@@ -31,48 +32,40 @@ type SessionField =
|
||||
| 'hourOfDay';
|
||||
|
||||
export const actions = {
|
||||
event: async (event: RequestEvent) => {
|
||||
const formData = await event.request.formData();
|
||||
const id = formData.get('id') as string;
|
||||
const restart = formData.get('restart') === 'true';
|
||||
const object: any = {};
|
||||
update: async (event: RequestEvent) => {
|
||||
const { id, restart, file, data } = await destructureFormData(event);
|
||||
|
||||
const sessions: Array<Record<SessionField, string | number>> = [];
|
||||
formData.forEach((value, key) => {
|
||||
const sessionMatch = key.match(/sessions\[(\d+)\]\[(\w+)\]/);
|
||||
if (sessionMatch) {
|
||||
const index = parseInt(sessionMatch[1]);
|
||||
const field = sessionMatch[2] as SessionField;
|
||||
|
||||
if (!sessions[index]) {
|
||||
sessions[index] = {
|
||||
hourOfDay: 0,
|
||||
dayOfWeekend: 0,
|
||||
timeMultiplier: 0,
|
||||
sessionType: '',
|
||||
sessionDurationMinutes: 0
|
||||
};
|
||||
}
|
||||
|
||||
// Assign the value to the corresponding session field
|
||||
sessions[index][field] = value !== '' && !Number.isNaN(+value) ? +value : (value as string);
|
||||
}
|
||||
});
|
||||
object.sessions = sessions;
|
||||
formData.forEach((value, key) => {
|
||||
switch (key) {
|
||||
case 'id':
|
||||
case 'restart':
|
||||
case 'sessions':
|
||||
return;
|
||||
default:
|
||||
object[key] = value != '' && !Number.isNaN(+value) ? +value : value;
|
||||
}
|
||||
});
|
||||
await updateConfig(event, id, configFile.event, object, true, restart);
|
||||
redirect(303, '/dashboard');
|
||||
await updateConfig(event, id, file, data, true, restart);
|
||||
}
|
||||
} satisfies Actions;
|
||||
|
||||
async function destructureFormData(
|
||||
event: RequestEvent
|
||||
): Promise<{ id: string; restart: boolean; data: Config; file: configFile }> {
|
||||
const formData = await event.request.formData();
|
||||
const id = formData.get('id') as string;
|
||||
const restart = formData.get('restart') === 'true';
|
||||
const file = formData.get('file') as configFile;
|
||||
const object: any = {};
|
||||
formData.forEach((value, key) => {
|
||||
switch (key) {
|
||||
case 'id':
|
||||
case 'restart':
|
||||
case 'file':
|
||||
return;
|
||||
default:
|
||||
set(object, key, parseFormField(value));
|
||||
}
|
||||
});
|
||||
return { id, restart, data: object, file };
|
||||
}
|
||||
|
||||
function parseFormField(value: FormDataEntryValue): string | number {
|
||||
return value !== '' && !Number.isNaN(+value) ? +value : (value as string);
|
||||
}
|
||||
|
||||
function tryParse(str: string) {
|
||||
try {
|
||||
return JSON.parse(str);
|
||||
|
||||
Reference in New Issue
Block a user