mirror of
https://github.com/iflytek/skillhub.git
synced 2026-09-07 08:26:00 +00:00
fix(frontend): use fetchJson and getCsrfHeaders in admin user mutations
Replace manual fetch + CSRF handling with shared fetchJson utility for consistency with other hooks and proper ApiResponse unwrapping.
This commit is contained in:
parent
559a9133ab
commit
af78262c81
1 changed files with 5 additions and 17 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { fetchJson } from '@/api/client'
|
||||
import { fetchJson, getCsrfHeaders } from '@/api/client'
|
||||
|
||||
export interface AdminUser {
|
||||
id: string
|
||||
|
|
@ -36,31 +36,19 @@ async function getAdminUsers(params: AdminUsersParams): Promise<PagedAdminUsers>
|
|||
}
|
||||
|
||||
async function updateUserRole(userId: string, role: string): Promise<void> {
|
||||
const csrfToken = document.cookie.match(/(?:^|; )XSRF-TOKEN=([^;]+)/)?.[1]
|
||||
const res = await fetch(`/api/v1/admin/users/${userId}/role`, {
|
||||
await fetchJson<void>(`/api/v1/admin/users/${userId}/role`, {
|
||||
method: 'PUT',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(csrfToken ? { 'X-XSRF-TOKEN': decodeURIComponent(csrfToken) } : {}),
|
||||
},
|
||||
headers: getCsrfHeaders({ 'Content-Type': 'application/json' }),
|
||||
body: JSON.stringify({ role }),
|
||||
})
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
|
||||
async function updateUserStatus(userId: string, status: 'ACTIVE' | 'DISABLED'): Promise<void> {
|
||||
const csrfToken = document.cookie.match(/(?:^|; )XSRF-TOKEN=([^;]+)/)?.[1]
|
||||
const res = await fetch(`/api/v1/admin/users/${userId}/status`, {
|
||||
await fetchJson<void>(`/api/v1/admin/users/${userId}/status`, {
|
||||
method: 'PUT',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(csrfToken ? { 'X-XSRF-TOKEN': decodeURIComponent(csrfToken) } : {}),
|
||||
},
|
||||
headers: getCsrfHeaders({ 'Content-Type': 'application/json' }),
|
||||
body: JSON.stringify({ status }),
|
||||
})
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
|
||||
export function useAdminUsers(params: AdminUsersParams) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue