Skip to content

Commit 98bbb59

Browse files
committed
Refactor report bookmark functionality to improve cache updates and streamline mutation handling
1 parent b7e6adf commit 98bbb59

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

frontend/src/common/hooks/useReports.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,36 @@ export const useMarkReportAsRead = () => {
7070
export const useToggleReportBookmark = () => {
7171
const queryClient = useQueryClient();
7272

73-
return useMutation({
74-
mutationFn: (params: { reportId: string; isBookmarked: boolean }) =>
75-
toggleReportBookmark(params.reportId, params.isBookmarked),
76-
onSuccess: (updatedReport: MedicalReport) => {
77-
// Update the reports cache
78-
queryClient.setQueryData<MedicalReport[]>([QueryKey.Reports], (oldReports) => {
79-
if (!oldReports) return undefined;
80-
return oldReports.map((report) =>
81-
report.id === updatedReport.id ? updatedReport : report,
82-
);
83-
});
73+
return async (reportId: string, isBookmarked: boolean) => {
74+
return useMutation({
75+
mutationFn: () => toggleReportBookmark(reportId, isBookmarked),
76+
onSuccess: (updatedReport: MedicalReport) => {
77+
// Update the reports cache
78+
queryClient.setQueryData<MedicalReport[]>([QueryKey.Reports], (oldReports) => {
79+
if (!oldReports) return undefined;
80+
return oldReports.map((report) =>
81+
report.id === updatedReport.id ? updatedReport : report,
82+
);
83+
});
8484

85-
// Update the latest reports cache
86-
queryClient.setQueryData<MedicalReport[]>([QueryKey.LatestReports], (oldReports) => {
87-
if (!oldReports) return undefined;
88-
return oldReports.map((report) =>
89-
report.id === updatedReport.id ? updatedReport : report,
85+
// Update the latest reports cache
86+
queryClient.setQueryData<MedicalReport[]>([QueryKey.LatestReports], (oldReports) => {
87+
if (!oldReports) return undefined;
88+
return oldReports.map((report) =>
89+
report.id === updatedReport.id ? updatedReport : report,
90+
);
91+
});
92+
93+
// Update the bookmark status in the report detail page
94+
queryClient.setQueryData<MedicalReport | undefined>(
95+
[QueryKey.ReportDetail, reportId],
96+
(oldReport) => {
97+
if (!oldReport) return undefined;
98+
if (oldReport.id !== updatedReport.id) return oldReport;
99+
return { ...oldReport, bookmarked: updatedReport.bookmarked };
100+
},
90101
);
91-
});
92-
},
93-
});
102+
},
103+
});
104+
};
94105
};

frontend/src/pages/Processing/ProcessingPage.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import './ProcessingPage.scss';
88
import { getAuthConfig } from 'common/api/reportService';
99
import ProcessingError from './components/ProcessingError';
1010
import ProcessingAnimation from './components/ProcessingAnimation';
11+
import { QueryKey } from 'common/utils/constants';
12+
import { useQueryClient } from '@tanstack/react-query';
1113

1214
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
1315

@@ -20,6 +22,7 @@ const ProcessingPage: React.FC = () => {
2022
const firstName = currentUser?.name?.split(' ')[0];
2123
const axios = useAxios();
2224
const history = useHistory();
25+
const queryClient = useQueryClient();
2326

2427
// States to track processing
2528
const [isProcessing, setIsProcessing] = useState(true);
@@ -70,6 +73,9 @@ const ProcessingPage: React.FC = () => {
7073

7174
console.log('Processing complete');
7275

76+
queryClient.invalidateQueries({ queryKey: [QueryKey.Reports] });
77+
queryClient.invalidateQueries({ queryKey: [QueryKey.LatestReports] });
78+
7379
history.push(`/tabs/reports/${reportId}`);
7480
} else if (data.status === 'failed') {
7581
if (data.isMedicalReport === false) {

frontend/src/pages/Reports/ReportDetailPage.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IonPage, IonContent } from '@ionic/react';
22
import { useState } from 'react';
33
import { useHistory, useParams } from 'react-router-dom';
44
import './ReportDetailPage.scss';
5-
import { useQuery } from '@tanstack/react-query';
5+
import { useQuery, useQueryClient } from '@tanstack/react-query';
66
import axios from 'axios';
77
import { MedicalReport } from '../../common/models/medicalReport';
88
import { useTranslation } from 'react-i18next';
@@ -39,6 +39,7 @@ const ReportDetailPage: React.FC = () => {
3939
const { t } = useTranslation();
4040
const { createToast } = useToasts();
4141
const [isUploadModalOpen, setIsUploadModalOpen] = useState(false);
42+
const queryClient = useQueryClient();
4243

4344
const handleUploadComplete = () => {
4445
setIsUploadModalOpen(false);
@@ -108,6 +109,10 @@ const ReportDetailPage: React.FC = () => {
108109
duration: 2000,
109110
});
110111

112+
queryClient.invalidateQueries({ queryKey: [QueryKey.Reports] });
113+
queryClient.invalidateQueries({ queryKey: [QueryKey.LatestReports] });
114+
queryClient.invalidateQueries({ queryKey: [QueryKey.ReportDetail, reportId] });
115+
111116
// Navigate back
112117
history.push('/tabs/home');
113118
} catch (error) {
@@ -130,6 +135,11 @@ const ReportDetailPage: React.FC = () => {
130135
setIsProcessing(true);
131136
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
132137
setIsProcessing(false);
138+
139+
queryClient.invalidateQueries({ queryKey: [QueryKey.Reports] });
140+
queryClient.invalidateQueries({ queryKey: [QueryKey.LatestReports] });
141+
queryClient.invalidateQueries({ queryKey: [QueryKey.ReportDetail, reportId] });
142+
133143
setIsUploadModalOpen(true);
134144
} catch (error) {
135145
setIsProcessing(false);
@@ -169,7 +179,7 @@ const ReportDetailPage: React.FC = () => {
169179

170180
<UploadModal
171181
isOpen={isUploadModalOpen}
172-
onClose={() => setIsUploadModalOpen(false)}
182+
onClose={handleUploadComplete}
173183
onUploadComplete={handleUploadComplete}
174184
/>
175185
</IonContent>

frontend/src/pages/Reports/ReportsListPage.tsx

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ import {
1515
} from '@ionic/react';
1616
import { useTranslation } from 'react-i18next';
1717
import { useHistory, useLocation } from 'react-router-dom';
18-
import { useQuery, useQueryClient } from '@tanstack/react-query';
19-
import { fetchAllReports, toggleReportBookmark } from 'common/api/reportService';
20-
import { useMarkReportAsRead } from 'common/hooks/useReports';
18+
import { useQuery } from '@tanstack/react-query';
19+
import { fetchAllReports } from 'common/api/reportService';
20+
import { useMarkReportAsRead, useToggleReportBookmark } from 'common/hooks/useReports';
2121
import ReportItem from 'pages/Home/components/ReportItem/ReportItem';
2222
import NoReportsMessage from 'pages/Home/components/NoReportsMessage/NoReportsMessage';
2323
import { useState, useMemo, useEffect, useRef } from 'react';
24-
import { MedicalReport } from 'common/models/medicalReport';
2524
import sortSvg from 'assets/icons/sort.svg';
2625
import filterOutlineIcon from 'assets/icons/filter-outline.svg';
2726
import reportsIcon from 'assets/icons/reports.svg';
@@ -43,7 +42,7 @@ const ReportsListPage: React.FC = () => {
4342
const { t } = useTranslation(['report', 'common']);
4443
const history = useHistory();
4544
const location = useLocation();
46-
const queryClient = useQueryClient();
45+
const toggleBookmark = useToggleReportBookmark();
4746
const [filter, setFilter] = useState<FilterOption>('all');
4847
const [sortDirection, setSortDirection] = useState<SortDirection>('desc'); // Default sort by newest first
4948
const [showToast, setShowToast] = useState(false);
@@ -123,23 +122,6 @@ const ReportsListPage: React.FC = () => {
123122
history.push(`/tabs/reports/${reportId}`);
124123
};
125124

126-
const handleToggleBookmark = async (reportId: string, isCurrentlyBookmarked: boolean) => {
127-
try {
128-
// Toggle the bookmark status
129-
const updatedReport = await toggleReportBookmark(reportId, !isCurrentlyBookmarked);
130-
131-
// Update the reports in the cache
132-
queryClient.setQueryData<MedicalReport[]>(['reports'], (oldReports) => {
133-
if (!oldReports) return [];
134-
return oldReports.map((report) =>
135-
report.id === updatedReport.id ? updatedReport : report,
136-
);
137-
});
138-
} catch (error) {
139-
console.error('Failed to toggle bookmark:', error);
140-
}
141-
};
142-
143125
const handleUpload = () => {
144126
history.push('/tabs/upload');
145127
};
@@ -244,7 +226,7 @@ const ReportsListPage: React.FC = () => {
244226
key={report.id}
245227
report={report}
246228
onClick={() => handleReportClick(report.id)}
247-
onToggleBookmark={() => handleToggleBookmark(report.id, report.bookmarked)}
229+
onToggleBookmark={() => toggleBookmark(report.id, report.bookmarked)}
248230
showBookmarkButton
249231
/>
250232
));

0 commit comments

Comments
 (0)