1
- import { Question , StatusBody , Difficulty } from "@/api/structs" ;
2
- import styles from '@/style/question.module.css' ;
1
+ "use client" ;
2
+ import { deleteQuestion } from "@/api/gateway" ;
3
+ import { Question , Difficulty } from "@/api/structs" ;
4
+ import Chip from "@/components/shared/Chip" ;
5
+ import PeerprepButton from "@/components/shared/PeerprepButton" ;
6
+ import styles from "@/style/question.module.css" ;
7
+ import { useRouter } from "next/navigation" ;
3
8
4
9
interface Props {
5
- question : Question ;
10
+ question : Question ;
6
11
}
7
12
8
- const difficultyColor = ( diff : Difficulty ) => {
9
- return (
10
- diff === Difficulty . Easy ? < p className = { `${ styles . title } ${ styles . easy } ` } > Easy</ p >
11
- : diff === Difficulty . Medium ? < p className = { `${ styles . title } ${ styles . med } ` } > Med</ p >
12
- : < p className = { `${ styles . title } ${ styles . hard } ` } > Hard</ p >
13
+ interface DifficultyChipProps {
14
+ diff : Difficulty ;
15
+ }
16
+
17
+ function DifficultyChip ( { diff } : DifficultyChipProps ) {
18
+ return diff === Difficulty . Easy ? (
19
+ < Chip className = { styles . easy } > Easy</ Chip >
20
+ ) : diff === Difficulty . Medium ? (
21
+ < Chip className = { styles . med } > Med</ Chip >
22
+ ) : (
23
+ < Chip className = { styles . hard } > Hard</ Chip >
13
24
) ;
14
25
}
15
26
16
27
function QuestionBlock ( { question } : Props ) {
28
+ const router = useRouter ( ) ;
17
29
const keys = question . test_cases ? Object . keys ( question . test_cases ) : [ ] ;
18
30
19
31
const createRow = ( key : string ) => (
20
32
< tr key = { key } >
21
33
< td className = { `${ styles . table } ${ styles . cell } ` } > { key } </ td >
22
- < td className = { `${ styles . table } ${ styles . cell } ` } > { question . test_cases [ key ] } </ td >
34
+ < td className = { `${ styles . table } ${ styles . cell } ` } >
35
+ { question . test_cases [ key ] }
36
+ </ td >
23
37
</ tr >
24
38
) ;
25
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 (
49
+ `Failed to delete question. Code ${ status . status } : ${ status . error } `
50
+ ) ;
51
+ return ;
52
+ }
53
+ console . log ( `Successfully deleted the question.` ) ;
54
+ router . push ( "/questions" ) ;
55
+ } else {
56
+ console . log ( "Deletion cancelled." ) ;
57
+ }
58
+ } ;
59
+
26
60
return (
27
61
< >
28
62
< div className = { styles . qn_container } >
29
63
< div className = { styles . title_wrapper } >
30
- < h1 className = { styles . title } > Q{ question . id } : { question . title } </ h1 >
31
- { difficultyColor ( question . difficulty ) }
64
+ < div className = { styles . label_wrapper } >
65
+ < h1 className = { styles . title } >
66
+ Q{ question . id } : { question . title }
67
+ </ h1 >
68
+ < DifficultyChip diff = { question . difficulty } />
69
+ </ div >
70
+ < PeerprepButton
71
+ className = { ` ${ styles . button } ` }
72
+ onClick = { handleDelete }
73
+ >
74
+ Delete
75
+ </ PeerprepButton >
76
+ </ div >
77
+ < div className = { styles . label_wrapper } >
78
+ < p > Categories: </ p >
79
+ { question . categories . length == 0 ? (
80
+ < p > No categories listed.</ p >
81
+ ) : (
82
+ question . categories . map ( ( elem , idx ) => < p key = { idx } > { elem } </ p > )
83
+ ) }
32
84
</ div >
33
- < br />
34
85
< p > { question . description } </ p >
35
- < br />
86
+ < br />
36
87
{ question . test_cases && (
37
88
< table className = { styles . table } >
38
89
< tbody >
39
90
< tr >
40
- < th className = { `${ styles . table } ${ styles . header } ${ styles . input } ` } > Input</ th >
41
- < th className = { `${ styles . table } ${ styles . header } ${ styles . output } ` } > Expected Output</ th >
91
+ < th
92
+ className = { `${ styles . table } ${ styles . header } ${ styles . input } ` }
93
+ >
94
+ Input
95
+ </ th >
96
+ < th
97
+ className = { `${ styles . table } ${ styles . header } ${ styles . output } ` }
98
+ >
99
+ Expected Output
100
+ </ th >
42
101
</ tr >
43
102
{ keys . map ( createRow ) }
44
103
</ tbody >
45
104
</ table >
46
105
) }
47
106
</ div >
48
107
< form className = { styles . editor_container } >
49
- < textarea className = { styles . code_editor } />
108
+ < textarea className = { styles . code_editor } />
50
109
</ form >
51
110
</ >
52
111
) ;
53
112
}
54
113
55
- export default QuestionBlock ;
114
+ export default QuestionBlock ;
0 commit comments