import { type NextPage } from "next";
import Head from "next/head";
import { type GetServerSideProps } from "next";
import { getServerAuthSession } from "../../server/auth";
import { api } from "~/utils/api";
import Loading from "~/components/Loading";
import { IoTrashBinOutline } from "react-icons/io5";
import { AiOutlineClear } from "react-icons/ai";
import { FaShieldAlt } from "react-icons/fa";
import type { Role } from "~/utils/types";
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);
// Redirect to login if not signed in
if (!session) {
return {
redirect: {
destination: `/api/auth/signin?callbackUrl=${ctx.resolvedUrl}`,
permanent: false,
},
};
}
if (session.user.role !== "ADMIN") {
ctx.res.statusCode = 403;
return {
redirect: {
destination: "/",
permanent: false,
},
};
}
// Return session if logged in
return {
props: { session },
};
};
const Admin: NextPage = () => {
return (
<>
Sprint Padawan - Admin
>
);
};
export default Admin;
const AdminBody: React.FC = () => {
const {
data: usersCount,
isLoading: usersCountLoading,
isFetching: usersCountFetching,
refetch: refetchUsersCount,
} = api.user.countAll.useQuery();
const {
data: users,
isLoading: usersLoading,
isFetching: usersFetching,
refetch: refetchUsers,
} = api.user.getAll.useQuery();
const {
data: roomsCount,
isLoading: roomsCountLoading,
isFetching: roomsCountFetching,
refetch: refetchRoomsCount,
} = api.room.countAll.useQuery();
const {
data: votesCount,
isLoading: votesCountLoading,
isFetching: votesCountFetching,
refetch: refetchVotesCount,
} = api.vote.countAll.useQuery();
const deleteUserMutation = api.user.delete.useMutation({
onSuccess: async () => {
await refetchData();
},
});
const clearSessionsMutation = api.session.deleteAll.useMutation({
onSuccess: async () => {
await refetchData();
},
});
const setRoleMutation = api.user.setRole.useMutation({
onSuccess: async () => {
await refetchData();
},
});
const deleteUserHandler = async (userId: string) => {
await deleteUserMutation.mutateAsync({ userId });
};
const clearSessionsHandler = async (userId: string) => {
await clearSessionsMutation.mutateAsync({ userId });
};
const setUserRoleHandler = async (userId: string, role: Role) => {
await setRoleMutation.mutateAsync({ userId, role });
};
const refetchData = async () => {
await Promise.all([
refetchUsers(),
refetchUsersCount(),
refetchRoomsCount(),
refetchVotesCount(),
]);
};
return (
<>
Admin Panel
Users
{usersCountLoading || usersCountFetching ? (
) : (
<>{usersCount ? usersCount : "0"}>
)}
Rooms
{roomsCountLoading || roomsCountFetching ? (
) : (
<>{roomsCount ? roomsCount : "0"}>
)}
Votes
{votesCountLoading || votesCountFetching ? (
) : (
<>{votesCount ? votesCount : "0"}>
)}
{usersCountFetching ||
usersFetching ||
roomsCountFetching ||
votesCountFetching ? (
) : (
)}
Users:
{usersLoading || usersFetching ? (
) : (
{/* head */}
ID |
Name |
Created At |
# Sessions |
Actions |
{users
?.sort((user1, user2) =>
user2.createdAt > user1.createdAt ? 1 : -1
)
.map((user) => {
return (
{user.id}
|
{user.name}
|
{user.createdAt.toLocaleDateString()}
|
{user.sessions.length}
|
|
);
})}
)}
>
);
};