@@ -33,8 +33,20 @@ interface ChallengeResult {
3333 is_correct : boolean ;
3434}
3535
36+ const getJsonFromResponse = async ( response : Response ) => {
37+ try {
38+ const text = await response . text ( ) ;
39+ if ( text ) {
40+ return JSON . parse ( text ) ;
41+ }
42+ } catch ( e ) {
43+ console . error ( 'Failed to parse response JSON' , e ) ;
44+ }
45+ return { detail : response . statusText || 'An unknown error occurred' } ;
46+ } ;
47+
3648export default function Page4 ( ) {
37- const { id, progressStatus } = useFormStore ( ) ; // Use progressStatus
49+ const { id, progressStatus, teamId , memberIds , reset , setQuestion , setChallengeId } = useFormStore ( ) ; // Use progressStatus
3850 const { setCurrentPage } = useNavigationStore ( ) ;
3951
4052 const [ result , setResult ] = useState < string > ( '' ) ;
@@ -90,8 +102,38 @@ export default function Page4() {
90102 }
91103 } , [ result ] ) ; // Depend on result
92104
93- const handleTryAgain = ( ) => {
94- setCurrentPage ( 'page2' ) ;
105+ const handleTryAgain = async ( ) => {
106+ try {
107+ // First, reset the form store to clear previous challenge data.
108+ // This preserves the user session while clearing challenge-specific state.
109+ reset ( ) ;
110+
111+ // Call assign API to get a new challenge
112+ const assignResponse = await authenticatedFetch ( '/api/v1/challenges/assign' , {
113+ method : 'POST' ,
114+ headers : { 'Content-Type' : 'application/json' } ,
115+ body : JSON . stringify ( { team_id : teamId , my_id : id , members_ids : memberIds . filter ( ( memberId ) => memberId !== Number ( id ) ) } ) ,
116+ } ) ;
117+
118+ if ( ! assignResponse . ok ) {
119+ const errorData = await getJsonFromResponse ( assignResponse ) ;
120+ throw new Error ( `Failed to assign new challenge: ${ errorData . detail } ` ) ;
121+ }
122+
123+ const newChallengeData = await assignResponse . json ( ) ;
124+ console . log ( 'Successfully assigned new challenge:' , newChallengeData ) ;
125+
126+ // Navigate to page3 to start the new challenge. Page3's useEffect will handle populating the question.
127+ setCurrentPage ( 'page3' ) ;
128+ } catch ( error : unknown ) {
129+ console . error ( 'Error trying again:' , error ) ;
130+ let errorMessage = '새로운 챌린지를 할당하는 데 실패했습니다.' ;
131+ if ( error instanceof Error ) {
132+ errorMessage = error . message ;
133+ }
134+ alert ( `재도전 실패: ${ errorMessage } ` ) ;
135+ setCurrentPage ( 'page2' ) ; // Fallback to page2 if something goes wrong
136+ }
95137 } ;
96138
97139 const handleSuccessClick = async ( ) => {
@@ -176,11 +218,10 @@ export default function Page4() {
176218 < h3 className = "text-2xl font-bold mb-4" > 우리 팀원들</ h3 >
177219 < div className = "grid grid-cols-1 md:grid-cols-2 gap-4" >
178220 { teamData . members
179- . filter ( member => member . id !== id ) // Filter out current user's info
221+ . filter ( member => member . user_id !== id ) // Filter out current user's info, and use correct id
180222 . map ( ( member ) => (
181- < div key = { member . id } className = "bg-muted p-4 rounded-lg shadow-md" >
223+ < div key = { member . user_id } className = "bg-muted p-4 rounded-lg shadow-md cursor-pointer hover:bg-muted/80" onClick = { ( ) => handleMemberClick ( member . user_id ) } >
182224 < p className = "text-lg font-semibold" > { member . name } </ p >
183- < p className = "text-sm text-gray-600" > Email: { member . email } </ p >
184225 { member . github_url && (
185226 < p className = "text-sm text-gray-600" >
186227 GitHub: < a href = { member . github_url } target = "_blank" rel = "noopener noreferrer" className = "text-blue-500 hover:underline" > { member . github_url } </ a >
0 commit comments