Skip to content

Commit 952fcdb

Browse files
committed
Merge branch 'fix-refresh-on-delete-question' into post-ms6
2 parents 921673d + dada4cf commit 952fcdb

File tree

7 files changed

+52
-49
lines changed

7 files changed

+52
-49
lines changed

peerprep/app/api/internal/questions/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { generateAuthHeaders, generateJSONHeaders } from "@/api/gateway";
22
import { NextRequest, NextResponse } from "next/server";
3+
import { revalidatePath } from "next/cache";
34

45
export async function GET() {
56
try {
@@ -8,22 +9,22 @@ export async function GET() {
89
{
910
method: "GET",
1011
headers: generateAuthHeaders(),
11-
}
12+
},
1213
);
1314
if (!response.ok) {
1415
return NextResponse.json(
1516
{
1617
error: await response.text(),
1718
status: response.status,
1819
},
19-
{ status: response.status }
20+
{ status: response.status },
2021
);
2122
}
2223
return response;
2324
} catch (err: any) {
2425
return NextResponse.json(
2526
{ error: err.message, status: 400 },
26-
{ status: 400 }
27+
{ status: 400 ,
2728
);
2829
}
2930
}
@@ -37,7 +38,7 @@ export async function POST(request: NextRequest) {
3738
method: "POST",
3839
body: JSON.stringify(body),
3940
headers: generateJSONHeaders(),
40-
}
41+
},
4142
);
4243
if (response.ok) {
4344
return NextResponse.json(
@@ -74,10 +75,11 @@ export async function DELETE(request: NextRequest) {
7475
{
7576
method: "DELETE",
7677
headers: generateAuthHeaders(),
77-
}
78+
},
7879
);
7980
if (response.ok) {
8081
// NextResponse doesn't support 204.
82+
revalidatePath("/questions");
8183
return new Response(null, { status: response.status });
8284
}
8385
return NextResponse.json(

peerprep/app/questions/[question]/question.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function QuestionBlock({ question }: Props) {
4545
}
4646
console.log(`Successfully deleted the question.`);
4747
router.push("/questions");
48+
router.refresh();
4849
} else {
4950
console.log("Deletion cancelled.");
5051
}

peerprep/app/questions/loading.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const LoadingPage = () => {
2+
return (
3+
<div>
4+
<h1>Loading...</h1>
5+
</div>
6+
);
7+
};
8+
9+
export default LoadingPage;

peerprep/app/questions/new/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function NewQuestion({}: Props) {
8989
...formData,
9090
};
9191
const status = await addQuestion(question);
92+
router.refresh();
9293
if (status.error) {
9394
console.log("Failed to add question.");
9495
console.log(`Code ${status.status}: ${status.error}`);

peerprep/app/questions/page.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ import Matchmaking from "@/components/questionpage/Matchmaking";
44
import { QuestionFilterProvider } from "@/contexts/QuestionFilterContext";
55
import { UserInfoProvider } from "@/contexts/UserInfoContext";
66
import { hydrateUid } from "../actions/server_actions";
7+
import { Question } from "@/api/structs";
8+
import { generateAuthHeaders } from "@/api/gateway";
79

810
async function QuestionsPage() {
911
const userId = await hydrateUid();
12+
13+
const questions: Question[] = await fetch(
14+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
15+
{
16+
method: "GET",
17+
headers: generateAuthHeaders(),
18+
},
19+
).then((res) => res.json());
20+
1021
return (
1122
<UserInfoProvider userid={userId}>
1223
<QuestionFilterProvider>
1324
<Matchmaking></Matchmaking>
14-
<QuestionList></QuestionList>
25+
<QuestionList questions={questions}></QuestionList>
1526
</QuestionFilterProvider>
1627
</UserInfoProvider>
1728
);

peerprep/components/questionpage/QuestionCard.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PeerprepButton from "../shared/PeerprepButton";
55
import { useRouter } from "next/navigation";
66
import styles from "@/style/questionCard.module.css";
77
import { deleteQuestion } from "@/app/api/internal/questions/helper";
8-
import DOMPurify from "dompurify";
8+
import DOMPurify from "isomorphic-dompurify";
99

1010
type QuestionCardProps = {
1111
question: Question;
@@ -16,10 +16,11 @@ const QuestionCard: React.FC<QuestionCardProps> = ({ question }) => {
1616
const handleDelete = async () => {
1717
if (
1818
confirm(
19-
`Are you sure you want to delete ${question.title}? (ID: ${question.id}) `
19+
`Are you sure you want to delete ${question.title}? (ID: ${question.id}) `,
2020
)
2121
) {
2222
const status = await deleteQuestion(question.id);
23+
router.refresh();
2324
if (status.error) {
2425
console.log("Failed to delete question.");
2526
console.log(`Code ${status.status}: ${status.error}`);
@@ -58,7 +59,7 @@ const QuestionCard: React.FC<QuestionCardProps> = ({ question }) => {
5859
Difficulty:{" "}
5960
<span
6061
className={`capitalize font-bold ${getDifficultyColor(
61-
question.difficulty
62+
question.difficulty,
6263
)}`}
6364
>
6465
{Difficulty[question.difficulty]}

peerprep/components/questionpage/QuestionList.tsx

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
11
"use client";
2-
import React, { useEffect, useState } from "react";
2+
import React, { useState } from "react";
33
import QuestionCard from "./QuestionCard";
4-
import { Question, Difficulty, isError } from "@/api/structs";
4+
import { Difficulty, Question } from "@/api/structs";
55
import PeerprepDropdown from "../shared/PeerprepDropdown";
66
import PeerprepSearchBar from "../shared/PeerprepSearchBar";
77
import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
8+
9+
type Props = {
10+
questions: Question[];
11+
};
12+
813
// TODO make multiple select for topics at least
9-
const QuestionList: React.FC = () => {
10-
const [questions, setQuestions] = useState<Question[]>([]);
11-
const [loading, setLoading] = useState(true);
14+
const QuestionList = ({ questions }: Props) => {
1215
const [searchFilter, setSearchFilter] = useState<string>("");
1316
const [difficultyFilter, setDifficultyFilter] = useState<string>(
14-
Difficulty.All
17+
Difficulty.All,
1518
);
1619
const [topicFilter, setTopicFilter] = useState<string>("all");
17-
1820
const { topicList, setTopicList } = useQuestionFilter();
1921

20-
useEffect(() => {
21-
const fetchQuestions = async () => {
22-
const payload = await fetch(`/api/internal/questions`).then((res) =>
23-
res.json()
24-
);
25-
// uh
26-
if (isError(payload)) {
27-
// should also reflect the error
28-
return;
29-
}
30-
const data: Question[] = payload;
31-
32-
setLoading(false);
33-
setQuestions(data);
34-
35-
// get all present topics in all qns
36-
const uniqueTopics = Array.from(
37-
new Set(data.flatMap((question) => question.topicTags))
38-
).sort();
39-
setTopicList(["all", ...uniqueTopics]);
40-
};
41-
42-
fetchQuestions();
43-
}, []);
22+
const uniqueTopics = Array.from(
23+
new Set(questions.flatMap((question) => question.topicTags) ?? []),
24+
);
25+
setTopicList(["all", ...uniqueTopics]);
4426

4527
const filteredQuestions = questions.filter((question) => {
4628
const matchesDifficulty =
@@ -89,15 +71,11 @@ const QuestionList: React.FC = () => {
8971
/>
9072
</div>
9173

92-
{loading ? (
93-
<p>Loading questions...</p>
94-
) : (
95-
<div>
96-
{sortedQuestions.map((question) => (
97-
<QuestionCard key={question.id} question={question} />
98-
))}
99-
</div>
100-
)}
74+
<div>
75+
{sortedQuestions.map((question) => (
76+
<QuestionCard key={question.id} question={question} />
77+
))}
78+
</div>
10179
</div>
10280
);
10381
};

0 commit comments

Comments
 (0)