chore: admin page refac

This commit is contained in:
Timothy J. Baek 2023-12-26 11:00:56 -08:00
parent 1303407f53
commit bb190245f7
2 changed files with 70 additions and 47 deletions

View File

@ -0,0 +1,58 @@
import { WEBUI_API_BASE_URL } from '$lib/constants';
export const updateUserRole = async (token: string, id: string, role: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
id: id,
role: role
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
error = error.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
export const getUsers = async (token: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/users`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
return null;
});
if (error) {
throw error;
}
return res ? res : [];
};

View File

@ -6,62 +6,27 @@
import toast from 'svelte-french-toast'; import toast from 'svelte-french-toast';
import { updateUserRole, getUsers } from '$lib/apis/users';
let loaded = false; let loaded = false;
let users = []; let users = [];
const updateUserRole = async (id, role) => { const updateRoleHandler = async (id, role) => {
const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, { const res = await updateUserRole(localStorage.token, id, role).catch((error) => {
method: 'POST', toast.error(error);
headers: { return null;
'Content-Type': 'application/json', });
Authorization: `Bearer ${localStorage.token}`
},
body: JSON.stringify({
id: id,
role: role
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
toast.error(error.detail);
return null;
});
if (res) { if (res) {
await getUsers(); users = await getUsers(localStorage.token);
} }
}; };
const getUsers = async () => {
const res = await fetch(`${WEBUI_API_BASE_URL}/users`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((error) => {
console.log(error);
toast.error(error.detail);
return null;
});
users = res ? res : [];
};
onMount(async () => { onMount(async () => {
if ($user?.role !== 'admin') { if ($user?.role !== 'admin') {
await goto('/'); await goto('/');
} else { } else {
await getUsers(); users = await getUsers(localStorage.token);
} }
loaded = true; loaded = true;
}); });
@ -115,11 +80,11 @@
class=" dark:text-white underline" class=" dark:text-white underline"
on:click={() => { on:click={() => {
if (user.role === 'user') { if (user.role === 'user') {
updateUserRole(user.id, 'admin'); updateRoleHandler(user.id, 'admin');
} else if (user.role === 'pending') { } else if (user.role === 'pending') {
updateUserRole(user.id, 'user'); updateRoleHandler(user.id, 'user');
} else { } else {
updateUserRole(user.id, 'pending'); updateRoleHandler(user.id, 'pending');
} }
}}>{user.role}</button }}>{user.role}</button
> >