From 7fade0bb2f308741a5207ba69ba09fc368a48547 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Thu, 28 Dec 2023 23:12:58 -0800 Subject: [PATCH] feat: delete user button added to admin page --- src/lib/apis/users/index.ts | 32 ++++++++++++++++++-- src/routes/(app)/admin/+page.svelte | 45 ++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/lib/apis/users/index.ts b/src/lib/apis/users/index.ts index b1f9e5d9f..3fca8b99a 100644 --- a/src/lib/apis/users/index.ts +++ b/src/lib/apis/users/index.ts @@ -45,8 +45,9 @@ export const getUsers = async (token: string) => { if (!res.ok) throw await res.json(); return res.json(); }) - .catch((error) => { - console.log(error); + .catch((err) => { + console.log(err); + error = err.detail; return null; }); @@ -56,3 +57,30 @@ export const getUsers = async (token: string) => { return res ? res : []; }; + +export const deleteUserById = async (token: string, userId: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/users/${userId}`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err.detail; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; diff --git a/src/routes/(app)/admin/+page.svelte b/src/routes/(app)/admin/+page.svelte index 13078b3ca..5f5beeae3 100644 --- a/src/routes/(app)/admin/+page.svelte +++ b/src/routes/(app)/admin/+page.svelte @@ -6,7 +6,7 @@ import toast from 'svelte-french-toast'; - import { updateUserRole, getUsers } from '$lib/apis/users'; + import { updateUserRole, getUsers, deleteUserById } from '$lib/apis/users'; let loaded = false; let users = []; @@ -22,6 +22,16 @@ } }; + const deleteUserHandler = async (id) => { + const res = await deleteUserById(localStorage.token, id).catch((error) => { + toast.error(error); + return null; + }); + if (res) { + users = await getUsers(localStorage.token); + } + }; + onMount(async () => { if ($user?.role !== 'admin') { await goto('/'); @@ -55,7 +65,7 @@ Name Email Role - + Action @@ -63,15 +73,16 @@
user -
{user.name}
+
{user.name}
{user.email} @@ -89,9 +100,29 @@ }}>{user.role} - + + + {/each}