devices page: implement delete option

This commit is contained in:
2025-01-09 18:55:16 -08:00
parent e764f78501
commit 99f4016eb3
7 changed files with 174 additions and 8 deletions

View File

@@ -0,0 +1,23 @@
import { error, redirect } from '@sveltejs/kit';
import { deleteDevice } from '$lib/server/devices/delete';
import type { Actions } from './$types';
export const actions = {
delete: async (event) => {
if (!event.locals.user) return error(401, 'Unauthorized');
const deviceId = Number.parseInt(event.params.id);
if (Number.isNaN(deviceId)) return error(400, 'Invalid device id');
const res = await deleteDevice(event.locals.user.id, deviceId);
switch (res._tag) {
case 'ok': {
return redirect(303, '/devices');
}
case 'err': {
const [status, message] = res.error;
return error(status, message);
}
}
},
} satisfies Actions;