33'use client'
44
55import { useEffect , useState } from 'react'
6- import Link from 'next/link '
6+ import { useRouter } from 'next/navigation '
77import { motion , useAnimation } from 'framer-motion'
8+ import { User } from 'lucia'
9+ import { useRazorpay } from '@/hooks/use-razorpay'
810import {
911 Clock ,
1012 CreditCard ,
13+ Loader2 ,
1114 FilePlus2 ,
1215 FileQuestion ,
1316 Pencil ,
@@ -39,8 +42,11 @@ type Exam = {
3942 price : number
4043}
4144
42- export default function AvailableExams ( ) {
43- const { exams, setExams } = useGlobalStore ( )
45+ export default function AvailableExams ( { user } : { user : User } ) {
46+ const { exams, setExams } = useGlobalStore ( ( state ) => ( {
47+ exams : state . exams ,
48+ setExams : state . setExams ,
49+ } ) )
4450
4551 const [ selectedExam , setSelectedExam ] = useState < Exam | null > ( null )
4652 const [ isCreateModalOpen , setCreateModalOpen ] = useState ( false )
@@ -50,6 +56,10 @@ export default function AvailableExams() {
5056 const [ examToDelete , setExamToDelete ] = useState < string | null > ( null )
5157
5258 const controls = useAnimation ( )
59+ const router = useRouter ( )
60+ const processPayment = useRazorpay ( )
61+
62+ const [ isLoading , setIsLoading ] = useState ( false )
5363
5464 useEffect ( ( ) => {
5565 controls . start ( ( i ) => ( {
@@ -108,6 +118,21 @@ export default function AvailableExams() {
108118 return colors [ index % colors . length ]
109119 }
110120
121+ const handlePaymentSuccess = ( examId : string ) => {
122+ router . push ( `/take/${ examId } ` )
123+ }
124+
125+ const handleTakeTestClick = ( examId : string , amount : number ) => async ( ) => {
126+ setIsLoading ( true )
127+ await processPayment ( {
128+ amount,
129+ examId,
130+ successCallback : ( ) => handlePaymentSuccess ( examId ) ,
131+ user,
132+ } )
133+ setIsLoading ( false )
134+ }
135+
111136 return (
112137 < div className = 'min-h-screen w-full' >
113138 < div className = 'mx-auto px-4 py-12' >
@@ -120,7 +145,7 @@ export default function AvailableExams() {
120145 < h1 className = 'text-4xl font-bold text-foreground' >
121146 Available Exams
122147 </ h1 >
123- < p className = 'mt-2 text-lg text-muted-foreground max-w-2xl ' >
148+ < p className = 'mt-2 max-w-2xl text-lg text-muted-foreground' >
124149 Choose from our selection of professional exams to test and
125150 certify your skills.
126151 </ p >
@@ -143,13 +168,13 @@ export default function AvailableExams() {
143168 whileHover = { { y : - 5 , transition : { duration : 0.2 } } }
144169 >
145170 < Card
146- className = { `h-full flex flex-col bg-gradient-to-br ${ getGradientColor (
171+ className = { `flex h-full flex-col bg-gradient-to-br ${ getGradientColor (
147172 index
148- ) } hover:shadow-lg transition-all duration-300 border border-secondary `}
173+ ) } border border-secondary transition-all duration-300 hover:shadow-lg `}
149174 >
150175 < CardHeader >
151176 < div className = 'flex justify-between' >
152- < CardTitle className = 'text-xl mb-2' >
177+ < CardTitle className = 'mb-2 text-xl ' >
153178 { exam . title }
154179 </ CardTitle >
155180 < div className = 'flex gap-2' >
@@ -171,29 +196,37 @@ export default function AvailableExams() {
171196 < CardDescription > { exam . description } </ CardDescription >
172197 </ CardHeader >
173198 < CardContent className = 'flex-grow' >
174- < div className = 'flex items-center mb-4 text-muted-foreground' >
199+ < div className = 'mb-4 flex items-center text-muted-foreground' >
175200 < Clock className = 'mr-2 h-4 w-4' />
176201 < span > { exam . duration } minutes</ span >
177202 </ div >
178- < div className = 'flex items-center mb-4 text-muted-foreground' >
203+ < div className = 'mb-4 flex items-center text-muted-foreground' >
179204 < FileQuestion className = 'mr-2 h-4 w-4' />
180205 < span > { exam . numQuestions } Questions</ span >
181206 </ div >
182- < div className = 'flex items-center text-foreground font-semibold' >
207+ < div className = 'flex items-center font-semibold text-foreground ' >
183208 < CreditCard className = 'mr-2 h-4 w-4' />
184209 < span > INR { exam . price } </ span >
185210 </ div >
186211 </ CardContent >
187212 < CardFooter >
188- < Button asChild className = 'w-full' >
189- < Link href = { `/take/${ exam . id } ` } > Take Test</ Link >
213+ < Button
214+ className = 'w-full'
215+ disabled = { isLoading }
216+ onClick = { handleTakeTestClick ( exam . id , exam . price ) }
217+ >
218+ { isLoading ? (
219+ < Loader2 className = 'mr-2 h-4 w-4 animate-spin' />
220+ ) : (
221+ 'Pay & Take Test'
222+ ) }
190223 </ Button >
191224 </ CardFooter >
192225 </ Card >
193226 </ motion . div >
194227 ) )
195228 ) : (
196- < div className = 'flex flex-col h-96 justify-center items-center' >
229+ < div className = 'flex h-96 flex-col items-center justify -center' >
197230 < Loader />
198231 Loading Exams...
199232 </ div >
0 commit comments