diff --git a/package.json b/package.json index cdbf4c1..50ccd47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lanhu-admin", - "version": "1.0.1", + "version": "1.0.2", "private": true, "scripts": { "dev": "next dev -H 0.0.0.0 --turbopack", diff --git a/src/app/(root)/user/page.tsx b/src/app/(root)/user/page.tsx index 7871717..bb39021 100644 --- a/src/app/(root)/user/page.tsx +++ b/src/app/(root)/user/page.tsx @@ -10,7 +10,7 @@ import type { User } from "@/models/user" export default function UserPage() { const table = useDataTable((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() { ), }, diff --git a/src/components/data-table/hooks.ts b/src/components/data-table/hooks.ts index 8d2ae05..f7ded99 100644 --- a/src/components/data-table/hooks.ts +++ b/src/components/data-table/hooks.ts @@ -51,8 +51,9 @@ export function useDataTable( }, [refresh, page, size]) return { - status, data, + status, + setStatus, pagination: { page, size, @@ -60,5 +61,6 @@ export function useDataTable( onPageChange, onSizeChange, }, + refresh, } } diff --git a/src/hooks/data.ts b/src/hooks/data.ts index eac5747..b0c8b32 100644 --- a/src/hooks/data.ts +++ b/src/hooks/data.ts @@ -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( + table: ReturnType, fetchData: (...args: TArgs) => Promise>, messages: { done?: string fail?: string }, - setStatus?: Dispatch>, ) { 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, + ], ) } diff --git a/src/models/user.ts b/src/models/user.ts index 11ce83d..05b2b06 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -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 +}