11'use client' ;
22
3- import React , { useState , useEffect , useRef } from 'react' ;
3+ import React , { useState , useEffect , useRef , useCallback } from 'react' ;
44import Link from 'next/link' ;
55import { useRouter } from 'next/navigation' ;
66import { Button } from '@/components/ui/button' ;
@@ -11,25 +11,60 @@ import Modal from '@/components/Modal';
1111import Cookies from 'js-cookie' ;
1212
1313export default function Page2 ( ) {
14- const { email } = useFormStore ( ) ;
14+ const { id } = useFormStore ( ) ;
1515 const router = useRouter ( ) ;
16- const [ inputs , setInputs ] = useState < string [ ] > ( ( ) => {
17- const initialInputs = Array ( 5 ) . fill ( '' ) ;
18- if ( email ) {
19- initialInputs [ 0 ] = email ;
20- }
16+ const [ inputs , setInputs ] = useState < Array < { id : string ; displayName : string } > > ( ( ) => {
17+ const initialInputs = Array ( 5 ) . fill ( { id : '' , displayName : '' } ) ;
2118 return initialInputs ;
2219 } ) ;
2320 const [ showModal , setShowModal ] = useState ( false ) ;
2421 const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ; // Add timeoutRef
2522
23+ const fetchUserById = useCallback ( async ( index : number , userId : string ) => {
24+ if ( ! userId ) return ; // Don't fetch if ID is empty
25+
26+ // Prevent adding self as a team member
27+ if ( index !== 0 && id && Number ( userId ) === Number ( id ) ) { // Check only for other team members, not self
28+ alert ( `자기 자신(${ userId } )을 팀원으로 추가할 수 없습니다.` ) ;
29+ const newInputs = [ ...inputs ] ;
30+ newInputs [ index ] = { id : '' , displayName : '' } ; // Clear the input field
31+ setInputs ( newInputs ) ;
32+ return ;
33+ }
34+
35+ try {
36+ const response = await fetch ( `/api/v1/users/${ userId } ` ) ;
37+ if ( ! response . ok ) {
38+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
39+ }
40+ const userData = await response . json ( ) ;
41+ // Assuming the API returns an object with a 'data' field which is the display name
42+ const newInputs = [ ...inputs ] ;
43+ newInputs [ index ] = { id : userId , displayName : userData . data || userId } ; // Store both id and display name
44+ setInputs ( newInputs ) ;
45+ } catch ( error ) {
46+ console . error ( `Error fetching user ${ userId } :` , error ) ;
47+ const newInputs = [ ...inputs ] ;
48+ newInputs [ index ] = { id : userId , displayName : userId } ; // Fallback to just ID if fetch fails
49+ setInputs ( newInputs ) ;
50+ // Optionally, display an error message to the user
51+ }
52+ } , [ id , inputs ] ) ;
53+
2654 useEffect ( ( ) => {
2755 const hasSeenModal = Cookies . get ( 'doNotShowModalPage2' ) ;
2856 if ( ! hasSeenModal ) {
2957 setShowModal ( true ) ;
3058 }
3159 } , [ ] ) ;
3260
61+ // Effect to pre-fill the first input if ID is available
62+ useEffect ( ( ) => {
63+ if ( id && inputs [ 0 ] . id === '' ) { // Only fetch if ID exists and input is empty
64+ fetchUserById ( 0 , id ) ;
65+ }
66+ } , [ id , fetchUserById , inputs ] ) ; // Rerun when id changes
67+
3368 const handleConfirm = ( ) => {
3469 setShowModal ( false ) ;
3570 } ;
@@ -41,7 +76,7 @@ export default function Page2() {
4176
4277 const handleInputChange = ( index : number , value : string ) => {
4378 const newInputs = [ ...inputs ] ;
44- newInputs [ index ] = value ;
79+ newInputs [ index ] = { id : value , displayName : value } ; // Temporarily set display name to ID
4580 setInputs ( newInputs ) ;
4681
4782 // Clear any existing timeout
@@ -55,26 +90,8 @@ export default function Page2() {
5590 } , 3000 ) ;
5691 } ;
5792
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- }
74- } ;
75-
7693 const handleSolveProblem = ( ) => {
77- console . log ( 'Inputs:' , inputs ) ;
94+ console . log ( 'Inputs:' , inputs . map ( input => input . id ) ) ;
7895 // You can add logic here to process the inputs before navigating
7996 router . push ( '/page3' ) ;
8097 } ;
@@ -95,7 +112,6 @@ export default function Page2() {
95112 onDoNotShowAgain = { handleDoNotShowAgain }
96113 isOpen = { showModal }
97114 />
98- { email && < p className = "mt-4" > 이메일: < strong > { email } </ strong > </ p > }
99115
100116 < div className = "mt-8 space-y-4" >
101117 { [ ...Array ( 5 ) ] . map ( ( _ , index ) => (
@@ -106,13 +122,13 @@ export default function Page2() {
106122 id = { `team-id-${ index } ` }
107123 type = "id"
108124 placeholder = { `팀원 ${ index + 1 } 의 번호` }
109- value = { inputs [ index ] }
125+ value = { inputs [ index ] . displayName }
110126 onChange = { ( e ) => handleInputChange ( index , e . target . value ) }
111- onBlur = { ( e ) => {
127+ onBlur = { ( ) => {
112128 if ( timeoutRef . current ) {
113129 clearTimeout ( timeoutRef . current ) ; // Clear any pending debounce
114130 }
115- fetchUserById ( index , e . target . value ) ; // Fetch immediately on blur
131+ fetchUserById ( index , inputs [ index ] . id ) ; // Fetch immediately on blur using the stored ID
116132 } }
117133 disabled = { index === 0 }
118134 />
0 commit comments