1
1
'use client' ;
2
- import React from 'react' ;
2
+ import React , { useState } from 'react' ;
3
3
import { useFilteredProblems } from '@/hooks/useFilteredProblems' ;
4
4
import FilterBar from '../(main)/components/filter/FilterBar' ;
5
5
import ProblemTable from '../../components/problems/ProblemTable' ;
6
6
import { axiosQuestionClient } from '@/network/axiosClient' ;
7
7
import { Problem } from '@/types/types' ;
8
8
import { isAxiosError } from 'axios' ;
9
+ import ProblemInputDialog from '@/components/problems/ProblemInputDialog' ;
10
+ import InformationDialog from '@/components/dialogs/InformationDialog' ;
9
11
10
12
function AdminPage ( ) {
13
+ const [ isAddDialogOpen , setIsAddDialogOpen ] = useState ( false ) ;
14
+ const [ informationDialog , setInformationDialog ] = useState ( '' ) ;
11
15
const {
12
16
problems,
13
17
filters,
@@ -61,13 +65,60 @@ function AdminPage() {
61
65
}
62
66
} ;
63
67
68
+ const handleAdd = async ( problem : Problem ) => {
69
+ // TODO: Add proper validation of fields
70
+ if (
71
+ problem . description === '' ||
72
+ problem . title === '' ||
73
+ problem . tags . length === 0
74
+ ) {
75
+ setInformationDialog ( 'Please fill in all required fields' ) ;
76
+ return ;
77
+ }
78
+ try {
79
+ const res = await axiosQuestionClient . post ( `/questions` , {
80
+ difficulty : problem . difficulty ,
81
+ description : problem . description ,
82
+ examples : problem . examples ,
83
+ constraints : problem . constraints ,
84
+ tags : problem . tags ,
85
+ title_slug : problem . title_slug ,
86
+ title : problem . title ,
87
+ } ) ;
88
+
89
+ refetchFilter ( ) ;
90
+ setIsAddDialogOpen ( false ) ;
91
+ return res ;
92
+ } catch ( e : unknown ) {
93
+ if ( isAxiosError ( e ) ) {
94
+ switch ( e . status ) {
95
+ case 400 :
96
+ throw new Error ( 'Invalid question data. Please check your input.' ) ;
97
+ case 409 :
98
+ throw new Error ( 'Question already exists' ) ;
99
+ case 404 :
100
+ throw new Error ( 'Question not found' ) ;
101
+ default :
102
+ throw new Error ( 'Failed to update question' ) ;
103
+ }
104
+ }
105
+ if ( e instanceof Error ) {
106
+ throw new Error ( e . message ) ;
107
+ } else {
108
+ throw new Error ( 'An unknown error occurred' ) ;
109
+ }
110
+ }
111
+ } ;
112
+
64
113
return (
65
114
< div className = "min-h-screen bg-gray-900 p-6 pt-24 text-gray-100" >
66
115
< div className = "mx-auto max-w-7xl" >
67
116
< FilterBar
68
117
filters = { filters }
69
118
updateFilter = { updateFilter }
70
119
removeFilter = { removeFilter }
120
+ isAdmin
121
+ buttonCallback = { ( ) => setIsAddDialogOpen ( true ) }
71
122
/>
72
123
< ProblemTable
73
124
problems = { problems }
@@ -77,6 +128,19 @@ function AdminPage() {
77
128
handleEdit = { handleEdit }
78
129
/>
79
130
</ div >
131
+ < ProblemInputDialog
132
+ isOpen = { isAddDialogOpen }
133
+ onClose = { ( ) => setIsAddDialogOpen ( false ) }
134
+ requestCallback = { handleAdd }
135
+ requestTitle = "Add"
136
+ />
137
+
138
+ < InformationDialog
139
+ isOpen = { informationDialog !== '' }
140
+ onClose = { ( ) => setInformationDialog ( '' ) }
141
+ title = "Status"
142
+ description = { informationDialog }
143
+ />
80
144
</ div >
81
145
) ;
82
146
}
0 commit comments