Skip to content

Commit 1683607

Browse files
committed
Refactor bookmark handling in HomePage to use custom hook for improved readability and maintainability
1 parent 98bbb59 commit 1683607

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

frontend/src/pages/Home/HomePage.tsx

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
import { useTranslation } from 'react-i18next';
1313
import { useHistory } from 'react-router-dom';
1414
import { useState } from 'react';
15-
import { useGetLatestReports, useMarkReportAsRead } from 'common/hooks/useReports';
15+
import {
16+
useGetLatestReports,
17+
useMarkReportAsRead,
18+
useToggleReportBookmark,
19+
} from 'common/hooks/useReports';
1620
import { 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';
2021
import Avatar from 'common/components/Icon/Avatar';
2122
import ReportItem from './components/ReportItem/ReportItem';
2223
import NoReportsMessage from './components/NoReportsMessage/NoReportsMessage';
@@ -30,7 +31,7 @@ import './HomePage.scss';
3031
const HomePage: React.FC = () => {
3132
const { t } = useTranslation('home');
3233
const history = useHistory();
33-
const queryClient = useQueryClient();
34+
const toggleBookmark = useToggleReportBookmark();
3435
const { data: reports, isLoading, isError } = useGetLatestReports(3);
3536
const { mutate: markAsRead } = useMarkReportAsRead();
3637
const currentUser = useCurrentUser();
@@ -47,31 +48,6 @@ const HomePage: React.FC = () => {
4748
history.push(`/tabs/reports/${reportId}`);
4849
};
4950

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-
7551
const handleUpload = () => {
7652
history.push('/upload');
7753
};
@@ -121,7 +97,7 @@ const HomePage: React.FC = () => {
12197
key={report.id}
12298
report={report}
12399
onClick={() => handleReportClick(report.id)}
124-
onToggleBookmark={() => handleToggleBookmark(report.id, report.bookmarked)}
100+
onToggleBookmark={() => toggleBookmark(report.id, report.bookmarked)}
125101
showBookmarkButton={true}
126102
/>
127103
));

0 commit comments

Comments
 (0)