1
1
"use client" ;
2
+ import { deleteQuestion } from "@/api/gateway" ;
2
3
import { Question , Difficulty } from "@/api/structs" ;
3
4
import Chip from "@/components/shared/Chip" ;
4
5
import PeerprepButton from "@/components/shared/PeerprepButton" ;
5
6
import styles from "@/style/question.module.css" ;
7
+ import { useRouter } from "next/navigation" ;
6
8
7
9
interface Props {
8
10
question : Question ;
@@ -23,6 +25,7 @@ function DifficultyChip({ diff }: DifficultyChipProps) {
23
25
}
24
26
25
27
function QuestionBlock ( { question } : Props ) {
28
+ const router = useRouter ( ) ;
26
29
const keys = question . test_cases ? Object . keys ( question . test_cases ) : [ ] ;
27
30
28
31
const createRow = ( key : string ) => (
@@ -34,6 +37,24 @@ function QuestionBlock({ question }: Props) {
34
37
</ tr >
35
38
) ;
36
39
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
+
37
58
return (
38
59
< >
39
60
< div className = { styles . qn_container } >
@@ -46,10 +67,7 @@ function QuestionBlock({ question }: Props) {
46
67
</ div >
47
68
< PeerprepButton
48
69
className = { ` ${ styles . button } ` }
49
- onClick = {
50
- /* TODO: Replace this function with gateway.delete*/ ( ) =>
51
- console . log ( "Delete Me!" )
52
- }
70
+ onClick = { handleDelete }
53
71
>
54
72
Delete
55
73
</ PeerprepButton >
0 commit comments