@@ -6,55 +6,44 @@ import { enrollUserToCourseAction } from '@/actions/dashboard/courseActions'
6
6
import { Button } from '@/components/ui/button'
7
7
import { useToast } from '@/components/ui/use-toast'
8
8
9
- export default function EnrollButton ( {
10
- courseId
11
- } : {
12
- courseId : number
13
- } ) {
9
+ export default function EnrollButton ( { courseId } : { courseId : number } ) {
14
10
const { toast } = useToast ( )
15
11
const [ loading , setLoading ] = useState ( false )
16
12
17
13
return (
18
- < form
19
- onSubmit = { async ( e ) => {
20
- e . preventDefault ( )
21
- async ( ) => {
22
- setLoading ( true )
23
- try {
24
- const enrollUser = await enrollUserToCourseAction ( {
25
- courseId
26
- } )
27
-
28
- console . log ( enrollUser )
29
-
30
- if ( enrollUser . status === 'error' ) {
31
- return toast ( {
32
- title : enrollUser . message ,
33
- description : enrollUser . error ,
34
- variant : 'destructive'
35
- } )
36
- }
14
+ < Button
15
+ disabled = { loading }
16
+ onClick = { async ( ) => {
17
+ setLoading ( true )
18
+ try {
19
+ const enrollUser = await enrollUserToCourseAction ( {
20
+ courseId
21
+ } )
37
22
38
- toast ( {
39
- title : enrollUser . message
40
- } )
41
- } catch ( error ) {
42
- toast ( {
43
- title : 'Error enrolling user' ,
44
- description : error . message ,
23
+ if ( enrollUser . status === 'error' ) {
24
+ return toast ( {
25
+ title : enrollUser . message ,
26
+ description : enrollUser . error ,
45
27
variant : 'destructive'
46
28
} )
47
- } finally {
48
- setLoading ( false )
49
29
}
30
+
31
+ toast ( {
32
+ title : enrollUser . message
33
+ } )
34
+ } catch ( error ) {
35
+ toast ( {
36
+ title : 'Error enrolling user' ,
37
+ description : error . message ,
38
+ variant : 'destructive'
39
+ } )
40
+ } finally {
41
+ setLoading ( false )
50
42
}
51
- } }
43
+ }
44
+ }
52
45
>
53
- < Button
54
- disabled = { loading }
55
- >
56
- { loading ? 'Enrolling...' : 'Enroll Now' }
57
- </ Button >
58
- </ form >
46
+ { loading ? 'Enrolling...' : 'Enroll Now' }
47
+ </ Button >
59
48
)
60
49
}
0 commit comments