forked from open-webui/open-webui
		
	chore: admin page refac
This commit is contained in:
		
							parent
							
								
									1303407f53
								
							
						
					
					
						commit
						bb190245f7
					
				
					 2 changed files with 70 additions and 47 deletions
				
			
		
							
								
								
									
										58
									
								
								src/lib/apis/users/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/lib/apis/users/index.ts
									
										
									
									
									
										Normal 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 : []; | ||||
| }; | ||||
|  | @ -6,62 +6,27 @@ | |||
| 
 | ||||
| 	import toast from 'svelte-french-toast'; | ||||
| 
 | ||||
| 	import { updateUserRole, getUsers } from '$lib/apis/users'; | ||||
| 
 | ||||
| 	let loaded = false; | ||||
| 	let users = []; | ||||
| 
 | ||||
| 	const updateUserRole = async (id, role) => { | ||||
| 		const res = await fetch(`${WEBUI_API_BASE_URL}/users/update/role`, { | ||||
| 			method: 'POST', | ||||
| 			headers: { | ||||
| 				'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; | ||||
| 			}); | ||||
| 	const updateRoleHandler = async (id, role) => { | ||||
| 		const res = await updateUserRole(localStorage.token, id, role).catch((error) => { | ||||
| 			toast.error(error); | ||||
| 			return null; | ||||
| 		}); | ||||
| 
 | ||||
| 		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 () => { | ||||
| 		if ($user?.role !== 'admin') { | ||||
| 			await goto('/'); | ||||
| 		} else { | ||||
| 			await getUsers(); | ||||
| 			users = await getUsers(localStorage.token); | ||||
| 		} | ||||
| 		loaded = true; | ||||
| 	}); | ||||
|  | @ -115,11 +80,11 @@ | |||
| 												class="  dark:text-white underline" | ||||
| 												on:click={() => { | ||||
| 													if (user.role === 'user') { | ||||
| 														updateUserRole(user.id, 'admin'); | ||||
| 														updateRoleHandler(user.id, 'admin'); | ||||
| 													} else if (user.role === 'pending') { | ||||
| 														updateUserRole(user.id, 'user'); | ||||
| 														updateRoleHandler(user.id, 'user'); | ||||
| 													} else { | ||||
| 														updateUserRole(user.id, 'pending'); | ||||
| 														updateRoleHandler(user.id, 'pending'); | ||||
| 													} | ||||
| 												}}>{user.role}</button | ||||
| 											> | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy J. Baek
						Timothy J. Baek