@@ -106,10 +106,52 @@ function MatchingPage() {
106106 const [ profile , setProfile ] = useState ( null ) ;
107107 const [ location , setLocation ] = useState ( '' ) ;
108108 const [ matches , setMatches ] = useState ( [ ] ) ;
109+ const [ loadingHistory , setLoadingHistory ] = useState ( true ) ;
109110 const [ matchingLoading , setMatchingLoading ] = useState ( false ) ;
110111 const [ showExplanationModal , setShowExplanationModal ] = useState ( false ) ;
111112 const [ selectedExplanation , setSelectedExplanation ] = useState ( null ) ;
112113
114+ // Load previously stored matches on first mount
115+ useEffect ( ( ) => {
116+ ( async ( ) => {
117+ try {
118+ const token = await getAccessTokenSilently ( ) ;
119+ const historyRes = await fetch ( `${ API_URL } /matching/history/${ encodeURIComponent ( user . sub ) } ` , {
120+ headers : { Authorization : `Bearer ${ token } ` } ,
121+ } ) ;
122+ if ( historyRes . ok ) {
123+ const history = await historyRes . json ( ) ;
124+ // Fetch user details for each matched user in parallel
125+ const userDetailsArr = await Promise . all ( history . map ( h =>
126+ fetch ( `${ API_URL } /user/${ encodeURIComponent ( h . matchedUserId ) } ` , {
127+ headers : { Authorization : `Bearer ${ token } ` } ,
128+ } ) . then ( res => res . ok ? res . json ( ) : null )
129+ ) ) ;
130+ const userMap = Object . fromEntries ( userDetailsArr . filter ( Boolean ) . map ( u => [ u . id , u ] ) ) ;
131+
132+ const formatted = history . map ( h => {
133+ const u = userMap [ h . matchedUserId ] || { } ;
134+ return {
135+ id : h . matchedUserId ,
136+ name : u . name || 'Unknown' ,
137+ distance : 0 ,
138+ avatar : u . picture || '/images/default-avatar.png' ,
139+ sports : u . sportInterests || [ ] ,
140+ shared : Array . isArray ( h . commonPreferences ) ? h . commonPreferences . join ( ', ' ) : '' ,
141+ match : h . score ? Math . round ( h . score * 100 ) : 0 ,
142+ explanation : h . explanation ,
143+ } ;
144+ } ) ;
145+ setMatches ( formatted ) ;
146+ }
147+ } catch ( e ) {
148+ console . warn ( 'Failed to load match history' , e ) ;
149+ } finally {
150+ setLoadingHistory ( false ) ;
151+ }
152+ } ) ( ) ;
153+ } , [ ] ) ; // run once on mount
154+
113155 // validation helpers and Match click handler
114156 const hasValidAvailability = ( avl ) => avl && Object . values ( avl ) . some ( ( arr ) => Array . isArray ( arr ) && arr . length ) ;
115157
@@ -337,7 +379,7 @@ function MatchingPage() {
337379
338380 { /* Matches list */ }
339381 { view === 'cards' && (
340- matchingLoading ? (
382+ ( matchingLoading || loadingHistory ) ? (
341383 < div className = "match-loader" >
342384 < div className = "spinner" />
343385 < p > Finding your best matches…</ p >
@@ -349,7 +391,7 @@ function MatchingPage() {
349391 < button className = "btn-link edit" > ⚙️ Edit Preferences</ button >
350392 </ div >
351393 < div className = "match-grid" >
352- { ( matches . length ? matches : mockMatches ) . map ( ( m ) => (
394+ { matches . map ( ( m ) => (
353395 < div key = { m . id } className = "match-card card" >
354396 < div className = "match-header" >
355397 < img src = { m . avatar } alt = { m . name } className = "avatar-sm" />
0 commit comments