Skip to content

Commit 5c5d286

Browse files
committed
Implement Edit function for Leetcode questions
1 parent 3ef9424 commit 5c5d286

File tree

5 files changed

+223
-104
lines changed

5 files changed

+223
-104
lines changed

frontend/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@types/node": "^20",
4747
"@types/react": "^18",
4848
"@types/react-dom": "^18",
49+
"@types/react-modal": "^3.16.3",
4950
"eslint": "^8",
5051
"eslint-config-next": "14.2.11",
5152
"postcss": "^8",

frontend/src/api/leetcode-dashboard.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ export const getLeetcodeDashboardData = async (): Promise<
88
const url = `${QUESTION_SERVICE}/all`;
99
const response = await fetch(url);
1010
const data = await response.json();
11-
console.log(data);
11+
// console.log(data);
12+
return data;
13+
};
14+
15+
export const fetchSingleLeetcodeQuestion = async (
16+
questionId: string
17+
): Promise<any> => {
18+
const url = `${QUESTION_SERVICE}/${questionId}`;
19+
const response = await fetch(url);
20+
const data = await response.json();
21+
// console.log(data);
1222
return data;
1323
};

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

Lines changed: 89 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
import { topicsList } from "@/app/(auth)/match/page";
44
import { Button } from "@/components/ui/button";
5-
import {
6-
DialogClose,
7-
DialogContent,
8-
DialogHeader,
9-
} from "@/components/ui/dialog";
105
import {
116
Form,
127
FormControl,
@@ -20,13 +15,33 @@ import { MultiSelect } from "@/components/ui/multiselect";
2015
import { Textarea } from "@/components/ui/textarea";
2116
import { QuestionDifficulty } from "@/types/find-match";
2217
import { zodResolver } from "@hookform/resolvers/zod";
18+
import { useEffect, useState } from "react";
2319
import { useForm } from "react-hook-form";
2420
import Swal from "sweetalert2";
2521
import { z } from "zod";
22+
import MoonLoader from "react-spinners/MoonLoader";
23+
import { fetchSingleLeetcodeQuestion } from "@/api/leetcode-dashboard";
2624

2725
const QUESTION_SERVICE = process.env.NEXT_PUBLIC_QUESTION_SERVICE;
2826

29-
const EditQuestionDialog = ({ id }: { id: string }) => {
27+
interface EditQuestionDialogProp {
28+
setClose: () => void;
29+
questionId: string;
30+
}
31+
32+
const initialValues = {
33+
questionTitle: "",
34+
questionDifficulty: "",
35+
questionTopics: [],
36+
questionDescription: "",
37+
};
38+
39+
const EditQuestionDialog = ({
40+
setClose,
41+
questionId,
42+
}: EditQuestionDialogProp) => {
43+
const [isSubmitting, setIsSubmitting] = useState(false);
44+
const [leetcodeData, setLeetcodeData] = useState(initialValues);
3045
const formSchema = z.object({
3146
questionTitle: z.string().min(2, {
3247
message: "Title must be at least 2 characters.",
@@ -44,49 +59,70 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
4459

4560
const form = useForm<z.infer<typeof formSchema>>({
4661
resolver: zodResolver(formSchema),
47-
defaultValues: {
48-
questionTitle: "",
49-
questionDifficulty: "",
50-
questionTopics: [],
51-
questionDescription: "",
52-
},
62+
defaultValues: leetcodeData,
5363
});
5464

55-
function onSubmit(values: z.infer<typeof formSchema>) {
56-
const url = `${QUESTION_SERVICE}/${id}/update`;
57-
fetch(url, {
58-
method: "POST",
59-
headers: {
60-
"Content-Type": "application/json",
61-
},
62-
body: JSON.stringify(values),
63-
})
64-
.then((res) => {
65-
if (res.ok) {
66-
Swal.fire({
67-
icon: "success",
68-
title: "Question updated successfully!",
69-
});
70-
} else {
71-
Swal.fire({
72-
icon: "error",
73-
title: "Failed to update question.",
74-
});
75-
}
76-
})
77-
.catch((error) => {
78-
Swal.fire({
79-
icon: "error",
80-
title: "Failed to update question.",
81-
});
82-
});
65+
const { reset } = form;
66+
67+
useEffect(() => {
68+
fetchSingleLeetcodeQuestion(questionId).then((resp) => {
69+
const questionData = {
70+
questionTitle: resp.title,
71+
questionDifficulty: resp.complexity,
72+
questionTopics: resp.category.map((x: string) => x.toLowerCase()),
73+
questionDescription: resp.description,
74+
};
75+
console.log(questionData);
76+
setLeetcodeData(questionData);
77+
reset(questionData);
78+
});
79+
}, [questionId, reset]);
80+
81+
async function onSubmit(values: z.infer<typeof formSchema>) {
82+
console.log(values);
83+
// setIsSubmitting(true);
84+
// const url = `${QUESTION_SERVICE}/create`;
85+
// await fetch(url, {
86+
// method: "POST",
87+
// headers: {
88+
// "Content-Type": "application/json",
89+
// },
90+
// body: JSON.stringify({
91+
// title: values.questionTitle,
92+
// description: values.questionDescription,
93+
// category: values.questionTopics,
94+
// complexity: values.questionDifficulty,
95+
// }),
96+
// })
97+
// .then((response) => {
98+
// if (response.ok) {
99+
// Swal.fire({
100+
// icon: "success",
101+
// title: "Question Added",
102+
// text: "Question has been added successfully.",
103+
// });
104+
// }
105+
106+
// return response.json();
107+
// })
108+
// .catch((error) => {
109+
// Swal.fire({
110+
// icon: "error",
111+
// title: "Question Add Failed",
112+
// text: "Please try again later.",
113+
// });
114+
// })
115+
// .finally(() => {
116+
// setIsSubmitting(false);
117+
// setClose();
118+
// });
83119
}
84120

85121
return (
86-
<DialogContent className="bg-primary-700 h-80%">
87-
<DialogHeader className="text-[32px] font-semibold text-yellow-500">
122+
<div className="bg-primary-700 p-10 w-[60vw] rounded-lg pb-14">
123+
<div className="text-[32px] font-semibold text-yellow-500">
88124
Edit Question
89-
</DialogHeader>
125+
</div>
90126
<Form {...form}>
91127
<form
92128
onSubmit={form.handleSubmit(onSubmit)} // Ensure form.handleSubmit is used correctly
@@ -123,7 +159,7 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
123159
className="w-full bg-primary-800 text-white p-2 rounded-md border border-white capitalize"
124160
{...field} // Connect field to the select element
125161
>
126-
<option value="">Select difficulty</option>
162+
<div>Select difficulty</div>
127163
{Object.values(QuestionDifficulty).map((qd) => (
128164
<option value={qd} key={qd}>
129165
<span className="capitalize">{qd}</span>
@@ -145,9 +181,9 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
145181
<FormControl>
146182
<MultiSelect
147183
options={topicsList}
148-
onValueChange={field.onChange} // Bind field.onChange for multi-select
149-
value={field.value} // Bind the value for controlled component
150-
placeholder="Select options"
184+
onValueChange={field.onChange}
185+
value={field.value}
186+
placeholder="Select topics"
151187
variant="inverted"
152188
className="bg-primary-800"
153189
/>
@@ -167,19 +203,20 @@ const EditQuestionDialog = ({ id }: { id: string }) => {
167203
<Textarea
168204
placeholder="Type your description here."
169205
className="text-white bg-primary-800"
170-
{...field} // Bind the Textarea to form control
206+
rows={6}
207+
{...field}
171208
/>
172209
</FormControl>
173210
<FormMessage />
174211
</FormItem>
175212
)}
176213
/>
177-
<DialogClose>
178-
<Button type="submit">Submit</Button>
179-
</DialogClose>
214+
<Button type="submit" className="mt-8">
215+
{isSubmitting ? <MoonLoader size="20" /> : "Submit"}
216+
</Button>
180217
</form>
181218
</Form>
182-
</DialogContent>
219+
</div>
183220
);
184221
};
185222

0 commit comments

Comments
 (0)