11import { zodResolver } from '@hookform/resolvers/zod' ;
2- import { Cross2Icon , DotsVerticalIcon , Pencil1Icon , PlusIcon } from '@radix-ui/react-icons' ;
2+ import { Cross2Icon , PlusIcon } from '@radix-ui/react-icons' ;
33import { useMutation } from '@tanstack/react-query' ;
4- import { type FC , useState } from 'react' ;
4+ import { Loader2 } from 'lucide-react' ;
5+ import { type Dispatch , type FC , type SetStateAction , useState } from 'react' ;
56import { useForm } from 'react-hook-form' ;
67import Markdown from 'react-markdown' ;
78import rehypeKatex from 'rehype-katex' ;
@@ -19,12 +20,6 @@ import {
1920 DialogHeader ,
2021 DialogTitle ,
2122} from '@/components/ui/dialog' ;
22- import {
23- DropdownMenu ,
24- DropdownMenuContent ,
25- DropdownMenuItem ,
26- DropdownMenuTrigger ,
27- } from '@/components/ui/dropdown-menu' ;
2823import {
2924 Form ,
3025 FormControl ,
@@ -47,6 +42,8 @@ import { Textarea } from '@/components/ui/textarea';
4742import type { IGetQuestionDetailsResponse } from '@/types/question-types' ;
4843
4944type AdminEditFormProps = {
45+ isFormOpen : boolean ;
46+ setIsFormOpen : Dispatch < SetStateAction < boolean > > ;
5047 questionDetails : IGetQuestionDetailsResponse [ 'question' ] ;
5148} ;
5249
@@ -57,10 +54,19 @@ const formSchema = z.object({
5754 description : z . string ( ) . min ( 1 ) ,
5855} ) ;
5956
60- export const AdminEditForm : FC < AdminEditFormProps > = ( { questionDetails } ) => {
61- const [ isFormOpen , setIsFormOpen ] = useState ( false ) ;
57+ export const AdminEditForm : FC < AdminEditFormProps > = ( {
58+ questionDetails,
59+ isFormOpen,
60+ setIsFormOpen,
61+ } ) => {
6262 const [ addedTopic , setAddedTopic ] = useState ( '' ) ;
63- const { mutate : _sendUpdate , isPending } = useMutation ( { } ) ;
63+ const {
64+ mutate : sendUpdate ,
65+ isPending,
66+ isSuccess,
67+ } = useMutation ( {
68+ mutationFn : async ( values : z . infer < typeof formSchema > ) => { } ,
69+ } ) ;
6470
6571 const form = useForm < z . infer < typeof formSchema > > ( {
6672 resolver : zodResolver ( formSchema ) ,
@@ -72,7 +78,9 @@ export const AdminEditForm: FC<AdminEditFormProps> = ({ questionDetails }) => {
7278 mode : 'onSubmit' ,
7379 } ) ;
7480
75- const onSubmit = ( _formValues : z . infer < typeof formSchema > ) => { } ;
81+ const onSubmit = ( formValues : z . infer < typeof formSchema > ) => {
82+ sendUpdate ( formValues ) ;
83+ } ;
7684
7785 const addTopic = ( topic : string ) => {
7886 const val = new Set ( form . getValues ( 'topic' ) . map ( ( v ) => v . toLowerCase ( ) ) ) ;
@@ -85,26 +93,6 @@ export const AdminEditForm: FC<AdminEditFormProps> = ({ questionDetails }) => {
8593
8694 return (
8795 < >
88- < DropdownMenu >
89- < DropdownMenuTrigger asChild >
90- < Button
91- className = 'min-h-none ml-auto flex !h-6 items-center gap-2 rounded-lg px-2'
92- size = 'sm'
93- >
94- < span > Actions</ span >
95- < DotsVerticalIcon />
96- </ Button >
97- </ DropdownMenuTrigger >
98- < DropdownMenuContent className = 'border-border' >
99- < DropdownMenuItem
100- onClick = { ( ) => setIsFormOpen ( ( isOpen ) => ! isOpen ) }
101- className = 'flex w-full justify-between gap-2 hover:cursor-pointer'
102- >
103- < span > Edit</ span >
104- < Pencil1Icon />
105- </ DropdownMenuItem >
106- </ DropdownMenuContent >
107- </ DropdownMenu >
10896 < Dialog open = { isFormOpen } onOpenChange = { setIsFormOpen } >
10997 < DialogContent className = 'border-border flex h-dvh w-dvw max-w-screen-lg flex-col' >
11098 < DialogHeader className = '' >
@@ -142,10 +130,10 @@ export const AdminEditForm: FC<AdminEditFormProps> = ({ questionDetails }) => {
142130 value = { field . value }
143131 onValueChange = { field . onChange }
144132 >
145- < SelectTrigger >
133+ < SelectTrigger className = 'focus:ring-secondary-foreground/50' >
146134 < SelectValue />
147135 </ SelectTrigger >
148- < SelectContent >
136+ < SelectContent className = 'border-secondary-foreground/50' >
149137 { ( [ 'Easy' , 'Medium' , 'Hard' ] as const ) . map ( ( value , index ) => (
150138 < SelectItem key = { index } value = { value } >
151139 { value }
@@ -201,7 +189,7 @@ export const AdminEditForm: FC<AdminEditFormProps> = ({ questionDetails }) => {
201189 className = ''
202190 disabled = { isPending }
203191 onClick = { ( ) => {
204- form . setValue ( 'topic' , questionDetails . topic ) ;
192+ form . resetField ( 'topic' ) ;
205193 } }
206194 size = 'sm'
207195 >
@@ -297,10 +285,30 @@ export const AdminEditForm: FC<AdminEditFormProps> = ({ questionDetails }) => {
297285 </ DialogDescription >
298286 < DialogFooter >
299287 < div className = 'flex w-full items-center justify-between' >
300- < Button variant = 'secondary' size = 'sm' >
288+ < Button
289+ disabled = { isPending || isSuccess }
290+ variant = 'secondary'
291+ size = 'sm'
292+ onClick = { ( ) => {
293+ form . reset ( ) ;
294+ setIsFormOpen ( false ) ;
295+ } }
296+ >
301297 Cancel
302298 </ Button >
303- < Button size = 'sm' > Save Changes</ Button >
299+ < Button
300+ disabled = { isPending || isSuccess }
301+ onClick = { ( ) => {
302+ onSubmit ( form . getValues ( ) ) ;
303+ } }
304+ size = 'sm'
305+ className = 'flex gap-2'
306+ >
307+ < span >
308+ { isPending ? 'Submitting' : isSuccess ? 'Updated! Closing form' : 'Save Changes' }
309+ </ span >
310+ { isPending && < Loader2 className = 'size-4 animate-spin' /> }
311+ </ Button >
304312 </ div >
305313 </ DialogFooter >
306314 </ DialogContent >
0 commit comments