edit users
This commit is contained in:
46
frontend/lib/api/admin/editUser.ts
Normal file
46
frontend/lib/api/admin/editUser.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { toast } from 'vue-sonner';
|
||||
import type { ApiResponse } from '#shared/types/api';
|
||||
import type { AuthUser } from '#shared/types/auth';
|
||||
import { getApiHeaders } from '..';
|
||||
|
||||
/** Admin function to edit an existing user */
|
||||
export async function editUser(
|
||||
user: AuthUser & { password: string | null }
|
||||
): Promise<{ success: true; user: AuthUser } | { success: false }> {
|
||||
const { data, error } = await useFetch<ApiResponse<AuthUser>>(
|
||||
getApiUrl('admin/users'),
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: getApiHeaders(),
|
||||
body: JSON.stringify({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
password: user.password,
|
||||
admin: user.admin,
|
||||
}),
|
||||
responseType: 'json',
|
||||
}
|
||||
);
|
||||
|
||||
if (data.value == null) {
|
||||
toast.error('Edit user', {
|
||||
description: error.value?.data ?? 'Failed to edit user',
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
await refreshNuxtData('admin-resources');
|
||||
|
||||
toast.success('Edit user', {
|
||||
description: 'Successfully edited user',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: data.value.data,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user