@@ -14,6 +14,9 @@ import { useHistory } from 'react-router-dom';
1414import { useState } from 'react' ;
1515import { useGetLatestReports , useMarkReportAsRead } from 'common/hooks/useReports' ;
1616import { useCurrentUser } from 'common/hooks/useAuth' ;
17+ import { toggleReportBookmark } from 'common/api/reportService' ;
18+ import { useQueryClient } from '@tanstack/react-query' ;
19+ import { MedicalReport } from 'common/models/medicalReport' ;
1720import Avatar from 'common/components/Icon/Avatar' ;
1821import ReportItem from './components/ReportItem/ReportItem' ;
1922import NoReportsMessage from './components/NoReportsMessage/NoReportsMessage' ;
@@ -27,6 +30,7 @@ import './HomePage.scss';
2730const HomePage : React . FC = ( ) => {
2831 const { t } = useTranslation ( 'home' ) ;
2932 const history = useHistory ( ) ;
33+ const queryClient = useQueryClient ( ) ;
3034 const { data : reports , isLoading, isError } = useGetLatestReports ( 3 ) ;
3135 const { mutate : markAsRead } = useMarkReportAsRead ( ) ;
3236 const currentUser = useCurrentUser ( ) ;
@@ -43,6 +47,31 @@ const HomePage: React.FC = () => {
4347 history . push ( `/tabs/reports/${ reportId } ` ) ;
4448 } ;
4549
50+ const handleToggleBookmark = async ( reportId : string , isCurrentlyBookmarked : boolean ) => {
51+ try {
52+ // Toggle the bookmark status
53+ const updatedReport = await toggleReportBookmark ( reportId , ! isCurrentlyBookmarked ) ;
54+
55+ // Update the reports in the cache
56+ queryClient . setQueryData < MedicalReport [ ] > ( [ 'reports' ] , ( oldReports ) => {
57+ if ( ! oldReports ) return [ ] ;
58+ return oldReports . map ( ( report ) =>
59+ report . id === updatedReport . id ? updatedReport : report ,
60+ ) ;
61+ } ) ;
62+
63+ // Update the latest reports cache with the correct query key including the limit
64+ queryClient . setQueryData < MedicalReport [ ] > ( [ 'latestReports' , 3 ] , ( oldReports ) => {
65+ if ( ! oldReports ) return [ ] ;
66+ return oldReports . map ( ( report ) =>
67+ report . id === updatedReport . id ? updatedReport : report ,
68+ ) ;
69+ } ) ;
70+ } catch ( error ) {
71+ console . error ( 'Failed to toggle bookmark:' , error ) ;
72+ }
73+ } ;
74+
4675 const handleUpload = ( ) => {
4776 history . push ( '/upload' ) ;
4877 } ;
@@ -92,6 +121,7 @@ const HomePage: React.FC = () => {
92121 key = { report . id }
93122 report = { report }
94123 onClick = { ( ) => handleReportClick ( report . id ) }
124+ onToggleBookmark = { ( ) => handleToggleBookmark ( report . id , report . bookmarked ) }
95125 showBookmarkButton = { true }
96126 />
97127 ) ) ;
0 commit comments