Skip to content

Commit e9e5bb9

Browse files
authored
Merge pull request #142 from ModusCreateOrg/ADE-215
[ADE-215] fix stale data in the app
2 parents df841c1 + b0a3365 commit e9e5bb9

File tree

8 files changed

+100
-75
lines changed

8 files changed

+100
-75
lines changed

backend/src/services/perplexity.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import axios from 'axios';
44
import { AwsSecretsService } from './aws-secrets.service';
5-
import { LabValue } from 'src/document-processor/services/aws-bedrock.service';
5+
import { LabValue } from '../document-processor/services/aws-bedrock.service';
66

77
export interface PerplexityMessage {
88
role: 'system' | 'user' | 'assistant';

frontend/src/common/hooks/useChat.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
22
import { bedrockService } from '../services/ai/bedrock.service';
3-
4-
export const CHAT_QUERY_KEY = 'chat';
3+
import { QueryKey } from 'common/utils/constants';
54

65
export function useChat(sessionId?: string) {
76
const queryClient = useQueryClient();
87

98
// Query for getting chat session
109
const { data: session } = useQuery({
11-
queryKey: [CHAT_QUERY_KEY, sessionId],
10+
queryKey: [QueryKey.Chat, sessionId],
1211
queryFn: () => (sessionId ? bedrockService.getChatSession(sessionId) : undefined),
1312
enabled: !!sessionId,
1413
});
1514

1615
// Query for getting all sessions
1716
const { data: sessions } = useQuery({
18-
queryKey: [CHAT_QUERY_KEY, 'sessions'],
17+
queryKey: [QueryKey.Chat, 'sessions'],
1918
queryFn: () => bedrockService.getAllSessions(),
2019
});
2120

2221
// Mutation for creating a new session
2322
const createSession = useMutation({
2423
mutationFn: () => bedrockService.createChatSession(),
2524
onSuccess: (newSessionId) => {
26-
queryClient.invalidateQueries({ queryKey: [CHAT_QUERY_KEY, 'sessions'] });
25+
queryClient.invalidateQueries({ queryKey: [QueryKey.Chat, 'sessions'] });
2726
return newSessionId;
2827
},
2928
});
@@ -35,8 +34,8 @@ export function useChat(sessionId?: string) {
3534
return bedrockService.sendMessage(sessionId, message);
3635
},
3736
onSuccess: () => {
38-
queryClient.invalidateQueries({ queryKey: [CHAT_QUERY_KEY, sessionId] });
39-
queryClient.invalidateQueries({ queryKey: [CHAT_QUERY_KEY, 'sessions'] });
37+
queryClient.invalidateQueries({ queryKey: [QueryKey.Chat, sessionId] });
38+
queryClient.invalidateQueries({ queryKey: [QueryKey.Chat, 'sessions'] });
4039
},
4140
});
4241

frontend/src/common/hooks/useReports.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { 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';
38
import { 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
*/
1416
export 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
*/
2830
export 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+
};

frontend/src/common/utils/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export enum QueryKey {
99
UserProfile = 'UserProfile',
1010
Users = 'Users',
1111
UserTokens = 'UserTokens',
12+
Chat = 'Chat',
13+
Reports = 'Reports',
14+
LatestReports = 'LatestReports',
15+
ReportDetail = 'ReportDetail',
1216
}
1317

1418
/**

frontend/src/pages/Home/HomePage.tsx

Lines changed: 9 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,9 @@ 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={() =>
101+
toggleBookmark.mutate({ reportId: report.id, isBookmarked: report.bookmarked })
102+
}
125103
showBookmarkButton={true}
126104
/>
127105
));

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: 14 additions & 3 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';
@@ -16,6 +16,7 @@ import InfoCard from './components/InfoCard';
1616
import ActionButtons from './components/ActionButtons';
1717
import AiAnalysisTab from './components/AiAnalysisTab';
1818
import UploadModal from 'common/components/Upload/UploadModal';
19+
import { QueryKey } from 'common/utils/constants';
1920

2021
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
2122

@@ -38,6 +39,7 @@ const ReportDetailPage: React.FC = () => {
3839
const { t } = useTranslation();
3940
const { createToast } = useToasts();
4041
const [isUploadModalOpen, setIsUploadModalOpen] = useState(false);
42+
const queryClient = useQueryClient();
4143

4244
const handleUploadComplete = () => {
4345
setIsUploadModalOpen(false);
@@ -46,7 +48,7 @@ const ReportDetailPage: React.FC = () => {
4648

4749
// Fetch report data using react-query
4850
const { data, isLoading, error } = useQuery<MedicalReport>({
49-
queryKey: ['report', reportId],
51+
queryKey: [QueryKey.ReportDetail, reportId],
5052
queryFn: () => fetchReportById(reportId!),
5153
enabled: !!reportId,
5254
});
@@ -107,6 +109,10 @@ const ReportDetailPage: React.FC = () => {
107109
duration: 2000,
108110
});
109111

112+
queryClient.invalidateQueries({ queryKey: [QueryKey.Reports] });
113+
queryClient.invalidateQueries({ queryKey: [QueryKey.LatestReports] });
114+
queryClient.invalidateQueries({ queryKey: [QueryKey.ReportDetail, reportId] });
115+
110116
// Navigate back
111117
history.push('/tabs/home');
112118
} catch (error) {
@@ -129,6 +135,11 @@ const ReportDetailPage: React.FC = () => {
129135
setIsProcessing(true);
130136
await axios.delete(`${API_URL}/api/reports/${reportId}`, await getAuthConfig());
131137
setIsProcessing(false);
138+
139+
queryClient.invalidateQueries({ queryKey: [QueryKey.Reports] });
140+
queryClient.invalidateQueries({ queryKey: [QueryKey.LatestReports] });
141+
queryClient.invalidateQueries({ queryKey: [QueryKey.ReportDetail, reportId] });
142+
132143
setIsUploadModalOpen(true);
133144
} catch (error) {
134145
setIsProcessing(false);
@@ -168,7 +179,7 @@ const ReportDetailPage: React.FC = () => {
168179

169180
<UploadModal
170181
isOpen={isUploadModalOpen}
171-
onClose={() => setIsUploadModalOpen(false)}
182+
onClose={handleUploadComplete}
172183
onUploadComplete={handleUploadComplete}
173184
/>
174185
</IonContent>

0 commit comments

Comments
 (0)