Skip to content

Commit bb0b52c

Browse files
committed
Fix /questions page refreshing on add/edit
1 parent 743a3c4 commit bb0b52c

File tree

8 files changed

+72
-191
lines changed

8 files changed

+72
-191
lines changed

peerprep/api/gateway.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { cookies } from "next/headers";
22
import { LoginResponse, Question, StatusBody, UserServiceResponse } from "./structs";
33
import DOMPurify from "isomorphic-dompurify";
44
import { CookieNames } from "@/app/actions/session";
5+
import { revalidatePath } from "next/cache";
56

67
export function generateAuthHeaders() {
78
return {
@@ -20,31 +21,32 @@ export function getUserData() {
2021
export function generateJSONHeaders() {
2122
return {
2223
...generateAuthHeaders(),
23-
"Content-type": "application/json; charset=UTF-8,
24+
"Content-type": "application/json; charset=UTF-8",
2425
};
2526
}
2627

2728
export async function fetchQuestion(
28-
questionId: number
29+
questionId: numbe,
2930
): Promise<Question | StatusBody> {
3031
try {
3132
const response = await fetch(
3233
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`,
3334
{
3435
method: "GET",
3536
headers: generateAuthHeaders(),
36-
cache: "no-store"
37+
cache: "no-store",
3738
},
3839
);
3940
if (!response.ok) {
4041
return {
4142
error: await response.text(),
42-
status: response.status
43+
status: response.statu,
4344
};
4445
}
4546

4647
const question = (await response.json()) as Question;
4748
question.content = DOMPurify.sanitize(question.content);
49+
revalidatePath(`/questions/edit/${questionId}`);
4850
return question;
4951
} catch (err: any) {
5052
return { error: err.message, status: 400 };
@@ -62,7 +64,7 @@ export async function getSessionLogin(validatedFields: {
6264
method: "POST",
6365
body: JSON.stringify(validatedFields),
6466
headers: {
65-
"Content-type": "application/json; charset=UTF-8"
67+
"Content-type": "application/json; charset=UTF-8",
6668
},
6769
},
6870
);
@@ -92,7 +94,7 @@ export async function postSignupUser(validatedFields: {
9294
method: "POST",
9395
body: JSON.stringify(validatedFields),
9496
headers: {
95-
"Content-type": "application/json; charset=UTF-8"
97+
"Content-type": "application/json; charset=UTF-8,
9698
},
9799
},
98100
);

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 0 additions & 135 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import React from "react";
44
import { Difficulty, Question } from "@/api/structs";
55
import Chip from "@/components/shared/Chip";
6-
import PeerprepButton from "@/components/shared/PeerprepButton";
76
import styles from "@/style/question.module.css";
87
import { useRouter } from "next/navigation";
9-
import { deleteQuestion } from "@/app/api/internal/questions/helper";
8+
import { deleteQuestion } from "@/app/questions/helper";
109
import CollabEditor from "@/components/questionpage/CollabEditor";
1110
import DOMPurify from "isomorphic-dompurify";
1211

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useForm } from "react-hook-form";
44
import { z } from "zod";
55
import { zodResolver } from "@hookform/resolvers/zod";
66
import { Question, QuestionSchema } from "@/api/structs";
7-
import { editQuestion } from "@/app/api/internal/questions/helper";
7+
import { editQuestion } from "@/app/questions/helper";
88
import QuestionForm from "@/app/questions/QuestionForm";
99
import { useRouter } from "next/navigation";
1010

@@ -23,7 +23,7 @@ const EditQuestion = ({ question }: { question: Question }) => {
2323

2424
const onSubmit = async (values: z.infer<typeof QuestionSchema>) => {
2525
console.log(values);
26-
const qn = {
26+
const qn: Question = {
2727
id: question.id,
2828
...values,
2929
};

peerprep/app/questions/helper.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use server";
2+
3+
import { Question, QuestionFullBody, StatusBody } from "@/api/structs";
4+
import { revalidatePath } from "next/cache";
5+
import { generateAuthHeaders, generateJSONHeaders } from "@/api/gateway";
6+
7+
export async function deleteQuestion(id: number): Promise<StatusBody> {
8+
const res = await fetch(
9+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/delete/${id}`,
10+
{
11+
method: "DELETE",
12+
headers: generateAuthHeaders(),
13+
},
14+
);
15+
if (res.ok) {
16+
return { status: res.status };
17+
}
18+
revalidatePath("/questions");
19+
const json = await res.json();
20+
return json as StatusBody;
21+
}
22+
23+
export async function editQuestion(question: Question): Promise<StatusBody> {
24+
console.log("editing question", question.id);
25+
const res = await fetch(
26+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/replace/${question.id}`,
27+
{
28+
method: "PUT",
29+
body: JSON.stringify(question),
30+
headers: generateJSONHeaders(),
31+
},
32+
);
33+
if (!res.ok) {
34+
return { status: res.status };
35+
}
36+
revalidatePath("/questions");
37+
revalidatePath("/questions/edit/" + question.id);
38+
const json = await res.json();
39+
return json as StatusBody;
40+
}
41+
42+
export async function addQuestion(
43+
question: QuestionFullBody,
44+
): Promise<StatusBody> {
45+
console.log("Adding question", question.title);
46+
const url = `${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`;
47+
console.log(url);
48+
const res = await fetch(url, {
49+
method: "POST",
50+
body: JSON.stringify(question),
51+
headers: generateJSONHeaders(),
52+
});
53+
if (!res.ok) {
54+
return { status: res.status };
55+
}
56+
revalidatePath("/questions");
57+
const json = await res.json();
58+
return json as StatusBody;
59+
}

peerprep/app/questions/new/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useRouter } from "next/navigation";
88
import { exampleQuestion } from "@/app/questions/new/ExampleQuestion";
99
import { zodResolver } from "@hookform/resolvers/zod";
1010
import QuestionForm from "@/app/questions/QuestionForm";
11-
import { addQuestion } from "@/app/api/internal/questions/helper";
11+
import { addQuestion } from "@/app/questions/helper";
1212

1313
const NewQuestion = () => {
1414
const router = useRouter();

peerprep/components/questionpage/QuestionCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Difficulty, Question } from "@/api/structs";
44
import PeerprepButton from "../shared/PeerprepButton";
55
import { useRouter } from "next/navigation";
66
import styles from "@/style/questionCard.module.css";
7-
import { deleteQuestion } from "@/app/api/internal/questions/helper";
7+
import { deleteQuestion } from "@/app/questions/helper";
88
import DOMPurify from "isomorphic-dompurify";
99

1010
type QuestionCardProps = {

0 commit comments

Comments
 (0)