@@ -40,10 +40,18 @@ export default function Page3() {
4040 const { setCurrentPage } = useNavigationStore ( ) ;
4141
4242 useEffect ( ( ) => {
43- const CHALLENGE_DATA_KEY = 'challengeData' ;
44-
4543 const initializeChallenge = async ( ) => {
46- if ( ! question && id && teamId ) {
44+ // If a question is already loaded, do nothing.
45+ if ( question ) {
46+ console . log ( 'Question already exists, skipping initialization.' ) ;
47+ return ;
48+ }
49+
50+ // If we are on page3, we should have the necessary IDs.
51+ // If not, something is wrong, but providers.tsx should redirect.
52+ if ( id && teamId ) {
53+ console . log ( 'No question found in store. Fetching/assigning challenge from server...' ) ;
54+
4755 // Fetch team members to get their names
4856 let members : TeamMember [ ] = [ ] ;
4957 try {
@@ -68,30 +76,9 @@ export default function Page3() {
6876 return member ? member . name : String ( userId ) ; // Fallback to user_id as string
6977 } ;
7078
71- // 1. Try to restore challenge from localStorage
72- const savedChallengeJSON = localStorage . getItem ( CHALLENGE_DATA_KEY ) ;
73- if ( savedChallengeJSON ) {
74- console . log ( 'Restoring challenge from localStorage...' ) ;
75- try {
76- const savedChallenge = JSON . parse ( savedChallengeJSON ) ;
77- if ( savedChallenge . category && savedChallenge . assigned_challenge_id ) {
78- const questionInfo = questions . find ( ( q ) => q . category === String ( savedChallenge . category ) ) ;
79- if ( questionInfo ) {
80- const memberName = findMemberName ( savedChallenge . user_id ) ;
81- setQuestion ( [ memberName , questionInfo . problem ] . join ( ' ' ) ) ;
82- setChallengeId ( savedChallenge . assigned_challenge_id ) ;
83- return ; // Challenge successfully restored
84- }
85- }
86- } catch ( e ) {
87- console . error ( 'Failed to parse challenge data from localStorage' , e ) ;
88- localStorage . removeItem ( CHALLENGE_DATA_KEY ) ; // Clear corrupted data
89- }
90- }
91-
92- // 2. If no valid saved data, assign a new challenge
93- console . log ( 'No valid saved challenge found. Assigning a new one...' ) ;
9479 try {
80+ // This endpoint will assign a new challenge if one doesn't exist,
81+ // or it should ideally return the existing one.
9582 const assignResponse = await authenticatedFetch ( '/api/v1/challenges/assign' , {
9683 method : 'POST' ,
9784 headers : { 'Content-Type' : 'application/json' } ,
@@ -100,63 +87,40 @@ export default function Page3() {
10087
10188 if ( ! assignResponse . ok ) {
10289 const errorData = await getJsonFromResponse ( assignResponse ) ;
103- throw new Error ( `Failed to assign challenge: ${ errorData . detail } ` ) ;
90+ throw new Error ( `Failed to assign/fetch challenge: ${ errorData . detail } ` ) ;
10491 }
10592
10693 const newChallengeData = await assignResponse . json ( ) ;
107- console . log ( 'Successfully assigned new challenge:' , newChallengeData ) ;
94+ console . log ( 'Successfully assigned/fetched challenge:' , newChallengeData ) ;
10895
10996 if ( newChallengeData . my_assigned && newChallengeData . my_assigned . category && newChallengeData . my_assigned . assigned_challenge_id ) {
110- const dataToSave = {
111- category : String ( newChallengeData . my_assigned . category ) ,
112- assigned_challenge_id : newChallengeData . my_assigned . assigned_challenge_id ,
113- user_id : newChallengeData . my_assigned . user_id ,
114- } ;
115-
116- // Save to localStorage for future restoration
117- localStorage . setItem ( CHALLENGE_DATA_KEY , JSON . stringify ( dataToSave ) ) ;
118- console . log ( 'Saved new challenge to localStorage.' ) ;
119-
120- // Set state from the new challenge data
121- const questionInfo = questions . find ( ( q ) => q . category === dataToSave . category ) ;
97+ const { user_id : userId , category, assigned_challenge_id } = newChallengeData . my_assigned ;
98+
99+ // Set state from the challenge data
100+ const questionInfo = questions . find ( ( q ) => q . category === String ( category ) ) ;
122101 if ( questionInfo ) {
123- const memberName = findMemberName ( dataToSave . user_id ) ;
102+ const memberName = findMemberName ( userId ) ;
124103 setQuestion ( [ memberName , questionInfo . problem ] . join ( ' ' ) ) ;
125- setChallengeId ( dataToSave . assigned_challenge_id ) ;
104+ setChallengeId ( assigned_challenge_id ) ;
105+ } else {
106+ throw new Error ( `Could not find question for category: ${ category } ` ) ;
126107 }
127108 } else {
128109 throw new Error ( 'Assigned challenge data is incomplete or malformed.' ) ;
129110 }
130111 } catch ( error ) {
131- console . error ( 'Error assigning challenge:' , error ) ;
132- // General error handling: leave team, reset state, clear local storage
133- if ( teamId && teamId > 0 ) {
134- try {
135- console . log ( `Attempting to leave team ${ teamId } due to error...` ) ;
136- await authenticatedFetch ( `/api/v1/teams/${ teamId } /cancel` , { method : 'POST' } ) ;
137- console . log ( `Successfully left team ${ teamId } .` ) ;
138- } catch ( cancelError ) {
139- console . error ( `Failed to leave team ${ teamId } :` , cancelError ) ;
140- }
141- }
142- localStorage . removeItem ( CHALLENGE_DATA_KEY ) ;
112+ console . error ( 'Error in challenge initialization:' , error ) ;
113+ // Attempt to recover by going back to team formation
143114 reset ( ) ;
144-
145- setCurrentPage ( 'page1' ) ;
115+ setCurrentPage ( 'page2' ) ;
146116 }
147- } else if ( question ) {
148- console . log ( 'Question already exists, skipping initialization.' ) ;
149- } else {
150- console . log ( 'Missing id or teamId, cannot initialize challenge. Resetting.' ) ;
151- localStorage . removeItem ( CHALLENGE_DATA_KEY ) ;
152- reset ( ) ;
153-
154- setCurrentPage ( 'page1' ) ;
155117 }
156118 } ;
157119
158120 initializeChallenge ( ) ;
159- } , [ id , teamId , question , memberIds , setQuestion , setChallengeId , reset , setCurrentPage ] ) ;
121+ // Added challengeId to dependency array to react to changes if needed,
122+ // though the main trigger is the absence of `question`.
123+ } , [ id , teamId , question , memberIds , setQuestion , setChallengeId , reset , setCurrentPage , challengeId ] ) ;
160124
161125 const handleSubmit = async ( e : React . FormEvent ) => {
162126 e . preventDefault ( ) ;
0 commit comments