Skip to content

Commit 88d7556

Browse files
committed
Merged fix-add-question and home-page
1 parent 833dd56 commit 88d7556

File tree

15 files changed

+233
-1769
lines changed

15 files changed

+233
-1769
lines changed

peerprep/.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# /node_modules
1+
/node_modules
22
# /.next
33
# .DS_Store
44
# .env

peerprep/api/gateway.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { cookies } from "next/headers";
2-
import {
3-
LoginResponse,
4-
Question,
5-
StatusBody,
6-
UserServiceResponse
7-
} from "./structs";
2+
import { LoginResponse, Question, StatusBody, UserServiceResponse } from "./structs";
83
import DOMPurify from "isomorphic-dompurify";
94
import { CookieNames } from "@/app/actions/session";
105

@@ -25,20 +20,21 @@ export function getUserData() {
2520
export function generateJSONHeaders() {
2621
return {
2722
...generateAuthHeaders(),
28-
"Content-type": "application/json; charset=UTF-8"
23+
"Content-type": "application/json; charset=UTF-8,
2924
};
3025
}
3126

3227
export async function fetchQuestion(
33-
questionId: string
28+
questionId: number
3429
): Promise<Question | StatusBody> {
3530
try {
3631
const response = await fetch(
3732
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`,
3833
{
3934
method: "GET",
40-
headers: generateAuthHeaders()
41-
}
35+
headers: generateAuthHeaders(),
36+
cache: "no-store"
37+
},
4238
);
4339
if (!response.ok) {
4440
return {
@@ -47,8 +43,6 @@ export async function fetchQuestion(
4743
};
4844
}
4945

50-
// NOTE: this may cause the following: "Can't resolve canvas"
51-
// https://github.com/kkomelin/isomorphic-dompurify/issues/54
5246
const question = (await response.json()) as Question;
5347
question.content = DOMPurify.sanitize(question.content);
5448
return question;
@@ -69,8 +63,8 @@ export async function getSessionLogin(validatedFields: {
6963
body: JSON.stringify(validatedFields),
7064
headers: {
7165
"Content-type": "application/json; charset=UTF-8"
72-
}
73-
}
66+
},
67+
},
7468
);
7569
const json = await res.json();
7670

@@ -99,8 +93,8 @@ export async function postSignupUser(validatedFields: {
9993
body: JSON.stringify(validatedFields),
10094
headers: {
10195
"Content-type": "application/json; charset=UTF-8"
102-
}
103-
}
96+
},
97+
},
10498
);
10599
const json = await res.json();
106100

@@ -122,7 +116,7 @@ export async function verifyUser(): Promise<UserServiceResponse | StatusBody> {
122116
{
123117
method: "GET",
124118
headers: generateAuthHeaders()
125-
}
119+
},
126120
);
127121
const json = await res.json();
128122

peerprep/app/actions/server_actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
isError,
77
LoginFormSchema,
88
SignupFormSchema,
9+
UserServiceResponse,
910
} from "@/api/structs";
1011
import { createSession } from "@/app/actions/session";
1112
import { redirect } from "next/navigation";
12-
import { cookies } from "next/headers";
13+
import { cookies } from "next/headers"; // credit - taken from Next.JS Auth tutorial
1314

1415
// credit - taken from Next.JS Auth tutorial
1516
export async function signup(state: FormState, formData: FormData) {

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@ export async function GET() {
99
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
1010
{
1111
method: "GET",
12-
headers: generateAuthHeaders()
13-
}
12+
headers: generateAuthHeaders(),
13+
},
1414
);
1515
if (!response.ok) {
1616
return NextResponse.json(
1717
{
1818
error: await response.text(),
19-
status: response.status
19+
status: response.status,
2020
},
21-
{ status: response.status }
21+
{ status: response.status },
2222
);
2323
}
2424
return response;
2525
} catch (err: any) {
2626
return NextResponse.json(
2727
{ error: err.message, status: 400 },
28-
{ status: 400 }
28+
{ status: 400 },
2929
);
3030
}
3131
}
3232

33-
3433
export async function PUT(request: NextRequest) {
3534
const body = (await request.json()) as Question;
3635
try {
@@ -39,26 +38,29 @@ export async function PUT(request: NextRequest) {
3938
{
4039
method: "PUT",
4140
body: JSON.stringify(body),
42-
headers: generateJSONHeaders()
43-
}
41+
headers: generateJSONHeaders(),
42+
},
4443
);
4544
if (response.ok) {
45+
revalidatePath("/questions");
46+
revalidatePath(`/questions/${body.id}`);
47+
revalidatePath(`/questions/edit/${body.title}`);
4648
return NextResponse.json(
4749
{ status: response.status },
48-
{ status: response.status }
50+
{ status: response.status },
4951
);
5052
}
5153
return NextResponse.json(
5254
{
5355
error: (await response.json())["Error adding question: "],
54-
status: response.status
56+
status: response.status,
5557
},
56-
{ status: response.status }
58+
{ status: response.status },
5759
);
5860
} catch (err: any) {
5961
return NextResponse.json(
6062
{ error: err.message, status: 400 },
61-
{ status: 400 }
63+
{ status: 400 },
6264
);
6365
}
6466
}
@@ -71,26 +73,27 @@ export async function POST(request: NextRequest) {
7173
{
7274
method: "POST",
7375
body: JSON.stringify(body),
74-
headers: generateJSONHeaders()
75-
}
76+
headers: generateJSONHeaders(),
77+
},
7678
);
7779
if (response.ok) {
80+
revalidatePath("/questions");
7881
return NextResponse.json(
7982
{ status: response.status },
80-
{ status: response.status }
83+
{ status: response.status },
8184
);
8285
}
8386
return NextResponse.json(
8487
{
8588
error: (await response.json())["Error adding question: "],
86-
status: response.status
89+
status: response.status,
8790
},
88-
{ status: response.status }
91+
{ status: response.status },
8992
);
9093
} catch (err: any) {
9194
return NextResponse.json(
9295
{ error: err.message, status: 400 },
93-
{ status: 400 }
96+
{ status: 400 },
9497
);
9598
}
9699
}
@@ -100,16 +103,16 @@ export async function DELETE(request: NextRequest) {
100103
if (body.qid === undefined) {
101104
return NextResponse.json(
102105
{ error: "No ID specified.", status: 400 },
103-
{ status: 400 }
106+
{ status: 400 },
104107
);
105108
}
106109
try {
107110
const response = await fetch(
108111
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/delete/${body.qid}`,
109112
{
110113
method: "DELETE",
111-
headers: generateAuthHeaders()
112-
}
114+
headers: generateAuthHeaders(),
115+
},
113116
);
114117
if (response.ok) {
115118
// NextResponse doesn't support 204.
@@ -119,14 +122,14 @@ export async function DELETE(request: NextRequest) {
119122
return NextResponse.json(
120123
{
121124
error: (await response.json())["Error deleting question: "],
122-
status: response.status
125+
status: response.status,
123126
},
124-
{ status: response.status }
127+
{ status: response.status },
125128
);
126129
} catch (err: any) {
127130
return NextResponse.json(
128131
{ error: `Bad request: ${err.message}`, status: 400 },
129-
{ status: 400 }
132+
{ status: 400 },
130133
);
131134
}
132135
}

peerprep/app/globals.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ body {
77
@apply overflow-hidden;
88
background-color: #121212;
99
height: 100%;
10+
display: flex;
11+
flex-direction: column;
1012
}
1113

1214
body,

peerprep/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export default function RootLayout({
1919
<body>
2020
<ThemeProvider>
2121
<Navbar />
22+
<main className="flex-grow overflow-y-auto">{children}</main>
2223
</ThemeProvider>
23-
<ThemeProvider>{children}</ThemeProvider>{" "}
2424
</body>
2525
</html>
2626
);

peerprep/app/questions/QuestionForm.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
FormField,
77
FormItem,
88
FormLabel,
9-
FormMessage,
109
} from "@/components/ui/form";
1110
import { Input } from "@/components/ui/input";
1211
import {
@@ -52,7 +51,6 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
5251
<FormControl>
5352
<Input placeholder="Two Sum" {...field} />
5453
</FormControl>
55-
<FormMessage />
5654
</FormItem>
5755
)}
5856
/>
@@ -74,7 +72,6 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
7472
<SelectItem value={Difficulty.Hard}>Hard</SelectItem>
7573
</SelectContent>
7674
</Select>
77-
<FormMessage />
7875
</FormItem>
7976
)}
8077
/>
@@ -97,7 +94,6 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
9794
)}
9895
/>
9996
</FormControl>
100-
<FormMessage />
10197
</FormItem>
10298
)}
10399
/>
@@ -113,7 +109,6 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
113109
onChange={field.onChange}
114110
/>
115111
</FormControl>
116-
<FormMessage />
117112
</FormItem>
118113
)}
119114
/>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import QuestionBlock from "./question";
77

88
type Props = {
99
searchParams: {
10-
match?: string
11-
},
10+
match?: string;
11+
};
1212
params: {
13-
question: string;
13+
question: number;
1414
roomID: string;
1515
};
1616
};
@@ -23,7 +23,7 @@ async function Question({ params, searchParams }: Props) {
2323
try {
2424
userId = JSON.parse(userData as string)?.id;
2525
} catch (err) {
26-
console.log("Failed to parse userid")
26+
console.log("Failed to parse userid");
2727
}
2828

2929
return (

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { fetchQuestion } from "@/api/gateway";
2-
import { Question as QnType, StatusBody, isError } from "@/api/structs";
2+
import { isError, Question as QnType, StatusBody } from "@/api/structs";
33
import styles from "@/style/question.module.css";
44
import ErrorBlock from "@/components/shared/ErrorBlock";
55
import React from "react";
66
import QuestionBlock from "./question";
77

88
type Props = {
99
params: {
10-
question: string;
10+
question: number;
1111
};
1212
};
1313

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import { revalidatePath } from "next/cache";
66

77
type Props = {
88
params: {
9-
question: string;
9+
question: number;
1010
};
1111
};
1212

1313
const EditQuestionPage = async ({ params }: Props) => {
1414
const question = (await fetchQuestion(params.question)) as Question;
1515
console.log("Fetching question");
16+
revalidatePath("/questions");
17+
revalidatePath(`/questions/${params.question}`);
1618
revalidatePath(`/questions/edit/${params.question}`);
1719

1820
return <EditQuestion question={question} />;

0 commit comments

Comments
 (0)