Skip to content

Commit d31617a

Browse files
committed
Add delete functionality to button on qn page
1 parent ae166c9 commit d31617a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"use client";
2+
import { deleteQuestion } from "@/api/gateway";
23
import { Question, Difficulty } from "@/api/structs";
34
import Chip from "@/components/shared/Chip";
45
import PeerprepButton from "@/components/shared/PeerprepButton";
56
import styles from "@/style/question.module.css";
7+
import { useRouter } from "next/navigation";
68

79
interface Props {
810
question: Question;
@@ -23,6 +25,7 @@ function DifficultyChip({ diff }: DifficultyChipProps) {
2325
}
2426

2527
function QuestionBlock({ question }: Props) {
28+
const router = useRouter();
2629
const keys = question.test_cases ? Object.keys(question.test_cases) : [];
2730

2831
const createRow = (key: string) => (
@@ -34,6 +37,24 @@ function QuestionBlock({ question }: Props) {
3437
</tr>
3538
);
3639

40+
const handleDelete = async () => {
41+
if (
42+
confirm(
43+
`Are you sure you want to delete ${question.title}? (ID: ${question.id}) `
44+
)
45+
) {
46+
const status = await deleteQuestion(question);
47+
if (status.error) {
48+
alert(`Failed to delete question. Code ${status.status}: ${status.error}`);
49+
return;
50+
}
51+
console.log(`Successfully deleted the question.`);
52+
router.push("/questions");
53+
} else {
54+
console.log("Deletion cancelled.");
55+
}
56+
}
57+
3758
return (
3859
<>
3960
<div className={styles.qn_container}>
@@ -46,10 +67,7 @@ function QuestionBlock({ question }: Props) {
4667
</div>
4768
<PeerprepButton
4869
className={` ${styles.button}`}
49-
onClick={
50-
/* TODO: Replace this function with gateway.delete*/ () =>
51-
console.log("Delete Me!")
52-
}
70+
onClick={handleDelete}
5371
>
5472
Delete
5573
</PeerprepButton>

0 commit comments

Comments
 (0)