open-webui/src/routes/(app)/admin/+page.svelte

120 lines
3.4 KiB
Svelte
Raw Normal View History

2023-11-19 09:13:59 +01:00
<script>
import { WEBUI_API_BASE_URL } from '$lib/constants';
import { config, user } from '$lib/stores';
import { goto } from '$app/navigation';
import { onMount } from 'svelte';
import toast from 'svelte-french-toast';
2023-12-26 20:00:56 +01:00
import { updateUserRole, getUsers } from '$lib/apis/users';
2023-11-19 09:13:59 +01:00
let loaded = false;
let users = [];
2023-12-26 20:00:56 +01:00
const updateRoleHandler = async (id, role) => {
const res = await updateUserRole(localStorage.token, id, role).catch((error) => {
toast.error(error);
return null;
});
2023-11-19 09:13:59 +01:00
if (res) {
2023-12-26 20:00:56 +01:00
users = await getUsers(localStorage.token);
2023-11-19 09:13:59 +01:00
}
};
onMount(async () => {
2023-12-26 12:28:30 +01:00
if ($user?.role !== 'admin') {
2023-11-19 09:13:59 +01:00
await goto('/');
} else {
2023-12-26 20:00:56 +01:00
users = await getUsers(localStorage.token);
2023-11-19 09:13:59 +01:00
}
loaded = true;
});
</script>
<div
class=" bg-white dark:bg-gray-800 dark:text-gray-100 min-h-screen w-full flex justify-center font-mona"
>
{#if loaded}
<div class="w-full max-w-3xl px-10 md:px-16 min-h-screen flex flex-col">
<div class="py-10 w-full">
<div class=" flex flex-col justify-center">
<div class=" text-2xl font-semibold">Users ({users.length})</div>
<div class=" text-gray-500 text-xs font-medium mt-1">
Click on the user role cell in the table to change a user's role.
</div>
<hr class=" my-3 dark:border-gray-600" />
<div class="scrollbar-hidden relative overflow-x-auto whitespace-nowrap">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead
class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400"
>
<tr>
<th scope="col" class="px-6 py-3"> Name </th>
<th scope="col" class="px-6 py-3"> Email </th>
<th scope="col" class="px-6 py-3"> Role </th>
<!-- <th scope="col" class="px-6 py-3"> Action </th> -->
</tr>
</thead>
<tbody>
{#each users as user}
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
>
<div class="flex flex-row">
<img
class=" rounded-full max-w-[30px] max-h-[30px] object-cover mr-4"
src={user.profile_image_url}
/>
<div class=" font-semibold md:self-center">{user.name}</div>
</div>
</th>
<td class="px-6 py-4"> {user.email} </td>
<td class="px-6 py-4">
<button
2023-11-20 03:27:56 +01:00
class=" dark:text-white underline"
2023-11-19 09:13:59 +01:00
on:click={() => {
if (user.role === 'user') {
2023-12-26 20:00:56 +01:00
updateRoleHandler(user.id, 'admin');
2023-11-19 09:13:59 +01:00
} else if (user.role === 'pending') {
2023-12-26 20:00:56 +01:00
updateRoleHandler(user.id, 'user');
2023-11-19 09:13:59 +01:00
} else {
2023-12-26 20:00:56 +01:00
updateRoleHandler(user.id, 'pending');
2023-11-19 09:13:59 +01:00
}
}}>{user.role}</button
>
</td>
<!-- <td class="px-6 py-4 text-center">
<button class=" text-white underline"> Edit </button>
</td> -->
</tr>
{/each}
</tbody>
</table>
</div>
</div>
</div>
</div>
{/if}
</div>
<style>
.font-mona {
font-family: 'Mona Sans';
}
.scrollbar-hidden::-webkit-scrollbar {
display: none; /* for Chrome, Safari and Opera */
}
.scrollbar-hidden {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
</style>