Skip to content

Commit a072ef5

Browse files
committed
Implement Update functionalities
1 parent 8eb21bb commit a072ef5

File tree

4 files changed

+119
-43
lines changed

4 files changed

+119
-43
lines changed

frontend/src/api/leetcode-dashboard.tsx

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import { QuestionMinified } from "@/types/find-match";
1+
import {
2+
QuestionMinified,
3+
QuestionFull,
4+
NewQuestionData,
5+
} from "@/types/find-match";
26

37
const QUESTION_SERVICE = process.env.NEXT_PUBLIC_QUESTION_SERVICE;
48

9+
export const createSingleLeetcodeQuestion = async ({
10+
title,
11+
description,
12+
category,
13+
complexity,
14+
}: NewQuestionData) => {
15+
const url = `${QUESTION_SERVICE}/create`;
16+
};
17+
518
export const getLeetcodeDashboardData = async (): Promise<
619
QuestionMinified[]
720
> => {
@@ -21,3 +34,50 @@ export const fetchSingleLeetcodeQuestion = async (
2134
// console.log(data);
2235
return data;
2336
};
37+
38+
export const updateSingleLeetcodeQuestion = async ({
39+
questionId,
40+
title,
41+
description,
42+
category,
43+
complexity,
44+
}: QuestionFull) => {
45+
const url = `${QUESTION_SERVICE}/${questionId}/update`;
46+
const resp = await fetch(url, {
47+
method: "POST",
48+
headers: {
49+
"Content-Type": "application/json",
50+
},
51+
body: JSON.stringify({
52+
title: title,
53+
description: description,
54+
category: category,
55+
complexity: complexity,
56+
}),
57+
});
58+
59+
if (!resp.ok) {
60+
throw new Error("Failed to update the question");
61+
}
62+
63+
return await resp.json();
64+
};
65+
66+
export const deleteSingleLeetcodeQuestion = async (questionId: string) => {
67+
const url = `${QUESTION_SERVICE}/${questionId}/delete`;
68+
const resp = await fetch(url, {
69+
method: "POST",
70+
headers: {
71+
"Content-Type": "application/json",
72+
},
73+
body: JSON.stringify({
74+
questionId: questionId,
75+
}),
76+
});
77+
78+
if (!resp.ok) {
79+
throw new Error("Failed to update the question");
80+
}
81+
82+
return await resp.json();
83+
};

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

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ 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 { fetchSingleLeetcodeQuestion } from "@/api/leetcode-dashboard";
23+
import {
24+
fetchSingleLeetcodeQuestion,
25+
updateSingleLeetcodeQuestion,
26+
} from "@/api/leetcode-dashboard";
2427

2528
const QUESTION_SERVICE = process.env.NEXT_PUBLIC_QUESTION_SERVICE;
2629

@@ -81,42 +84,36 @@ const EditQuestionDialog = ({
8184

8285
async function onSubmit(values: z.infer<typeof formSchema>) {
8386
console.log(values);
84-
// setIsSubmitting(true);
85-
// const url = `${QUESTION_SERVICE}/create`;
86-
// await fetch(url, {
87-
// method: "POST",
88-
// headers: {
89-
// "Content-Type": "application/json",
90-
// },
91-
// body: JSON.stringify({
92-
// title: values.questionTitle,
93-
// description: values.questionDescription,
94-
// category: values.questionTopics,
95-
// complexity: values.questionDifficulty,
96-
// }),
97-
// })
98-
// .then((response) => {
99-
// if (response.ok) {
100-
// Swal.fire({
101-
// icon: "success",
102-
// title: "Question Added",
103-
// text: "Question has been added successfully.",
104-
// });
105-
// }
87+
setIsSubmitting(true);
88+
updateSingleLeetcodeQuestion({
89+
title: values.questionTitle,
90+
description: values.questionDescription,
91+
category: values.questionTopics,
92+
complexity: values.questionDifficulty,
93+
questionId: questionId,
94+
})
95+
.then((response) => {
96+
if (response.ok) {
97+
Swal.fire({
98+
icon: "success",
99+
title: "Question Added",
100+
text: "Question has been modified successfully.",
101+
});
102+
}
106103

107-
// return response.json();
108-
// })
109-
// .catch((error) => {
110-
// Swal.fire({
111-
// icon: "error",
112-
// title: "Question Add Failed",
113-
// text: "Please try again later.",
114-
// });
115-
// })
116-
// .finally(() => {
117-
// setIsSubmitting(false);
118-
// setClose();
119-
// });
104+
return response.json();
105+
})
106+
.catch((error) => {
107+
Swal.fire({
108+
icon: "error",
109+
title: "Question Add Failed",
110+
text: "Please try again later.",
111+
});
112+
})
113+
.finally(() => {
114+
setIsSubmitting(false);
115+
setClose();
116+
});
120117
}
121118

122119
return (
@@ -126,7 +123,7 @@ const EditQuestionDialog = ({
126123
</div>
127124
<Form {...form}>
128125
<form
129-
onSubmit={form.handleSubmit(onSubmit)} // Ensure form.handleSubmit is used correctly
126+
onSubmit={form.handleSubmit(onSubmit)}
130127
className="flex flex-col gap-4"
131128
>
132129
<FormField
@@ -158,7 +155,7 @@ const EditQuestionDialog = ({
158155
<FormControl>
159156
<select
160157
className="w-full bg-primary-800 text-white p-2 rounded-md border border-white capitalize"
161-
{...field} // Connect field to the select element
158+
{...field}
162159
>
163160
<div>Select difficulty</div>
164161
{Object.values(QuestionDifficulty).map((qd) => (

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {
2222
import { cn } from "@/lib/utils";
2323
import { HiOutlinePencil } from "react-icons/hi";
2424
import { FaRegTrashAlt } from "react-icons/fa";
25-
import { getLeetcodeDashboardData } from "@/api/leetcode-dashboard";
25+
import {
26+
deleteSingleLeetcodeQuestion,
27+
getLeetcodeDashboardData,
28+
} from "@/api/leetcode-dashboard";
2629
import { QuestionMinified } from "@/types/find-match";
2730
import MoonLoader from "react-spinners/MoonLoader";
2831
import EditQuestionDialog from "@/app/(auth)/leetcode-dashboard/EditQuestionDialog";
@@ -48,16 +51,21 @@ export function LeetcodeDashboardTable() {
4851
string | null
4952
>(null);
5053

51-
function handleDelete(questionid: string) {
54+
function handleDelete(questionId: string) {
5255
Swal.fire({
5356
icon: "error",
5457
title: "Confirm delete?",
5558
text: "Are you sure you want to delete this question?",
5659
showCancelButton: true,
5760
confirmButtonText: "Confirm",
58-
}).then((result) => {
61+
}).then(async (result) => {
5962
if (result.isConfirmed) {
60-
Swal.fire("Question deleted successfully!", "", "success");
63+
const res = await deleteSingleLeetcodeQuestion(questionId);
64+
if (res.ok) {
65+
Swal.fire("Question deleted successfully!", "", "success");
66+
} else {
67+
Swal.fire("An error occured, please try again later");
68+
}
6169
}
6270
});
6371
}

frontend/src/types/find-match.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ export interface QuestionMinified {
8383
title: string;
8484
complexity: string;
8585
}
86+
87+
export interface NewQuestionData {
88+
title: string;
89+
description: string;
90+
category: string[];
91+
complexity: string;
92+
}
93+
94+
export interface QuestionFull extends NewQuestionData {
95+
questionId: string;
96+
}

0 commit comments

Comments
 (0)