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 '📎';
|
||||
};
|
||||
168
apps/Frontend/src/lib/api/documents.ts
Executable file
168
apps/Frontend/src/lib/api/documents.ts
Executable file
@@ -0,0 +1,168 @@
|
||||
import { apiRequest } from "../queryClient";
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL_BACKEND ?? "";
|
||||
|
||||
export interface PatientDocument {
|
||||
id: number;
|
||||
patientId: number;
|
||||
filename: string;
|
||||
originalName: string;
|
||||
mimeType: string;
|
||||
fileSize: number;
|
||||
filePath: string;
|
||||
uploadedAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface DocumentUploadResponse {
|
||||
success: boolean;
|
||||
document: PatientDocument;
|
||||
}
|
||||
|
||||
export interface DocumentsResponse {
|
||||
success: boolean;
|
||||
documents: PatientDocument[];
|
||||
total?: number;
|
||||
}
|
||||
|
||||
export interface ScanResponse {
|
||||
success: boolean;
|
||||
message: string;
|
||||
patientId: number;
|
||||
note?: string;
|
||||
}
|
||||
|
||||
// Upload a document for a patient
|
||||
export const uploadDocument = async (
|
||||
patientId: number,
|
||||
file: File,
|
||||
): Promise<DocumentUploadResponse> => {
|
||||
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: number,
|
||||
limit?: number,
|
||||
offset?: number,
|
||||
): Promise<DocumentsResponse> => {
|
||||
const urlPath = `/api/patient-documents/patient/${patientId}`;
|
||||
const url = API_BASE_URL
|
||||
? new URL(`${API_BASE_URL}${urlPath}`)
|
||||
: new URL(urlPath, window.location.origin);
|
||||
|
||||
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: Record<string, string> = {
|
||||
...(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: number): string => {
|
||||
if (API_BASE_URL) {
|
||||
return `${API_BASE_URL}/api/patient-documents/${documentId}/view`;
|
||||
}
|
||||
return `/api/patient-documents/${documentId}/view`;
|
||||
};
|
||||
|
||||
// Download a document
|
||||
export const downloadDocument = (documentId: number): string => {
|
||||
if (API_BASE_URL) {
|
||||
return `${API_BASE_URL}/api/patient-documents/${documentId}/download`;
|
||||
}
|
||||
return `/api/patient-documents/${documentId}/download`;
|
||||
};
|
||||
|
||||
// Delete a document
|
||||
export const deleteDocument = async (
|
||||
documentId: number,
|
||||
): Promise<{ success: boolean; message: string }> => {
|
||||
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: number,
|
||||
): Promise<ScanResponse> => {
|
||||
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: number): string => {
|
||||
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: string): string => {
|
||||
if (mimeType.startsWith("image/")) return "🖼️";
|
||||
if (mimeType === "application/pdf") return "📄";
|
||||
if (mimeType.includes("word") || mimeType.includes("document")) return "📝";
|
||||
if (mimeType.includes("text")) return "📄";
|
||||
return "📎";
|
||||
};
|
||||
37
apps/Frontend/src/lib/protected-route.tsx
Executable file
37
apps/Frontend/src/lib/protected-route.tsx
Executable file
@@ -0,0 +1,37 @@
|
||||
import AppLayout from "@/components/layout/app-layout";
|
||||
import LoadingScreen from "@/components/ui/LoadingScreen";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
import { Suspense } from "react";
|
||||
import { Redirect, Route } from "wouter";
|
||||
|
||||
type ComponentLike = React.ComponentType; // works for both lazy() and regular components
|
||||
|
||||
export function ProtectedRoute({
|
||||
path,
|
||||
component: Component,
|
||||
}: {
|
||||
path: string;
|
||||
component: ComponentLike;
|
||||
}) {
|
||||
const { user, isLoading } = useAuth();
|
||||
|
||||
return (
|
||||
<Route path={path}>
|
||||
{/* While auth is resolving: keep layout visible and show a small spinner in the content area */}
|
||||
{isLoading ? (
|
||||
<AppLayout>
|
||||
<LoadingScreen />
|
||||
</AppLayout>
|
||||
) : !user ? (
|
||||
<Redirect to="/auth" />
|
||||
) : (
|
||||
// Authenticated: render page inside layout. Lazy pages load with an in-layout spinner.
|
||||
<AppLayout>
|
||||
<Suspense fallback={<LoadingScreen />}>
|
||||
<Component />
|
||||
</Suspense>
|
||||
</AppLayout>
|
||||
)}
|
||||
</Route>
|
||||
);
|
||||
}
|
||||
149
apps/Frontend/src/lib/queryClient.ts
Executable file
149
apps/Frontend/src/lib/queryClient.ts
Executable file
@@ -0,0 +1,149 @@
|
||||
import { QueryClient, QueryFunction } from "@tanstack/react-query";
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL_BACKEND ?? "";
|
||||
|
||||
async function throwIfResNotOk(res: Response) {
|
||||
if (!res.ok) {
|
||||
if (res.status === 401) {
|
||||
localStorage.removeItem("token");
|
||||
if (!window.location.pathname.startsWith("/auth")) {
|
||||
window.location.href = "/auth";
|
||||
throw new Error(`${res.status}: Unauthorized`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to parse the response as JSON for a more meaningful error message
|
||||
let message = `${res.status}: ${res.statusText}`;
|
||||
|
||||
try {
|
||||
const errorBody = await res.clone().json();
|
||||
|
||||
if (errorBody?.error) {
|
||||
message = errorBody.error;
|
||||
} else if (errorBody?.message) {
|
||||
message = errorBody.message;
|
||||
} else if (errorBody?.detail) {
|
||||
message = errorBody.detail;
|
||||
}
|
||||
} catch {
|
||||
// fallback to reading raw text so no error is lost
|
||||
try {
|
||||
const text = await res.clone().text();
|
||||
if (text?.trim()) {
|
||||
message = text.trim();
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiRequest(
|
||||
method: string,
|
||||
url: string,
|
||||
data?: unknown | undefined
|
||||
): Promise<Response> {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const isFormData =
|
||||
typeof FormData !== "undefined" && data instanceof FormData;
|
||||
|
||||
const isFileLike =
|
||||
(typeof File !== "undefined" && data instanceof File) ||
|
||||
(typeof Blob !== "undefined" && data instanceof Blob);
|
||||
|
||||
const isArrayBufferLike =
|
||||
(typeof ArrayBuffer !== "undefined" && data instanceof ArrayBuffer) ||
|
||||
(typeof Uint8Array !== "undefined" && data instanceof Uint8Array) ||
|
||||
(data != null && (data as any)?.constructor?.name === "Buffer"); // Node Buffer
|
||||
|
||||
// Decide Content-Type header appropriately:
|
||||
const headers: Record<string, string> = {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
};
|
||||
|
||||
if (!isFormData) {
|
||||
if (isFileLike) {
|
||||
// File/Blob: use its own MIME type if present, otherwise fallback
|
||||
const mime = (data as File | Blob).type || "application/octet-stream";
|
||||
headers["Content-Type"] = mime;
|
||||
} else if (isArrayBufferLike) {
|
||||
// ArrayBuffer / Buffer / Uint8Array: use generic octet-stream
|
||||
headers["Content-Type"] = "application/octet-stream";
|
||||
} else {
|
||||
// Normal JSON body
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
}
|
||||
// If FormData, we must NOT set Content-Type (browser will set multipart boundary)
|
||||
|
||||
// Build final body
|
||||
const finalBody = isFormData
|
||||
? (data as FormData)
|
||||
: isFileLike
|
||||
? // File/Blob can be passed directly as BodyInit
|
||||
(data as BodyInit)
|
||||
: isArrayBufferLike
|
||||
? // ArrayBuffer / Uint8Array / Buffer -> convert to Uint8Array if needed
|
||||
(data as BodyInit)
|
||||
: data !== undefined
|
||||
? JSON.stringify(data)
|
||||
: undefined;
|
||||
|
||||
const res = await fetch(`${API_BASE_URL}${url}`, {
|
||||
method,
|
||||
headers,
|
||||
body: finalBody,
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
await throwIfResNotOk(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
type UnauthorizedBehavior = "returnNull" | "throw";
|
||||
|
||||
export const getQueryFn: <T>(options: {
|
||||
on401: UnauthorizedBehavior;
|
||||
}) => QueryFunction<T> =
|
||||
({ on401: unauthorizedBehavior }) =>
|
||||
async ({ queryKey }) => {
|
||||
const url = `${API_BASE_URL}${queryKey[0] as string}`;
|
||||
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (
|
||||
unauthorizedBehavior === "returnNull" &&
|
||||
(res.status === 401 || res.status === 403)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
await throwIfResNotOk(res);
|
||||
return await res.json();
|
||||
};
|
||||
|
||||
export const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
queryFn: getQueryFn({ on401: "throw" }),
|
||||
refetchInterval: false,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnMount: true,
|
||||
staleTime: 0,
|
||||
retry: false,
|
||||
},
|
||||
mutations: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
6
apps/Frontend/src/lib/utils.ts
Executable file
6
apps/Frontend/src/lib/utils.ts
Executable file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user