|
| 1 | +'use client'; |
| 2 | +import { useParams, useRouter, useSearchParams } from 'next/navigation'; |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import { |
| 5 | + Mail, |
| 6 | + Users, |
| 7 | + Shield, |
| 8 | + AlertCircle, |
| 9 | + Loader2, |
| 10 | + CheckCircle2, |
| 11 | + ArrowRight, |
| 12 | +} from 'lucide-react'; |
| 13 | +import { useAuthStatus } from '@/hooks/use-auth'; |
| 14 | +import { acceptTeamInvitation } from '@/lib/api/hackathons'; |
| 15 | +import { toast } from 'sonner'; |
| 16 | + |
| 17 | +const AcceptTeamInvitationPage = () => { |
| 18 | + const params = useParams(); |
| 19 | + const searchParams = useSearchParams(); |
| 20 | + const router = useRouter(); |
| 21 | + const { isAuthenticated, isLoading: authLoading } = useAuthStatus(); |
| 22 | + |
| 23 | + const [isProcessing, setIsProcessing] = useState(false); |
| 24 | + const [error, setError] = useState<string | null>(null); |
| 25 | + const [successTeamName, setSuccessTeamName] = useState<string>(''); |
| 26 | + const [showAcceptButton, setShowAcceptButton] = useState(false); |
| 27 | + |
| 28 | + const hackathonSlug = params.slug as string; |
| 29 | + const token = params.token as string; |
| 30 | + const redirectToken = searchParams.get('token'); |
| 31 | + const invitationToken = token || redirectToken; |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (!invitationToken) { |
| 35 | + router.push(`/hackathons/${hackathonSlug}`); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // If user is authenticated, show accept button |
| 40 | + if (isAuthenticated && !authLoading) { |
| 41 | + setShowAcceptButton(true); |
| 42 | + } |
| 43 | + |
| 44 | + // If user is not authenticated and not loading, redirect to auth |
| 45 | + if (!isAuthenticated && !authLoading) { |
| 46 | + redirectToAuth(); |
| 47 | + } |
| 48 | + }, [isAuthenticated, authLoading, invitationToken, hackathonSlug]); |
| 49 | + |
| 50 | + const redirectToAuth = () => { |
| 51 | + const redirectUrl = `/hackathons/${hackathonSlug}/team-invitations/${invitationToken}/accept`; |
| 52 | + const authUrl = `/auth?mode=signin&redirect=${encodeURIComponent(redirectUrl)}`; |
| 53 | + router.push(authUrl); |
| 54 | + }; |
| 55 | + |
| 56 | + const handleAcceptInvitation = async () => { |
| 57 | + if (!invitationToken || isProcessing) return; |
| 58 | + |
| 59 | + setIsProcessing(true); |
| 60 | + setError(null); |
| 61 | + |
| 62 | + try { |
| 63 | + const response = await acceptTeamInvitation(hackathonSlug, { |
| 64 | + token: invitationToken, |
| 65 | + }); |
| 66 | + |
| 67 | + if (response.success) { |
| 68 | + setSuccessTeamName(response.data.teamName); |
| 69 | + toast.success(`Successfully joined ${response.data.teamName}!`); |
| 70 | + setTimeout(() => { |
| 71 | + router.push(`/hackathons/${hackathonSlug}`); |
| 72 | + }, 2000); |
| 73 | + } |
| 74 | + } catch (err: any) { |
| 75 | + console.error('Failed to accept invitation:', err); |
| 76 | + |
| 77 | + const errorMessage = err?.message || 'Failed to accept invitation'; |
| 78 | + setError(errorMessage); |
| 79 | + |
| 80 | + // Handle specific error cases |
| 81 | + if (err?.status === 403) { |
| 82 | + if (errorMessage.includes('different email address')) { |
| 83 | + toast.error('This invitation was sent to a different email address'); |
| 84 | + } else { |
| 85 | + toast.error('Authentication required'); |
| 86 | + redirectToAuth(); |
| 87 | + } |
| 88 | + } else if (err?.status === 404) { |
| 89 | + toast.error('Invitation not found or has expired'); |
| 90 | + } else if (err?.status === 409) { |
| 91 | + toast.error('You are already a member of this team'); |
| 92 | + } else { |
| 93 | + toast.error(errorMessage); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + setIsProcessing(false); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + // Loading authentication state |
| 101 | + if (authLoading) { |
| 102 | + return ( |
| 103 | + <div className='flex min-h-screen items-center justify-center bg-gradient-to-br from-gray-900 via-gray-800 to-black p-4'> |
| 104 | + <div className='w-full max-w-md'> |
| 105 | + <div className='rounded-2xl border border-white/10 bg-gray-800/50 p-8 shadow-2xl backdrop-blur-sm'> |
| 106 | + <div className='mb-6 flex justify-center'> |
| 107 | + <div className='relative'> |
| 108 | + <div className='flex h-20 w-20 items-center justify-center rounded-full border border-[#a7f950]/20 bg-[#a7f950]/10'> |
| 109 | + <Users className='h-10 w-10 text-[#a7f950]' /> |
| 110 | + </div> |
| 111 | + <div className='absolute -top-1 -right-1'> |
| 112 | + <Loader2 className='h-6 w-6 animate-spin text-[#a7f950]' /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <h1 className='mb-2 text-center text-2xl font-bold text-white'> |
| 118 | + Verifying Invitation |
| 119 | + </h1> |
| 120 | + |
| 121 | + <p className='mb-6 text-center text-white/70'> |
| 122 | + Please wait while we verify your invitation... |
| 123 | + </p> |
| 124 | + |
| 125 | + <div className='space-y-3'> |
| 126 | + <div className='flex items-center gap-3 text-sm'> |
| 127 | + <div className='h-2 w-2 animate-pulse rounded-full bg-[#a7f950]' /> |
| 128 | + <span className='text-white/70'>Checking authentication</span> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // Show accept button (user is authenticated and ready to accept) |
| 138 | + if (showAcceptButton && !error && !successTeamName) { |
| 139 | + return ( |
| 140 | + <div className='flex min-h-screen items-center justify-center bg-gradient-to-br from-gray-900 via-gray-800 to-black p-4'> |
| 141 | + <div className='w-full max-w-md'> |
| 142 | + <div className='rounded-2xl border border-white/10 bg-gray-800/50 p-8 shadow-2xl backdrop-blur-sm'> |
| 143 | + {/* Icon */} |
| 144 | + <div className='mb-6 flex justify-center'> |
| 145 | + <div className='flex h-20 w-20 items-center justify-center rounded-full border border-[#a7f950]/20 bg-[#a7f950]/10'> |
| 146 | + <Users className='h-10 w-10 text-[#a7f950]' /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + {/* Title */} |
| 151 | + <h1 className='mb-2 text-center text-2xl font-bold text-white'> |
| 152 | + Team Invitation |
| 153 | + </h1> |
| 154 | + |
| 155 | + {/* Description */} |
| 156 | + <p className='mb-6 text-center text-white/70'> |
| 157 | + You've been invited to join a team for this hackathon. Click below |
| 158 | + to accept the invitation and become a team member. |
| 159 | + </p> |
| 160 | + |
| 161 | + {/* Info box */} |
| 162 | + <div className='mb-6 rounded-lg border border-[#a7f950]/20 bg-[#a7f950]/10 p-4'> |
| 163 | + <div className='flex items-start gap-3'> |
| 164 | + <Shield className='mt-0.5 h-5 w-5 flex-shrink-0 text-[#a7f950]' /> |
| 165 | + <div className='text-sm'> |
| 166 | + <p className='mb-1 font-medium text-[#a7f950]'> |
| 167 | + Ready to join? |
| 168 | + </p> |
| 169 | + <p className='text-white/70'> |
| 170 | + By accepting this invitation, you'll be added to the team |
| 171 | + and can start collaborating immediately. |
| 172 | + </p> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + |
| 177 | + {/* Action buttons */} |
| 178 | + <div className='space-y-3'> |
| 179 | + <button |
| 180 | + onClick={handleAcceptInvitation} |
| 181 | + disabled={isProcessing} |
| 182 | + className='flex w-full items-center justify-center gap-2 rounded-lg bg-[#a7f950] px-6 py-3 font-medium text-black transition-colors hover:bg-[#8ae63a] disabled:cursor-not-allowed disabled:opacity-50' |
| 183 | + > |
| 184 | + {isProcessing ? ( |
| 185 | + <> |
| 186 | + <Loader2 className='h-4 w-4 animate-spin' /> |
| 187 | + Accepting Invitation... |
| 188 | + </> |
| 189 | + ) : ( |
| 190 | + <> |
| 191 | + Accept Invitation |
| 192 | + <ArrowRight className='h-4 w-4' /> |
| 193 | + </> |
| 194 | + )} |
| 195 | + </button> |
| 196 | + <button |
| 197 | + onClick={() => router.push(`/hackathons/${hackathonSlug}`)} |
| 198 | + disabled={isProcessing} |
| 199 | + className='w-full rounded-lg border border-white/10 bg-white/5 px-6 py-3 font-medium text-white transition-colors hover:bg-white/10 disabled:cursor-not-allowed disabled:opacity-50' |
| 200 | + > |
| 201 | + Cancel |
| 202 | + </button> |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + // Error state |
| 211 | + if (error && !isProcessing) { |
| 212 | + const isWrongEmail = error.includes('different email address'); |
| 213 | + const isExpired = error.includes('expired') || error.includes('not found'); |
| 214 | + const isAlreadyMember = error.includes('already a member'); |
| 215 | + |
| 216 | + return ( |
| 217 | + <div className='flex min-h-screen items-center justify-center bg-gradient-to-br from-gray-900 via-gray-800 to-black p-4'> |
| 218 | + <div className='w-full max-w-md'> |
| 219 | + <div className='rounded-2xl border border-white/10 bg-gray-800/50 p-8 shadow-2xl backdrop-blur-sm'> |
| 220 | + {/* Icon */} |
| 221 | + <div className='mb-6 flex justify-center'> |
| 222 | + <div className='flex h-20 w-20 items-center justify-center rounded-full border border-[#a7f950]/20 bg-[#a7f950]/10'> |
| 223 | + {isAlreadyMember ? ( |
| 224 | + <CheckCircle2 className='h-10 w-10 text-[#a7f950]' /> |
| 225 | + ) : ( |
| 226 | + <AlertCircle className='h-10 w-10 text-[#a7f950]' /> |
| 227 | + )} |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + |
| 231 | + {/* Title */} |
| 232 | + <h1 className='mb-2 text-center text-2xl font-bold text-white'> |
| 233 | + {isAlreadyMember |
| 234 | + ? 'Already a Team Member' |
| 235 | + : isWrongEmail |
| 236 | + ? 'Wrong Account' |
| 237 | + : isExpired |
| 238 | + ? 'Invitation Expired' |
| 239 | + : 'Invitation Error'} |
| 240 | + </h1> |
| 241 | + |
| 242 | + {/* Description */} |
| 243 | + <p className='mb-6 text-center text-white/70'>{error}</p> |
| 244 | + |
| 245 | + {/* Additional info for wrong email */} |
| 246 | + {isWrongEmail && ( |
| 247 | + <div className='mb-6 rounded-lg border border-[#a7f950]/20 bg-[#a7f950]/10 p-4'> |
| 248 | + <div className='flex items-start gap-3'> |
| 249 | + <Mail className='mt-0.5 h-5 w-5 flex-shrink-0 text-[#a7f950]' /> |
| 250 | + <div className='text-sm'> |
| 251 | + <p className='mb-1 font-medium text-[#a7f950]'> |
| 252 | + Sign up with the correct email |
| 253 | + </p> |
| 254 | + <p className='text-white/70'> |
| 255 | + Create an account with the email this invitation was sent |
| 256 | + to. |
| 257 | + </p> |
| 258 | + </div> |
| 259 | + </div> |
| 260 | + </div> |
| 261 | + )} |
| 262 | + |
| 263 | + {/* Additional info for already member */} |
| 264 | + {isAlreadyMember && ( |
| 265 | + <div className='mb-6 rounded-lg border border-[#a7f950]/20 bg-[#a7f950]/10 p-4'> |
| 266 | + <div className='flex items-start gap-3'> |
| 267 | + <Shield className='mt-0.5 h-5 w-5 flex-shrink-0 text-[#a7f950]' /> |
| 268 | + <div className='text-sm'> |
| 269 | + <p className='mb-1 font-medium text-[#a7f950]'> |
| 270 | + You're all set! |
| 271 | + </p> |
| 272 | + <p className='text-white/70'> |
| 273 | + You're already part of this team. Head back to the |
| 274 | + hackathon page. |
| 275 | + </p> |
| 276 | + </div> |
| 277 | + </div> |
| 278 | + </div> |
| 279 | + )} |
| 280 | + |
| 281 | + {/* Action buttons */} |
| 282 | + <div className='space-y-3'> |
| 283 | + {isWrongEmail ? ( |
| 284 | + <> |
| 285 | + <button |
| 286 | + onClick={() => { |
| 287 | + const redirectUrl = `/hackathons/${hackathonSlug}/team-invitations/${invitationToken}/accept`; |
| 288 | + router.push( |
| 289 | + `/auth?mode=signup&redirect=${encodeURIComponent(redirectUrl)}` |
| 290 | + ); |
| 291 | + }} |
| 292 | + className='flex w-full items-center justify-center gap-2 rounded-lg bg-[#a7f950] px-6 py-3 font-medium text-black transition-colors hover:bg-[#8ae63a]' |
| 293 | + > |
| 294 | + Create Account |
| 295 | + </button> |
| 296 | + <button |
| 297 | + onClick={() => router.push(`/hackathons/${hackathonSlug}`)} |
| 298 | + className='w-full rounded-lg border border-white/10 bg-white/5 px-6 py-3 font-medium text-white transition-colors hover:bg-white/10' |
| 299 | + > |
| 300 | + Visit Hackathon |
| 301 | + </button> |
| 302 | + </> |
| 303 | + ) : ( |
| 304 | + <button |
| 305 | + onClick={() => router.push(`/hackathons/${hackathonSlug}`)} |
| 306 | + className='w-full rounded-lg bg-[#a7f950] px-6 py-3 font-medium text-black transition-colors hover:bg-[#8ae63a]' |
| 307 | + > |
| 308 | + Go to Hackathon |
| 309 | + </button> |
| 310 | + )} |
| 311 | + </div> |
| 312 | + </div> |
| 313 | + </div> |
| 314 | + </div> |
| 315 | + ); |
| 316 | + } |
| 317 | + |
| 318 | + // Success state (brief moment before redirect) |
| 319 | + if (successTeamName) { |
| 320 | + return ( |
| 321 | + <div className='flex min-h-screen items-center justify-center bg-gradient-to-br from-gray-900 via-gray-800 to-black p-4'> |
| 322 | + <div className='w-full max-w-md'> |
| 323 | + <div className='rounded-2xl border border-white/10 bg-gray-800/50 p-8 shadow-2xl backdrop-blur-sm'> |
| 324 | + {/* Icon */} |
| 325 | + <div className='mb-6 flex justify-center'> |
| 326 | + <div className='flex h-20 w-20 items-center justify-center rounded-full border border-[#a7f950]/20 bg-[#a7f950]/10'> |
| 327 | + <CheckCircle2 className='h-10 w-10 text-[#a7f950]' /> |
| 328 | + </div> |
| 329 | + </div> |
| 330 | + |
| 331 | + {/* Title */} |
| 332 | + <h1 className='mb-2 text-center text-2xl font-bold text-white'> |
| 333 | + Successfully Joined! |
| 334 | + </h1> |
| 335 | + |
| 336 | + {/* Description */} |
| 337 | + <p className='mb-6 text-center text-white/70'> |
| 338 | + Welcome to {successTeamName}! Redirecting to hackathon page... |
| 339 | + </p> |
| 340 | + |
| 341 | + {/* Progress bar */} |
| 342 | + <div className='h-1 overflow-hidden rounded-full bg-white/10'> |
| 343 | + <div className='h-full animate-[loading_2s_ease-in-out] bg-[#a7f950]' /> |
| 344 | + </div> |
| 345 | + </div> |
| 346 | + </div> |
| 347 | + </div> |
| 348 | + ); |
| 349 | + } |
| 350 | + |
| 351 | + return null; |
| 352 | +}; |
| 353 | + |
| 354 | +export default AcceptTeamInvitationPage; |
0 commit comments