修改客户管理认领和管理员字段展示逻辑
This commit is contained in:
@@ -10,7 +10,7 @@ import type { User } from "@/models/user"
|
||||
|
||||
export default function UserPage() {
|
||||
const table = useDataTable<User>((page, size) => getPageUsers({ page, size }))
|
||||
const bind = useFetch((id: number) => bindAdmin({ id }), {
|
||||
const bind = useFetch(table, (id: number) => bindAdmin({ id }), {
|
||||
done: "用户已认领",
|
||||
fail: "用户认领失败",
|
||||
})
|
||||
@@ -73,7 +73,10 @@ export default function UserPage() {
|
||||
},
|
||||
},
|
||||
{ header: "联系方式", accessorKey: "contact_wechat" },
|
||||
{ header: "管理员", accessorKey: "admin_id" },
|
||||
{
|
||||
header: "管理员",
|
||||
cell: ({ row }) => row.original.admin?.name,
|
||||
},
|
||||
{
|
||||
header: "最后登录时间",
|
||||
accessorKey: "last_login",
|
||||
@@ -92,9 +95,9 @@ export default function UserPage() {
|
||||
<Button
|
||||
size={"sm"}
|
||||
onClick={() => bind(ctx.row.original.id)}
|
||||
disabled={ctx.row.original.admin_id !== null}
|
||||
disabled={!!ctx.row.original.admin_id}
|
||||
>
|
||||
{ctx.row.original.admin_id !== null ? "已认领" : "认领"}
|
||||
{ctx.row.original.admin_id ? "已认领" : "认领"}
|
||||
</Button>
|
||||
),
|
||||
},
|
||||
|
||||
@@ -51,8 +51,9 @@ export function useDataTable<T>(
|
||||
}, [refresh, page, size])
|
||||
|
||||
return {
|
||||
status,
|
||||
data,
|
||||
status,
|
||||
setStatus,
|
||||
pagination: {
|
||||
page,
|
||||
size,
|
||||
@@ -60,5 +61,6 @@ export function useDataTable<T>(
|
||||
onPageChange,
|
||||
onSizeChange,
|
||||
},
|
||||
refresh,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
useState,
|
||||
} from "react"
|
||||
import { toast } from "sonner"
|
||||
import type { useDataTable } from "@/components/data-table"
|
||||
import type { ApiResponse } from "@/lib/api"
|
||||
|
||||
export function useStatus() {
|
||||
@@ -12,31 +13,39 @@ export function useStatus() {
|
||||
}
|
||||
|
||||
export function useFetch<TArgs extends unknown[], TResult>(
|
||||
table: ReturnType<typeof useDataTable>,
|
||||
fetchData: (...args: TArgs) => Promise<ApiResponse<TResult>>,
|
||||
messages: {
|
||||
done?: string
|
||||
fail?: string
|
||||
},
|
||||
setStatus?: Dispatch<SetStateAction<"load" | "fail" | "done">>,
|
||||
) {
|
||||
return useCallback(
|
||||
async (...args: TArgs) => {
|
||||
try {
|
||||
setStatus?.("load")
|
||||
table.setStatus?.("load")
|
||||
const resp = await fetchData(...args)
|
||||
if (!resp.success) {
|
||||
throw new Error(resp.message)
|
||||
}
|
||||
|
||||
setStatus?.("done")
|
||||
table.setStatus?.("done")
|
||||
table.refresh(table.pagination.page, table.pagination.size)
|
||||
toast.success(messages.done || "获取数据成功")
|
||||
} catch (e) {
|
||||
setStatus?.("fail")
|
||||
table.setStatus?.("fail")
|
||||
toast.error(messages.fail || "获取数据失败", {
|
||||
description: (e as Error).message || "未知错误",
|
||||
})
|
||||
}
|
||||
},
|
||||
[fetchData, setStatus, messages],
|
||||
[
|
||||
fetchData,
|
||||
table.setStatus,
|
||||
table.pagination.page,
|
||||
table.pagination.size,
|
||||
table.refresh,
|
||||
messages,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export type User = {
|
||||
id: number
|
||||
admin_id: number
|
||||
admin_id?: number
|
||||
admin?: Admin
|
||||
phone: string
|
||||
has_password: boolean
|
||||
username: string
|
||||
@@ -20,3 +21,7 @@ export type User = {
|
||||
created_at: Date
|
||||
updated_at: Date
|
||||
}
|
||||
|
||||
export type Admin = {
|
||||
name: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user