新增自动刷新
This commit is contained in:
@@ -17,10 +17,11 @@ import { toast } from "sonner";
|
|||||||
import { ApiError } from "next/dist/server/api-utils";
|
import { ApiError } from "next/dist/server/api-utils";
|
||||||
|
|
||||||
interface CreateUserEditorProps {
|
interface CreateUserEditorProps {
|
||||||
children: React.ReactNode
|
children: React.ReactNode;
|
||||||
|
onRefresh: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CreateUserEditor({ children }: CreateUserEditorProps) {
|
export function CreateUserEditor({ children, onRefresh }: CreateUserEditorProps) {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -35,6 +36,7 @@ export function CreateUserEditor({ children }: CreateUserEditorProps) {
|
|||||||
});
|
});
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
toast.success('创建成功')
|
toast.success('创建成功')
|
||||||
|
onRefresh();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error((error as ApiError).message || '创建失败')
|
toast.error((error as ApiError).message || '创建失败')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { CreateUserEditor } from "./components/create-user-editor";
|
|||||||
|
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const { users, isLoading, error, total, page, pageSize, mutate } = useUserList();
|
const { users, isLoading, error, total, page, pageSize, mutate, refresh } = useUserList();
|
||||||
const [editorUserId, setEditorUserId] = useState("");
|
const [editorUserId, setEditorUserId] = useState("");
|
||||||
|
|
||||||
const handleUserUpdate = async (newUser: User) => {
|
const handleUserUpdate = async (newUser: User) => {
|
||||||
@@ -49,7 +49,7 @@ export default function Page() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div>
|
<div>
|
||||||
<CreateUserEditor >
|
<CreateUserEditor onRefresh={() => refresh()}>
|
||||||
<Button >新增用户</Button>
|
<Button >新增用户</Button>
|
||||||
</CreateUserEditor>
|
</CreateUserEditor>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { list, UserListParams, UserListResponse } from '@/lib/api/admin/user'
|
import { list, UserListParams, UserListResponse } from '@/lib/api/admin/user'
|
||||||
import { ApiError } from '@/lib/api/fetcher'
|
import { ApiError } from '@/lib/api/fetcher'
|
||||||
|
import { useCallback } from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
|
|
||||||
@@ -11,6 +12,10 @@ export function useUserList(params?: UserListParams) {
|
|||||||
() => list(params),
|
() => list(params),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const refresh = useCallback(() => {
|
||||||
|
return mutate()
|
||||||
|
}, [mutate])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
users: data?.items ?? [],
|
users: data?.items ?? [],
|
||||||
total: data?.total ?? 0,
|
total: data?.total ?? 0,
|
||||||
@@ -19,5 +24,6 @@ export function useUserList(params?: UserListParams) {
|
|||||||
isLoading,
|
isLoading,
|
||||||
error,
|
error,
|
||||||
mutate,
|
mutate,
|
||||||
|
refresh,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user