1
1
"use client" ;
2
2
3
+ import { fetchAllUsers , fetchIsAdmin , promoteToAdmin } from "@/app/api" ;
3
4
import Button from "@/app/components/button/Button" ;
4
5
import useAdmin from "@/app/hooks/useAdmin" ;
6
+ import { message } from "antd" ;
5
7
import { useRouter } from "next/navigation" ;
6
- import { use , useState } from "react" ;
8
+ import { use , useEffect , useState } from "react" ;
7
9
import Select , { MultiValue } from "react-select" ;
8
10
9
11
const AdminPortalPage = ( ) => {
10
12
const isAdmin = useAdmin ( ) ;
11
13
const router = useRouter ( ) ;
14
+ const [ api , contextHolder ] = message . useMessage ( ) ;
15
+
16
+ type User = {
17
+ uid : string ;
18
+ name : string ;
19
+ imageUrl : string | null ;
20
+ preferredLang : string | null ;
21
+ role : string ;
22
+ } ;
12
23
13
24
interface SelectOptionType {
14
25
label : string ;
15
26
value : string ;
16
27
}
17
- const adminOptions : MultiValue < SelectOptionType > = [
18
- { label : "Hello" , value : "123" } ,
19
- { label : "Hello2" , value : "456" } ,
20
- ] ;
28
+ const [ adminOptions , setAdminOptions ] = useState <
29
+ MultiValue < SelectOptionType >
30
+ > ( [ ] ) ;
31
+
32
+ useEffect ( ( ) => {
33
+ fetchAllUsers ( ) . then ( ( allUsers ) => {
34
+ setAdminOptions (
35
+ allUsers . payload
36
+ . filter ( ( user : User ) => user . role === "user" )
37
+ . map ( ( user : User ) => ( { label : user . name , value : user . uid } ) ) ,
38
+ ) ;
39
+ } ) ;
40
+ } , [ api , contextHolder ] ) ;
21
41
22
42
const handleSelectChange = (
23
43
selectedOptions : MultiValue < SelectOptionType > ,
@@ -35,6 +55,7 @@ const AdminPortalPage = () => {
35
55
36
56
return (
37
57
< main className = "mt-12 flex flex-col items-center justify-center" >
58
+ { contextHolder }
38
59
< h1 className = "mb-2 block text-5xl font-bold text-white underline" >
39
60
Admin Portal
40
61
</ h1 >
@@ -58,7 +79,28 @@ const AdminPortalPage = () => {
58
79
classNamePrefix = "select"
59
80
/>
60
81
</ section >
61
- < Button className = "btn-accent" onClick = { ( ) => "todo" } >
82
+ < Button
83
+ className = "btn-accent"
84
+ onClick = { ( ) => {
85
+ promoteToAdmin ( selectedQnType . map ( ( option ) => option . value ) )
86
+ . then ( ( res ) => {
87
+ if ( res . statusMessage . type . toLowerCase ( ) === "success" ) {
88
+ api . success ( {
89
+ type : "success" ,
90
+ content : "Successfully updated profile!" ,
91
+ } ) ;
92
+ } else {
93
+ api . error ( {
94
+ type : "error" ,
95
+ content : "Failed to update profile :(" ,
96
+ } ) ;
97
+ }
98
+ } )
99
+ . then ( ( ) => {
100
+ setSelectedQnType ( [ ] ) ;
101
+ } ) ;
102
+ } }
103
+ >
62
104
Grant Access
63
105
</ Button >
64
106
</ div >
0 commit comments