11'use client' ;
22
33import { useState , useEffect , useCallback , useRef } from 'react' ;
4- import { useRouter } from 'next/navigation' ;
4+ import { useRouter , useSearchParams } from 'next/navigation' ;
55import Link from 'next/link' ;
66import { createClient } from '@/lib/supabase/client' ;
77import { toast } from 'sonner' ;
@@ -23,6 +23,10 @@ interface User {
2323}
2424
2525export default function CompleteProfile ( ) {
26+ const router = useRouter ( ) ;
27+ const searchParams = useSearchParams ( ) ;
28+ const returnUrl = searchParams . get ( 'returnUrl' ) || '/protected/dashboard' ;
29+
2630 const [ firstName , setFirstName ] = useState ( '' ) ;
2731 const [ lastName , setLastName ] = useState ( '' ) ;
2832 const [ username , setUsername ] = useState ( '' ) ;
@@ -32,7 +36,6 @@ export default function CompleteProfile() {
3236 const [ usernameError , setUsernameError ] = useState < string > ( '' ) ;
3337 const [ user , setUser ] = useState < User | null > ( null ) ;
3438 const [ isValidating , setIsValidating ] = useState ( true ) ;
35- const router = useRouter ( ) ;
3639 const usernameCheckTimeout = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
3740
3841 const getSupabaseClient = ( ) => {
@@ -60,7 +63,7 @@ export default function CompleteProfile() {
6063
6164 if ( isProfileComplete ) {
6265 // Profile is already complete, redirect to dashboard
63- router . push ( '/protected/dashboard' ) ;
66+ router . push ( returnUrl ) ;
6467 return ;
6568 }
6669
@@ -139,20 +142,15 @@ export default function CompleteProfile() {
139142 . single ( ) ;
140143
141144 if ( error && error . code !== 'PGRST116' ) {
142- console . error ( 'Username check error:' , error ) ;
143- // If there's a database error, assume username is available to allow form submission
144- setUsernameAvailable ( true ) ;
145- setUsernameError ( '' ) ;
146- return ;
145+ throw error ;
147146 }
148147
149148 // If no data found, username is available
150149 setUsernameAvailable ( ! data ) ;
151150 } catch ( error ) {
152151 console . error ( 'Error checking username:' , error ) ;
153- // On any error, assume username is available to allow form submission
154- setUsernameAvailable ( true ) ;
155- setUsernameError ( '' ) ;
152+ setUsernameAvailable ( null ) ;
153+ setUsernameError ( 'Unable to check username availability' ) ;
156154 } finally {
157155 setIsCheckingUsername ( false ) ;
158156 }
@@ -191,7 +189,7 @@ export default function CompleteProfile() {
191189 return ;
192190 }
193191
194- if ( usernameAvailable === false ) {
192+ if ( ! usernameAvailable ) {
195193 toast . error ( 'Username is not available' ) ;
196194 return ;
197195 }
@@ -228,7 +226,7 @@ export default function CompleteProfile() {
228226 }
229227
230228 toast . success ( 'Profile completed successfully! Welcome to CodeUnia! 🎉' ) ;
231- router . push ( '/protected/dashboard' ) ;
229+ router . push ( returnUrl ) ;
232230 } catch ( error ) {
233231 console . error ( 'Error updating profile:' , error ) ;
234232 toast . error ( 'Error completing profile setup' ) ;
@@ -280,7 +278,7 @@ export default function CompleteProfile() {
280278 Welcome! Let's set up your profile
281279 </ h1 >
282280 < p className = "text-gray-600 leading-relaxed" >
283- Complete your profile to get started with Codeunia . This will only take a moment.
281+ Complete your profile to get started with CodeUnia . This will only take a moment.
284282 </ p >
285283 </ div >
286284
@@ -424,9 +422,9 @@ export default function CompleteProfile() {
424422 { /* Submit Button */ }
425423 < button
426424 type = "submit"
427- disabled = { isLoading || ! firstName . trim ( ) || ! lastName . trim ( ) || ! username || usernameAvailable === false }
425+ disabled = { isLoading || ! firstName . trim ( ) || ! lastName . trim ( ) || ! username || ! usernameAvailable || ! ! usernameError }
428426 className = { `w-full py-4 px-6 rounded-xl font-semibold text-sm transition-all duration-200 ${
429- isLoading || ! firstName . trim ( ) || ! lastName . trim ( ) || ! username || usernameAvailable === false
427+ isLoading || ! firstName . trim ( ) || ! lastName . trim ( ) || ! username || ! usernameAvailable || ! ! usernameError
430428 ? 'bg-gray-200 text-gray-400 cursor-not-allowed'
431429 : 'bg-gradient-to-r from-blue-600 to-indigo-600 text-white hover:from-blue-700 hover:to-indigo-700 hover:shadow-lg hover:scale-[1.02] active:scale-[0.98]'
432430 } `}
0 commit comments