Skip to content

Commit

Permalink
Changes for a3 recording
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabau committed Nov 3, 2023
1 parent 570eeff commit 0544ed1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
29 changes: 24 additions & 5 deletions src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { signOut, useSession } from "next-auth/react";
import Link from "next/link";
import { useRouter } from "next/router";
import { useState } from "react";
import toast from "react-hot-toast";

const NavBar = () => {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);

const router = useRouter();
const toggleDropdown = () => {
setIsDropdownOpen(!isDropdownOpen);
};
const { data: session } = useSession();

const closeDropdown = () => setIsDropdownOpen(false);

Expand All @@ -22,6 +26,13 @@ const NavBar = () => {
</div>
{isDropdownOpen && (
<div className="absolute z-50 bg-slate-800 flex flex-col space-y-1 p-2">
<Link
href="/"
onClick={closeDropdown}
className="flex items-center justify-center font-bold text-white no-underline transition hover:bg-white/20 rounded-md whitespace-nowrap bg-white/10 flex-[1_0_0%] px-4"
>
PeerPrep
</Link>
<Link
href="/questions"
onClick={closeDropdown}
Expand Down Expand Up @@ -57,20 +68,28 @@ const NavBar = () => {
>
CodeExecution - Judge0
</Link>
<Link
{!session && <Link
href="/sign-up"
onClick={closeDropdown}
className="flex items-center justify-center font-bold text-white no-underline transition hover:bg-white/20 rounded-md whitespace-nowrap bg-white/10 flex-[1_0_0%] px-4"
>
SignUp
</Link>
<Link
</Link>}
{!session && <Link
href="/sign-in"
onClick={closeDropdown}
className="flex items-center justify-center font-bold text-white no-underline transition hover:bg-white/20 rounded-md whitespace-nowrap bg-white/10 flex-[1_0_0%] px-4"
>
SignIn
</Link>
</Link>}
{session && <div
onClick={() => {
void signOut();
}}
className="cursor-pointer flex items-center justify-center font-bold text-white no-underline transition hover:bg-white/20 rounded-md whitespace-nowrap bg-white/10 flex-[1_0_0%] px-4"
>
Log out
</div>}
</div>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/UserDenied.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LoadingPage } from "./Loading";
export default function UserDenied() {
return (
<PageLayout>
<LoadingPage />
User Denied
</PageLayout>
)
}
Expand Down
21 changes: 17 additions & 4 deletions src/components/wrapper/AuthWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { useRouter } from "next/router";
import UserDenied from "~/components/UserDenied";
import { LoadingPage } from "../Loading";
import { PageLayout } from "../Layout";
import { useEffect } from "react";
import toast from "react-hot-toast";

type Props = {
children: React.ReactElement;
maintainerOnly?: boolean;
};

/*
Expand All @@ -19,13 +22,22 @@ type Props = {
export default OrderDetail;
*/

export const AuthWrapper = ({ children }: Props): JSX.Element => {
const { status: sessionStatus } = useSession({ required: true });
export const AuthWrapper = ({ children, maintainerOnly }: Props): JSX.Element => {
const { status: sessionStatus, data: session } = useSession({ required: true });
const authorized = sessionStatus === "authenticated";
const isMaintainer = session?.user.role === "MAINTAINER";
const loading = sessionStatus === "loading";
const router = useRouter();

useEffect(() => {
if (session && maintainerOnly && !isMaintainer) {
toast.error("Maintainer only route");
void router.back();
}
}, [session]);

// if the user refreshed the page or somehow navigated to the protected page
if (loading) {
if (loading || maintainerOnly && !isMaintainer) {
return (
<>
<PageLayout>
Expand All @@ -42,11 +54,12 @@ export const AuthWrapper = ({ children }: Props): JSX.Element => {

export function WithAuthWrapper<Props extends JSX.IntrinsicAttributes>(
Component: (props: Props) => JSX.Element,
maintainerOnly = false
) {
// eslint-disable-next-line react/display-name
return (pageProps: Props) => {
return (
<AuthWrapper>
<AuthWrapper maintainerOnly={maintainerOnly}>
<Component {...pageProps} />
</AuthWrapper>
);
Expand Down
5 changes: 4 additions & 1 deletion src/pages/judge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { PeerPrepRectLogo } from "~/assets/logo";
import { api } from "~/utils/api";
import { PageLayout } from "~/components/Layout";
import Head from "next/head";
import { WithAuthWrapper } from "~/components/wrapper/AuthWrapper";
// import { useSession } from "next-auth/react";

export default function Questions() {
function Questions() {
const [output, setOutput] = useState<string | null>(null);
const [questionId, setQuestionId] = useState<string | null>(null);
const [environmentId, setEnvironmentId] = useState<string | null>(null);
Expand Down Expand Up @@ -269,3 +270,5 @@ export default function Questions() {
</>
);
}

export default WithAuthWrapper(Questions, true);
28 changes: 16 additions & 12 deletions src/pages/questions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { makeMap } from "../../utils/utils";
import { toast } from "react-hot-toast";
import { useSession } from "next-auth/react";
import WarningModal from "~/components/WarningModal";
import { WithAuthWrapper } from "~/components/wrapper/AuthWrapper";

export default function Questions() {
function Questions() {
const { data: sessionData } = useSession();
const allowedToModify = sessionData?.user.role === "MAINTAINER";
const getAllQuery = api.useContext().question.getAll;
Expand All @@ -21,18 +22,19 @@ export default function Questions() {
const [changedQns, setChangedQns] = useState(new Set<string>());
const [deletedQns, setDeletedQns] = useState(new Set<string>());

const questionsQuery = api.question.getAll.useQuery(undefined, {
onSuccess: (data) => {
const mappedData = data.map((q) => ({
...(changedQns.has(q.id) ? viewQns.get(q.id) ?? q : q),
}));
setViewQns(makeMap(mappedData, "id"));
},
onError: (e) => {
toast.error("Failed to fetch questions");
},
});
const questions = makeMap(
api.question.getAll.useQuery(undefined, {
onSuccess: (data) => {
const mappedData = data.map((q) => ({
...(changedQns.has(q.id) ? viewQns.get(q.id) ?? q : q),
}));
setViewQns(makeMap(mappedData, "id"));
},
onError: (e) => {
toast.error("Failed to fetch questions");
},
}).data ?? [],
questionsQuery.data ?? [],
"id",
);

Expand Down Expand Up @@ -225,3 +227,5 @@ export default function Questions() {
</>
);
}

export default WithAuthWrapper(Questions);

0 comments on commit 0544ed1

Please sign in to comment.