11import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
2- import { fetchAllReports , fetchLatestReports , markReportAsRead } from '../api/reportService' ;
2+ import {
3+ fetchAllReports ,
4+ fetchLatestReports ,
5+ markReportAsRead ,
6+ toggleReportBookmark ,
7+ } from '../api/reportService' ;
38import { MedicalReport } from '../models/medicalReport' ;
4-
5- // Query keys
6- const REPORTS_KEY = 'reports' ;
7- const LATEST_REPORTS_KEY = 'latestReports' ;
9+ import { QueryKey } from 'common/utils/constants' ;
810
911/**
1012 * Hook to fetch the latest reports.
@@ -13,7 +15,7 @@ const LATEST_REPORTS_KEY = 'latestReports';
1315 */
1416export const useGetLatestReports = ( limit = 3 ) => {
1517 return useQuery ( {
16- queryKey : [ LATEST_REPORTS_KEY , limit ] ,
18+ queryKey : [ QueryKey . LatestReports , limit ] ,
1719 queryFn : ( ) => fetchLatestReports ( limit ) ,
1820 refetchOnMount : true ,
1921 refetchOnWindowFocus : true ,
@@ -27,7 +29,7 @@ export const useGetLatestReports = (limit = 3) => {
2729 */
2830export const useGetAllReports = ( ) => {
2931 return useQuery ( {
30- queryKey : [ REPORTS_KEY ] ,
32+ queryKey : [ QueryKey . Reports ] ,
3133 queryFn : fetchAllReports ,
3234 } ) ;
3335} ;
@@ -43,15 +45,15 @@ export const useMarkReportAsRead = () => {
4345 mutationFn : ( reportId : string ) => markReportAsRead ( reportId ) ,
4446 onSuccess : ( updatedReport : MedicalReport ) => {
4547 // Update the reports cache
46- queryClient . setQueryData < MedicalReport [ ] > ( [ REPORTS_KEY ] , ( oldReports ) => {
48+ queryClient . setQueryData < MedicalReport [ ] > ( [ QueryKey . Reports ] , ( oldReports ) => {
4749 if ( ! oldReports ) return undefined ;
4850 return oldReports . map ( ( report ) =>
4951 report . id === updatedReport . id ? updatedReport : report ,
5052 ) ;
5153 } ) ;
5254
5355 // Update the latest reports cache
54- queryClient . setQueryData < MedicalReport [ ] > ( [ LATEST_REPORTS_KEY ] , ( oldReports ) => {
56+ queryClient . setQueryData < MedicalReport [ ] > ( [ QueryKey . LatestReports ] , ( oldReports ) => {
5557 if ( ! oldReports ) return undefined ;
5658 return oldReports . map ( ( report ) =>
5759 report . id === updatedReport . id ? updatedReport : report ,
@@ -60,3 +62,43 @@ export const useMarkReportAsRead = () => {
6062 } ,
6163 } ) ;
6264} ;
65+
66+ /**
67+ * Hook to toggle the bookmark status of a report.
68+ * @returns Mutation result for toggling the bookmark status
69+ */
70+ export const useToggleReportBookmark = ( ) => {
71+ const queryClient = useQueryClient ( ) ;
72+
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+ } ) ;
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+ } ) ;
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+ } ) ;
104+ } ;
0 commit comments