Skip to content

Commit b0a3365

Browse files
committed
Refactor bookmark toggle functionality to use mutation object for improved clarity and consistency
1 parent 1683607 commit b0a3365

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

frontend/src/common/hooks/useReports.ts

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

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-
});
84-
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-
});
73+
return useMutation({
74+
mutationFn: ({ reportId, isBookmarked }: { reportId: string; isBookmarked: boolean }) =>
75+
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+
});
9284

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-
},
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,
10190
);
102-
},
103-
});
104-
};
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+
},
101+
);
102+
},
103+
});
105104
};

frontend/src/pages/Home/HomePage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ const HomePage: React.FC = () => {
9797
key={report.id}
9898
report={report}
9999
onClick={() => handleReportClick(report.id)}
100-
onToggleBookmark={() => toggleBookmark(report.id, report.bookmarked)}
100+
onToggleBookmark={() =>
101+
toggleBookmark.mutate({ reportId: report.id, isBookmarked: report.bookmarked })
102+
}
101103
showBookmarkButton={true}
102104
/>
103105
));

frontend/src/pages/Reports/ReportsListPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ const ReportsListPage: React.FC = () => {
226226
key={report.id}
227227
report={report}
228228
onClick={() => handleReportClick(report.id)}
229-
onToggleBookmark={() => toggleBookmark(report.id, report.bookmarked)}
229+
onToggleBookmark={() =>
230+
toggleBookmark.mutate({ reportId: report.id, isBookmarked: report.bookmarked })
231+
}
230232
showBookmarkButton
231233
/>
232234
));

0 commit comments

Comments
 (0)