Skip to content

Commit ab78e8f

Browse files
committed
feat: Enhance report processing with loading state and mock data for testing
1 parent 69cf506 commit ab78e8f

File tree

4 files changed

+458
-182
lines changed

4 files changed

+458
-182
lines changed

frontend/src/common/api/reportService.ts

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import axios, { AxiosProgressEvent } from 'axios';
2-
import { MedicalReport } from '../models/medicalReport';
2+
import { MedicalReport, ReportCategory, ReportStatus } from '../models/medicalReport';
33
import { fetchAuthSession } from '@aws-amplify/auth';
44
// Get the API URL from environment variables
55
const API_URL = import.meta.env.VITE_BASE_URL_API || '';
66

7+
// Mock data for testing and development
8+
const mockReports: MedicalReport[] = [
9+
{
10+
id: '1',
11+
userId: 'user1',
12+
title: 'Blood Test Report',
13+
category: ReportCategory.GENERAL,
14+
bookmarked: false,
15+
isProcessed: true,
16+
labValues: [],
17+
summary: 'Blood test results within normal range',
18+
status: ReportStatus.UNREAD,
19+
filePath: '/reports/blood-test.pdf',
20+
createdAt: '2023-04-15T12:30:00Z',
21+
updatedAt: '2023-04-15T12:30:00Z',
22+
},
23+
{
24+
id: '2',
25+
userId: 'user1',
26+
title: 'Heart Checkup',
27+
category: ReportCategory.HEART,
28+
bookmarked: true,
29+
isProcessed: true,
30+
labValues: [],
31+
summary: 'Heart functioning normally',
32+
status: ReportStatus.READ,
33+
filePath: '/reports/heart-checkup.pdf',
34+
createdAt: '2023-04-10T10:15:00Z',
35+
updatedAt: '2023-04-10T10:15:00Z',
36+
},
37+
];
38+
739
/**
840
* Interface for upload progress callback
941
*/
@@ -14,16 +46,22 @@ export interface UploadProgressCallback {
1446
/**
1547
* Creates an authenticated request config with bearer token
1648
*/
17-
export const getAuthConfig = async (signal?: AbortSignal): Promise<{ headers: { Accept: string, 'Content-Type': string, Authorization: string }, signal?: AbortSignal, onUploadProgress?: (progressEvent: AxiosProgressEvent) => void }> => {
49+
export const getAuthConfig = async (
50+
signal?: AbortSignal,
51+
): Promise<{
52+
headers: { Accept: string; 'Content-Type': string; Authorization: string };
53+
signal?: AbortSignal;
54+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
55+
}> => {
1856
const session = await fetchAuthSession();
1957
const idToken = session.tokens?.idToken?.toString() || '';
2058
return {
21-
headers: {
22-
Accept: 'application/json',
23-
'Content-Type': 'application/json',
24-
Authorization: idToken ? `Bearer ${idToken}` : ''
25-
},
26-
signal
59+
headers: {
60+
Accept: 'application/json',
61+
'Content-Type': 'application/json',
62+
Authorization: idToken ? `Bearer ${idToken}` : '',
63+
},
64+
signal,
2765
};
2866
};
2967

@@ -47,7 +85,7 @@ export class ReportError extends Error {
4785
export const uploadReport = async (
4886
file: File,
4987
onProgress?: UploadProgressCallback,
50-
signal?: AbortSignal
88+
signal?: AbortSignal,
5189
): Promise<MedicalReport> => {
5290
try {
5391
// Import s3StorageService dynamically to avoid circular dependency
@@ -58,7 +96,7 @@ export const uploadReport = async (
5896
file,
5997
'reports',
6098
onProgress as (progress: number) => void,
61-
signal
99+
signal,
62100
);
63101

64102
// Then create the report record with the S3 key
@@ -70,7 +108,7 @@ export const uploadReport = async (
70108
{
71109
filePath: s3Key,
72110
},
73-
config
111+
config,
74112
);
75113

76114
return response.data;
@@ -79,7 +117,7 @@ export const uploadReport = async (
79117
if (signal?.aborted) {
80118
throw new DOMException('The operation was aborted', 'AbortError');
81119
}
82-
120+
83121
if (axios.isAxiosError(error)) {
84122
console.error('API Error Details:', error.response?.data, error.response?.headers);
85123
throw new ReportError(`Failed to upload report: ${error.message}`);
@@ -95,7 +133,10 @@ export const uploadReport = async (
95133
*/
96134
export const fetchLatestReports = async (limit = 3): Promise<MedicalReport[]> => {
97135
try {
98-
const response = await axios.get(`${API_URL}/api/reports/latest?limit=${limit}`, await getAuthConfig());
136+
const response = await axios.get(
137+
`${API_URL}/api/reports/latest?limit=${limit}`,
138+
await getAuthConfig(),
139+
);
99140
console.log('response', response.data);
100141
console.log('API_URL', API_URL);
101142
return response.data;
@@ -113,7 +154,7 @@ export const fetchLatestReports = async (limit = 3): Promise<MedicalReport[]> =>
113154
*/
114155
export const fetchAllReports = async (): Promise<MedicalReport[]> => {
115156
try {
116-
const response = await axios.get(`${API_URL}/api/reports`, await getAuthConfig() );
157+
const response = await axios.get(`${API_URL}/api/reports`, await getAuthConfig());
117158
return response.data;
118159
} catch (error) {
119160
if (axios.isAxiosError(error)) {
@@ -131,7 +172,7 @@ export const fetchAllReports = async (): Promise<MedicalReport[]> => {
131172
export const markReportAsRead = async (reportId: string): Promise<MedicalReport> => {
132173
try {
133174
const response = await axios.patch(`${API_URL}/api/reports/${reportId}`, {
134-
status: 'READ'
175+
status: 'READ',
135176
});
136177

137178
return response.data;
@@ -149,17 +190,24 @@ export const markReportAsRead = async (reportId: string): Promise<MedicalReport>
149190
* @param isBookmarked - Boolean indicating if the report should be bookmarked or not
150191
* @returns Promise with the updated report
151192
*/
152-
export const toggleReportBookmark = async (reportId: string, isBookmarked: boolean): Promise<MedicalReport> => {
193+
export const toggleReportBookmark = async (
194+
reportId: string,
195+
isBookmarked: boolean,
196+
): Promise<MedicalReport> => {
153197
try {
154-
await axios.patch(`${API_URL}/api/reports/${reportId}/bookmark`, {
155-
bookmarked: isBookmarked
156-
}, await getAuthConfig());
198+
await axios.patch(
199+
`${API_URL}/api/reports/${reportId}/bookmark`,
200+
{
201+
bookmarked: isBookmarked,
202+
},
203+
await getAuthConfig(),
204+
);
157205

158206
// In a real implementation, this would return the response from the API
159207
// return response.data;
160208

161209
// For now, we'll mock the response
162-
const report = mockReports.find(r => r.id === reportId);
210+
const report = mockReports.find((r) => r.id === reportId);
163211

164212
if (!report) {
165213
throw new Error(`Report with ID ${reportId} not found`);

0 commit comments

Comments
 (0)