diff --git a/app/admin/layout.tsx b/app/admin/layout.tsx index bb2c9f7..2b868c3 100644 --- a/app/admin/layout.tsx +++ b/app/admin/layout.tsx @@ -1,12 +1,20 @@ -"use client" +import { cookies } from "next/headers" import { AppSidebar } from "./sidebar" import { SiteHeader } from "./site-header" import { SidebarInset, SidebarProvider, } from "@/components/ui/sidebar" +import { redirect } from "next/navigation" + +export default async function Layout({ children }: { children: React.ReactNode }) { + + const isLoggedIn = (await cookies()).get('is_logged_in')?.value === 'true' + + if (!isLoggedIn) { + redirect('/login') + } -export default function Layout({ children }: { children: React.ReactNode }) { return ( { + if (!isLoading) { + if (!isAuthenticated) { + router.push('/login'); + console.log(user?.role) + return; + } + } + }, [isAuthenticated, isLoading, router, user]); + + return (<>) + } diff --git a/app/admin/users/user-table.tsx b/app/admin/users/user-table.tsx index 310a311..f19677d 100644 --- a/app/admin/users/user-table.tsx +++ b/app/admin/users/user-table.tsx @@ -307,10 +307,10 @@ const columns: ColumnDef>[] = [ Open menu - + Edit - Make a copy - Favorite + Reset Password + Ban User Delete @@ -358,39 +358,62 @@ const GET_USERS = gql` ` const USERS_INFO = gql` - query UsersInfo { - usersInfo { + query UsersInfo($offset: Int, $limit: Int) { + usersInfo(offset: $offset, limit: $limit) { totalUsers totalActiveUsers totalAdminUsers totalUserUsers + users { + id + username + email + role + createdAt + updatedAt + } } } ` export function UserTable() { - const { data, loading, error, refetch } = useQuery(GET_USERS, { - variables: { - offset: 0, - limit: 10, - }, - }) + const { data, loading, error: usersInfoError, refetch } = useQuery(USERS_INFO) + + const [localData, setLocalData] = React.useState([]) + + // 同步外部数据到本地状态 + React.useEffect(() => { + if (data && Array.isArray(data.usersInfo.users)) { + setLocalData(data.usersInfo.users) + } + }, [data]) + + function handleDragEnd(event: DragEndEvent) { + const { active, over } = event + if (active && over && active.id !== over.id) { + setLocalData((currentData) => { + const oldIndex = currentData.findIndex((item) => item.id === active.id) + const newIndex = currentData.findIndex((item) => item.id === over.id) + + if (oldIndex === -1 || newIndex === -1) return currentData + + // 只做本地排序,不保存到数据库 + return arrayMove(currentData, oldIndex, newIndex) + }) + } + } - const { data: usersInfo, loading: usersInfoLoading, error: usersInfoError } = useQuery(USERS_INFO) return ( <> - {loading &&
Loading...
} - {error &&
Error: {error.message}
} - {data && } + ) - } -function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any }) { +function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, refetch: any, info: any, isLoading: boolean, handleDragEnd: (event: DragEndEvent) => void }) { const [rowSelection, setRowSelection] = React.useState({}) const [columnVisibility, setColumnVisibility] = @@ -410,15 +433,9 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any }) useSensor(KeyboardSensor, {}) ) - React.useEffect(() => { - refetch({ - offset: pagination.pageIndex * pagination.pageSize, - limit: pagination.pageSize, - }) - }, [pagination, refetch]) const table = useReactTable({ - data: data?.users || [], + data: data || [], columns, state: { sorting, @@ -443,16 +460,7 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any }) manualPagination: true, }) - // function handleDragEnd(event: DragEndEvent) { - // const { active, over } = event - // if (active && over && active.id !== over.id) { - // setData((data) => { - // const oldIndex = dataIds.indexOf(active.id) - // const newIndex = dataIds.indexOf(over.id) - // return arrayMove(data, oldIndex, newIndex) - // }) - // } - // } + return ( @@ -467,7 +475,7 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any })
@@ -483,21 +491,21 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any }) - All Users - Active Users - Administrators - Inactive Users + All Users + Active Users + Administrators + Inactive Users - All Users - - Active Users {info?.usersInfo?.totalActiveUsers} + All Users + + Active Users {info?.totalActiveUsers} - - Administrators {info?.usersInfo?.totalAdminUsers} + + Administrators {info?.totalAdminUsers} - Inactive Users + Inactive Users
@@ -543,14 +551,14 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any })
@@ -574,9 +582,15 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any }) ))} - {table.getRowModel().rows?.length ? ( + {isLoading ? (<> + + + + + + ) : table.getRowModel().rows?.length ? ( user.id) || []} + items={data?.map((user: any) => user.id) || []} strategy={verticalListSortingStrategy} > {table.getRowModel().rows.map((row) => ( @@ -676,16 +690,16 @@ function Tabel({ data, refetch, info }: { data: any, refetch: any, info: any })
- +
diff --git a/app/login/layout.tsx b/app/login/layout.tsx new file mode 100644 index 0000000..405b3b5 --- /dev/null +++ b/app/login/layout.tsx @@ -0,0 +1,19 @@ +import { cookies } from "next/headers" +import { + SidebarInset, + SidebarProvider, +} from "@/components/ui/sidebar" +import { redirect } from "next/navigation" + +export default async function Layout({ children }: { children: React.ReactNode }) { + + const isLoggedIn = (await cookies()).get('is_logged_in')?.value === 'true' + + if (isLoggedIn) { + redirect('/') + } + + return ( + <>{children} + ) +} \ No newline at end of file diff --git a/app/login/page.tsx b/app/login/page.tsx index 10cab8d..0a66ab9 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -17,35 +17,35 @@ export default function LoginPage() { } }, [isAuthenticated, isLoading, router]); - // 如果正在加载或已认证,显示加载状态 - if (isLoading || isAuthenticated) { - return ( -
-
- -
-
-

正在跳转...

-
-
-
-
- Image -
-
- ); - } + // // 如果正在加载或已认证,显示加载状态 + // if (isLoading || isAuthenticated) { + // return ( + //
+ //
+ // + //
+ //
+ //

正在跳转...

+ //
+ //
+ //
+ //
+ // Image + //
+ //
+ // ); + // } return (
diff --git a/app/me/layout.tsx b/app/me/layout.tsx index c1f589d..2b6f6c0 100644 --- a/app/me/layout.tsx +++ b/app/me/layout.tsx @@ -1,11 +1,21 @@ -"use client" +import { cookies } from "next/headers" import { AppSidebar } from "./sidebar" import { SidebarInset, SidebarProvider, } from "@/components/ui/sidebar" +import { redirect } from "next/navigation" + +export default async function Layout({ children }: { children: React.ReactNode }) { + + const isLoggedIn = (await cookies()).get('is_logged_in')?.value === 'true' + + console.log(isLoggedIn) + + if (!isLoggedIn) { + redirect('/login') + } -export default function Layout({ children }: { children: React.ReactNode }) { return ( { - if (!isAuthenticated) { - redirect("/login") + if (!isLoading && !isAuthenticated) { + router.push('/login'); } - }, [isAuthenticated]) + }, [isAuthenticated, isLoading, router, user]); return (
diff --git a/app/user-context.tsx b/app/user-context.tsx index 24e7258..861a2f1 100644 --- a/app/user-context.tsx +++ b/app/user-context.tsx @@ -188,6 +188,8 @@ export function UserProvider({ children }: UserProviderProps) { isLoading: false }) + document.cookie = 'is_logged_in=true; path=/; max-age=3600' + // 登录成功后重置 Apollo 缓存 resetApolloCache() } catch (error) { @@ -261,6 +263,8 @@ export function UserProvider({ children }: UserProviderProps) { isLoading: false }) + document.cookie = 'is_logged_in=false; path=/; max-age=3600' + // 登出后重置 Apollo 缓存 resetApolloCache() }