11'use client' ;
22
3- import React , { useState , useEffect } from 'react' ;
3+ import React , { useState , useEffect , useRef } from 'react' ;
44import Link from 'next/link' ;
55import { useRouter } from 'next/navigation' ;
66import { Button } from '@/components/ui/button' ;
@@ -21,6 +21,7 @@ export default function Page2() {
2121 return initialInputs ;
2222 } ) ;
2323 const [ showModal , setShowModal ] = useState ( false ) ;
24+ const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ; // Add timeoutRef
2425
2526 useEffect ( ( ) => {
2627 const hasSeenModal = Cookies . get ( 'doNotShowModalPage2' ) ;
@@ -42,6 +43,34 @@ export default function Page2() {
4243 const newInputs = [ ...inputs ] ;
4344 newInputs [ index ] = value ;
4445 setInputs ( newInputs ) ;
46+
47+ // Clear any existing timeout
48+ if ( timeoutRef . current ) {
49+ clearTimeout ( timeoutRef . current ) ;
50+ }
51+
52+ // Set a new timeout to fetch user data after 3 seconds
53+ timeoutRef . current = setTimeout ( ( ) => {
54+ fetchUserById ( index , value ) ;
55+ } , 3000 ) ;
56+ } ;
57+
58+ const fetchUserById = async ( index : number , id : string ) => {
59+ if ( ! id ) return ; // Don't fetch if ID is empty
60+ try {
61+ const response = await fetch ( `${ process . env . APP_HOST } /v1/users/${ id } ` ) ;
62+ if ( ! response . ok ) {
63+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
64+ }
65+ const userData = await response . json ( ) ;
66+ // Assuming the API returns an object with a 'detail' field as per user's confirmation
67+ const newInputs = [ ...inputs ] ;
68+ newInputs [ index ] = userData . detail || id ; // Use fetched detail, or original ID if not found
69+ setInputs ( newInputs ) ;
70+ } catch ( error ) {
71+ console . error ( `Error fetching user ${ id } :` , error ) ;
72+ // Optionally, display an error message to the user
73+ }
4574 } ;
4675
4776 const handleSolveProblem = ( ) => {
@@ -74,11 +103,17 @@ export default function Page2() {
74103 < Label htmlFor = { `team-email-${ index } ` } > ํ์ { index + 1 } </ Label >
75104 { index === 0 && < Label > (๋)</ Label > }
76105 < Input
77- id = { `team-email -${ index } ` }
78- type = "email "
79- placeholder = { `ํ์ ${ index + 1 } ์ ์ด๋ฉ์ผ ์ฃผ์ ` }
106+ id = { `team-id -${ index } ` }
107+ type = "id "
108+ placeholder = { `ํ์ ${ index + 1 } ์ ๋ฒํธ ` }
80109 value = { inputs [ index ] }
81110 onChange = { ( e ) => handleInputChange ( index , e . target . value ) }
111+ onBlur = { ( e ) => {
112+ if ( timeoutRef . current ) {
113+ clearTimeout ( timeoutRef . current ) ; // Clear any pending debounce
114+ }
115+ fetchUserById ( index , e . target . value ) ; // Fetch immediately on blur
116+ } }
82117 disabled = { index === 0 }
83118 />
84119 </ div >
0 commit comments