Skip to content

Commit 9431b78

Browse files
committed
Refactor create leetcode question action
1 parent a072ef5 commit 9431b78

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

frontend/src/api/leetcode-dashboard.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ import {
66

77
const QUESTION_SERVICE = process.env.NEXT_PUBLIC_QUESTION_SERVICE;
88

9-
export const createSingleLeetcodeQuestion = async ({
10-
title,
11-
description,
12-
category,
13-
complexity,
14-
}: NewQuestionData) => {
9+
export const createSingleLeetcodeQuestion = async (
10+
data: NewQuestionData
11+
): Promise<QuestionFull> => {
1512
const url = `${QUESTION_SERVICE}/create`;
13+
const resp = await fetch(url, {
14+
method: "POST",
15+
headers: {
16+
"Content-Type": "application/json",
17+
},
18+
body: JSON.stringify(data),
19+
});
20+
21+
// Check if the response is not OK (e.g., status code 400, 500)
22+
if (!resp.ok) {
23+
throw new Error(`Failed to create the question: ${resp.statusText}`);
24+
}
25+
26+
// Parse and return the JSON response
27+
const result: QuestionFull = await resp.json();
28+
return result;
1629
};
1730

1831
export const getLeetcodeDashboardData = async (): Promise<
@@ -60,7 +73,7 @@ export const updateSingleLeetcodeQuestion = async ({
6073
throw new Error("Failed to update the question");
6174
}
6275

63-
return await resp.json();
76+
return resp.json();
6477
};
6578

6679
export const deleteSingleLeetcodeQuestion = async (questionId: string) => {

frontend/src/app/(auth)/leetcode-dashboard/AddQuestionDialog.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useForm } from "react-hook-form";
2020
import Swal from "sweetalert2";
2121
import { z } from "zod";
2222
import MoonLoader from "react-spinners/MoonLoader";
23+
import { createSingleLeetcodeQuestion } from "@/api/leetcode-dashboard";
2324

2425
const QUESTION_SERVICE = process.env.NEXT_PUBLIC_QUESTION_SERVICE;
2526

@@ -57,29 +58,19 @@ const AddQuestionDialog = ({ setClose }: AddQuestionDialogProp) => {
5758

5859
async function onSubmit(values: z.infer<typeof formSchema>) {
5960
setIsSubmitting(true);
60-
const url = `${QUESTION_SERVICE}/create`;
61-
await fetch(url, {
62-
method: "POST",
63-
headers: {
64-
"Content-Type": "application/json",
65-
},
66-
body: JSON.stringify({
67-
title: values.questionTitle,
68-
description: values.questionDescription,
69-
category: values.questionTopics,
70-
complexity: values.questionDifficulty,
71-
}),
61+
62+
createSingleLeetcodeQuestion({
63+
title: values.questionTitle,
64+
description: values.questionDescription,
65+
category: values.questionTopics,
66+
complexity: values.questionDifficulty,
7267
})
7368
.then((response) => {
74-
if (response.ok) {
75-
Swal.fire({
76-
icon: "success",
77-
title: "Question Added",
78-
text: "Question has been added successfully.",
79-
});
80-
}
81-
82-
return response.json();
69+
Swal.fire({
70+
icon: "success",
71+
title: "Question Added",
72+
text: "Question has been added successfully.",
73+
});
8374
})
8475
.catch((error) => {
8576
Swal.fire({

0 commit comments

Comments
 (0)