-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapiReport.ts
More file actions
73 lines (64 loc) · 2.26 KB
/
Copy pathapiReport.ts
File metadata and controls
73 lines (64 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import axios from 'axios';
import {IReportApiResponse} from '@/types';
const API_BASE_URL = `/api/report`;
const client = axios.create({
baseURL: API_BASE_URL,
});
/**
* Fetches a report based on the provided form data.
* @param formData - The FormData object containing the files to be processed.
* @return IReportApiResponse - A promise that resolves to the report data.
*/
const getReport = async (formData: FormData, type: string): Promise<IReportApiResponse> => {
try {
const response = await client.post(
`/process/${type}`,
formData
);
return response.data;
} catch (error) {
console.error('Error fetching report:', error);
throw error;
}
}
const getReportPdf = async (reportData: Partial<IReportApiResponse>) => {
try {
const response = await client.post(
'/report-pdf',
{ report_data: reportData }, // body data
{ responseType: 'blob' } // config
);
const fileURL = window.URL.createObjectURL(new Blob([response.data]));
const fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.setAttribute('download', 'report.pdf'); // change filename as needed
document.body.appendChild(fileLink);
fileLink.click();
document.body.removeChild(fileLink);
} catch (error) {
console.error('Error getting report PDF:', error);
throw error;
}
}
const downloadUpdatedJson = (jsonContent: Record<string, unknown>, filename: string = 'updated_asl_parameters.json') => {
try {
const jsonString = JSON.stringify(jsonContent, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const fileURL = window.URL.createObjectURL(blob);
const fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.setAttribute('download', filename);
document.body.appendChild(fileLink);
fileLink.click();
document.body.removeChild(fileLink);
window.URL.revokeObjectURL(fileURL);
} catch (error) {
console.error('Error downloading updated JSON:', error);
throw error;
}
}
export {
getReport,
getReportPdf,
downloadUpdatedJson,
}