Skip to content

Commit 9191630

Browse files
authored
Merge pull request #167 from PeerPrep/elroy-bug-fixes
fix: fix bugs
2 parents dfe7977 + 93d0bbb commit 9191630

File tree

5 files changed

+70
-38
lines changed

5 files changed

+70
-38
lines changed

frontend/src/app/admin/portal/page.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"use client";
22

3-
import { fetchAllUsers, fetchIsAdmin, promoteToAdmin } from "@/app/api";
3+
import { fetchAllUsers, promoteToAdmin } from "@/app/api";
44
import Button from "@/app/components/button/Button";
55
import useAdmin from "@/app/hooks/useAdmin";
6+
import LoadingPage from "@/app/loading";
67
import { message } from "antd";
7-
import { useRouter } from "next/navigation";
8-
import { use, useEffect, useState } from "react";
8+
import { useEffect, useState } from "react";
99
import Select, { MultiValue } from "react-select";
1010

1111
const AdminPortalPage = () => {
12-
const isAdmin = useAdmin();
13-
const router = useRouter();
12+
const { isAdmin, isLoading } = useAdmin();
1413
const [api, contextHolder] = message.useMessage();
1514

1615
type User = {
@@ -30,13 +29,15 @@ const AdminPortalPage = () => {
3029
>([]);
3130

3231
useEffect(() => {
33-
fetchAllUsers().then((allUsers) => {
34-
setAdminOptions(
35-
allUsers.payload
36-
.filter((user: User) => user.role === "user")
37-
.map((user: User) => ({ label: user.name, value: user.uid })),
38-
);
39-
});
32+
if (isAdmin) {
33+
fetchAllUsers().then((allUsers) => {
34+
setAdminOptions(
35+
allUsers.payload
36+
.filter((user: User) => user.role === "user")
37+
.map((user: User) => ({ label: user.name, value: user.uid })),
38+
);
39+
});
40+
}
4041
}, [api, contextHolder]);
4142

4243
const handleSelectChange = (
@@ -49,8 +50,20 @@ const AdminPortalPage = () => {
4950
MultiValue<SelectOptionType>
5051
>([]);
5152

53+
if (isLoading) {
54+
return <LoadingPage />;
55+
}
56+
5257
if (!isAdmin) {
53-
router.push("/");
58+
return (
59+
<div className="flex h-full items-center justify-center">
60+
<div className="flex items-center">
61+
<h1 className="text-6xl text-white">401</h1>
62+
<div className="divider divider-horizontal h-24"></div>
63+
<h2 className="text-xl text-white">Unauthorized</h2>
64+
</div>
65+
</div>
66+
);
5467
}
5568

5669
return (

frontend/src/app/admin/question/page.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { onAuthStateChanged } from "firebase/auth";
3030
import useLogin from "@/app/hooks/useLogin";
3131
import { useRouter } from "next/navigation";
3232
import useAdmin from "@/app/hooks/useAdmin";
33+
import LoadingPage from "@/app/loading";
3334

3435
export interface QuestionType {
3536
_id?: string;
@@ -58,12 +59,7 @@ const Table = dynamic(() => import("antd/lib").then((m) => m.Table), {
5859
});
5960

6061
const QuestionPage = () => {
61-
const router = useRouter();
62-
const isAdmin = useAdmin();
63-
64-
if (!isAdmin) {
65-
router.push("/");
66-
}
62+
const { isAdmin, isLoading } = useAdmin();
6763

6864
const user = useLogin();
6965
const [api, contextHolder] = message.useMessage();
@@ -265,6 +261,22 @@ const QuestionPage = () => {
265261
);
266262
};
267263

264+
if (isLoading) {
265+
return <LoadingPage />;
266+
}
267+
268+
if (!isAdmin) {
269+
return (
270+
<div className="flex h-full items-center justify-center">
271+
<div className="flex items-center">
272+
<h1 className="text-6xl text-white">401</h1>
273+
<div className="divider divider-horizontal h-24"></div>
274+
<h2 className="text-xl text-white">Unauthorized</h2>
275+
</div>
276+
</div>
277+
);
278+
}
279+
268280
return (
269281
<>
270282
<EditQuestionModal

frontend/src/app/components/modal/QuestionModal.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,32 @@ const QuestionModal = () => {
3434

3535
const [questions, setQuestions] = useState<QuestionType[]>([]);
3636
const setCollabQuestion = useSetAtom(triggerQuestionIdUpdateRequestAtom);
37+
const questionDifficulty = useAtomValue(
38+
questionDifficultyAtom,
39+
)?.toLowerCase();
40+
3741
useEffect(() => {
3842
fetchAllQuestionsUrl().then((res) => {
39-
setQuestions(res.payload);
43+
setQuestions(
44+
res.payload.filter((question: any) => {
45+
const curQnDifficulty = question.difficulty.toLowerCase();
46+
return curQnDifficulty === questionDifficulty;
47+
}),
48+
);
4049
});
4150
}, []);
4251

43-
const questionDifficulty = useAtomValue(
44-
questionDifficultyAtom,
45-
)?.toLowerCase();
52+
// setQuestions(
53+
// questions.filter(
54+
// }),
55+
// );
4656

47-
const options: SelectedOptionType[] = questions
48-
.filter((question) => {
49-
const curQnDifficulty = question.difficulty.toLowerCase();
50-
return curQnDifficulty === questionDifficulty;
51-
})
52-
.map((question, i) => {
53-
return {
54-
label: question.title as string,
55-
value: i,
56-
};
57-
});
57+
const options: SelectedOptionType[] = questions.map((question, i) => {
58+
return {
59+
label: question.title as string,
60+
value: i,
61+
};
62+
});
5863

5964
let color;
6065

frontend/src/app/components/navbar/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Navbar = () => {
2424
const [user, setUser] = useState<User | null>(null);
2525
const [profileImageUrl, setProfileImageUrl] = useState<string | null>(null);
2626
const [name, setName] = useState<string>("");
27-
const isAdmin = useAdmin();
27+
const { isAdmin, isLoading } = useAdmin();
2828

2929
useEffect(() => {
3030
fetchProfileUrl().then((res) => {

frontend/src/app/hooks/useAdmin.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { useEffect, useState } from "react";
33
import { getAuth, onAuthStateChanged } from "firebase/auth";
44
import { auth } from "../../libs/firebase-config";
5-
import { useRouter } from "next/navigation";
65
import { fetchIsAdmin } from "../api";
76

87
export interface Profile {
@@ -14,21 +13,24 @@ export interface Profile {
1413
}
1514

1615
const useAdmin = () => {
17-
const [isAdmin, setIsAdmin] = useState<boolean>(true);
16+
const [isAdmin, setIsAdmin] = useState<boolean>(false);
17+
const [isLoading, setIsLoading] = useState<boolean>(true); // Add isLoading state
18+
1819
useEffect(() => {
1920
const unsubscribe = onAuthStateChanged(auth, async (authUser) => {
2021
if (authUser) {
2122
// If a user is authenticated, update the user state
2223
const isAdmin = await fetchIsAdmin();
2324
setIsAdmin(isAdmin);
2425
}
26+
setIsLoading(false); // Set isLoading to false
2527
});
2628

2729
// Clean up the listener when the component unmounts
2830
return () => unsubscribe();
2931
}, []);
3032

31-
return isAdmin;
33+
return { isAdmin, isLoading }; // Return both isAdmin and isLoading
3234
};
3335

3436
export default useAdmin;

0 commit comments

Comments
 (0)