feature: a very minimal patients feature implemented. it needs lots of improvments though
This commit is contained in:
43
frontend/src/lib/api/patients.ts
Normal file
43
frontend/src/lib/api/patients.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { apiClient } from './client';
|
||||
import {
|
||||
CreatePatientInput,
|
||||
CreateTreatmentHistoryInput,
|
||||
Patient,
|
||||
PatientsListResponse,
|
||||
TreatmentHistoryItem,
|
||||
} from '@/types/patient';
|
||||
|
||||
export const patientsApi = {
|
||||
list: async (params?: { q?: string; page?: number; limit?: number }): Promise<PatientsListResponse> => {
|
||||
const response = await apiClient.get('/patients', { params });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
create: async (data: CreatePatientInput): Promise<{ success: boolean; data: Patient }> => {
|
||||
const response = await apiClient.post('/patients', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getOne: async (id: string): Promise<{ success: boolean; data: Patient }> => {
|
||||
const response = await apiClient.get(`/patients/${id}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
listTreatments: async (
|
||||
patientId: string,
|
||||
limit = 20,
|
||||
): Promise<{ success: boolean; data: TreatmentHistoryItem[] }> => {
|
||||
const response = await apiClient.get(`/patients/${patientId}/treatments`, {
|
||||
params: { limit },
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
addTreatment: async (
|
||||
patientId: string,
|
||||
data: CreateTreatmentHistoryInput,
|
||||
): Promise<{ success: boolean; data: TreatmentHistoryItem }> => {
|
||||
const response = await apiClient.post(`/patients/${patientId}/treatments`, data);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
@@ -77,11 +77,18 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const storedOrgId = localStorage.getItem('currentOrganizationId');
|
||||
if (storedOrgId && orgs.length > 0) {
|
||||
const org = orgs.find(o => o.id === storedOrgId);
|
||||
if (org) setCurrentOrganization(org);
|
||||
else setCurrentOrganization(null);
|
||||
if (org) {
|
||||
setCurrentOrganization(org);
|
||||
// Ensure cookie token carries organizationId for org-scoped APIs.
|
||||
await authApi.selectOrganization(org.id);
|
||||
} else {
|
||||
setCurrentOrganization(null);
|
||||
}
|
||||
} else if (orgs.length === 1 && userData) {
|
||||
setCurrentOrganization(orgs[0]);
|
||||
localStorage.setItem('currentOrganizationId', orgs[0].id);
|
||||
// Keep JWT in sync with selected org even for single-org users.
|
||||
await authApi.selectOrganization(orgs[0].id);
|
||||
} else {
|
||||
setCurrentOrganization(null);
|
||||
}
|
||||
@@ -125,6 +132,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
if (orgs.length === 1) {
|
||||
const org = orgs[0];
|
||||
await authApi.selectOrganization(org.id);
|
||||
setCurrentOrganization(org);
|
||||
localStorage.setItem('currentOrganizationId', org.id);
|
||||
router.push('/today');
|
||||
@@ -156,6 +164,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
if (orgs.length === 1) {
|
||||
const org = orgs[0];
|
||||
await authApi.selectOrganization(org.id);
|
||||
setCurrentOrganization(org);
|
||||
localStorage.setItem('currentOrganizationId', org.id);
|
||||
router.push('/today');
|
||||
|
||||
Reference in New Issue
Block a user