initial commit
This commit is contained in:
94
apps/Frontend/src/lib/api/documents.js
Executable file
94
apps/Frontend/src/lib/api/documents.js
Executable file
@@ -0,0 +1,94 @@
|
||||
import { apiRequest } from "../queryClient";
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL_BACKEND ?? "";
|
||||
// Upload a document for a patient
|
||||
export const uploadDocument = async (patientId, file) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('patientId', patientId.toString());
|
||||
const token = localStorage.getItem("token");
|
||||
const response = await fetch(`${API_BASE_URL}/api/patient-documents/upload`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
body: formData,
|
||||
// Don't set Content-Type header when using FormData
|
||||
});
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || `Upload failed: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
// Get all documents for a patient
|
||||
export const getPatientDocuments = async (patientId, limit, offset) => {
|
||||
const url = new URL(`${API_BASE_URL}/api/patient-documents/patient/${patientId}`);
|
||||
if (limit !== undefined) {
|
||||
url.searchParams.append('limit', limit.toString());
|
||||
}
|
||||
if (offset !== undefined) {
|
||||
url.searchParams.append('offset', offset.toString());
|
||||
}
|
||||
const token = localStorage.getItem("token");
|
||||
const headers = {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
};
|
||||
const response = await fetch(url.toString(), {
|
||||
headers,
|
||||
credentials: "include",
|
||||
});
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || `Failed to fetch documents: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
// View a document (inline display)
|
||||
export const viewDocument = (documentId) => {
|
||||
return `${API_BASE_URL}/api/patient-documents/${documentId}/view`;
|
||||
};
|
||||
// Download a document
|
||||
export const downloadDocument = (documentId) => {
|
||||
return `${API_BASE_URL}/api/patient-documents/${documentId}/download`;
|
||||
};
|
||||
// Delete a document
|
||||
export const deleteDocument = async (documentId) => {
|
||||
const response = await apiRequest("DELETE", `/api/patient-documents/${documentId}`);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || `Delete failed: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
// Scan document (placeholder for scanner integration)
|
||||
export const scanDocument = async (patientId) => {
|
||||
const response = await apiRequest("POST", "/api/patient-documents/scan", {
|
||||
patientId,
|
||||
});
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.error || `Scan failed: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
// Helper function to format file size
|
||||
export const formatFileSize = (bytes) => {
|
||||
if (bytes === 0)
|
||||
return '0 Bytes';
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
|
||||
};
|
||||
// Helper function to get file icon based on MIME type
|
||||
export const getFileIcon = (mimeType) => {
|
||||
if (mimeType.startsWith('image/'))
|
||||
return '🖼️';
|
||||
if (mimeType === 'application/pdf')
|
||||
return '📄';
|
||||
if (mimeType.includes('word') || mimeType.includes('document'))
|
||||
return '📝';
|
||||
if (mimeType.includes('text'))
|
||||
return '📄';
|
||||
return '📎';
|
||||
};
|
||||
Reference in New Issue
Block a user