1
1
import { useMutation } from '@tanstack/react-query' ;
2
+ import { Loader2 } from 'lucide-react' ;
2
3
import { FC , PropsWithChildren , useCallback , useState } from 'react' ;
4
+ import { useNavigate } from 'react-router-dom' ;
3
5
4
6
import { Button } from '@/components/ui/button' ;
5
7
import {
@@ -9,19 +11,31 @@ import {
9
11
DialogHeader ,
10
12
DialogTrigger ,
11
13
} from '@/components/ui/dialog' ;
14
+ import { addQuestionAttempt } from '@/services/question-service' ;
12
15
import { IYjsUserState } from '@/types/collab-types' ;
13
16
17
+ import { COMPLETION_STATES } from '../constants' ;
18
+
14
19
type CompleteDialogProps = {
20
+ userId : string ;
15
21
questionId : number ;
16
22
code : string ;
23
+ language : string ;
17
24
members : Array < IYjsUserState [ 'user' ] > ;
18
25
setCompleting : ( state : string , resetId ?: boolean ) => void ;
19
26
} ;
20
27
21
28
export const CompleteDialog : FC < PropsWithChildren < CompleteDialogProps > > = ( {
22
29
children,
23
30
setCompleting,
31
+ questionId,
32
+ userId,
33
+ code,
34
+ language,
35
+ members,
24
36
} ) => {
37
+ const navigate = useNavigate ( ) ;
38
+
25
39
const [ isOpen , _setIsOpen ] = useState ( false ) ;
26
40
const setIsOpen = useCallback (
27
41
( openState : boolean ) => {
@@ -36,13 +50,28 @@ export const CompleteDialog: FC<PropsWithChildren<CompleteDialogProps>> = ({
36
50
[ isOpen ]
37
51
) ;
38
52
39
- const { mutate : _m } = useMutation ( {
40
- mutationFn : async ( ) => { } ,
53
+ const { mutate : sendCompleteRequest , isPending } = useMutation ( {
54
+ mutationFn : async ( ) => {
55
+ return await addQuestionAttempt ( {
56
+ questionId,
57
+ code,
58
+ language,
59
+ userId1 : userId ,
60
+ userId2 :
61
+ members . length < 2 ? undefined : members . filter ( ( v ) => v . userId !== userId ) [ 0 ] . userId ,
62
+ } ) ;
63
+ } ,
41
64
onSuccess : ( ) => {
42
- setCompleting ( 'success' ) ;
65
+ setCompleting ( COMPLETION_STATES . SUCCESS ) ;
43
66
// Navigate to home page
67
+ setTimeout ( ( ) => {
68
+ setCompleting ( COMPLETION_STATES . EMPTY , true ) ;
69
+ navigate ( '/' ) ;
70
+ } , 200 ) ;
71
+ } ,
72
+ onError : ( ) => {
73
+ setCompleting ( COMPLETION_STATES . ERROR ) ;
44
74
} ,
45
- onError : ( ) => { } ,
46
75
} ) ;
47
76
48
77
return (
@@ -62,7 +91,14 @@ export const CompleteDialog: FC<PropsWithChildren<CompleteDialogProps>> = ({
62
91
>
63
92
Go Back
64
93
</ Button >
65
- < Button > Complete Question</ Button >
94
+ < Button
95
+ disabled = { isPending }
96
+ onClick = { ( ) => sendCompleteRequest ( ) }
97
+ className = 'flex flex-row items-center gap-2'
98
+ >
99
+ < span > Complete Question</ span >
100
+ { isPending && < Loader2 className = 'animate-spin' /> }
101
+ </ Button >
66
102
</ div >
67
103
</ DialogFooter >
68
104
</ DialogContent >
0 commit comments