4b697374f6
* feat: obsolete medication archiving, start date, and UI improvements - Add soft-archive (obsolete) for medications with dedicated section and toggle - Add medication start date field with date picker and validation - Add obsolete/reactivate API endpoints with proper auth - Filter obsolete meds from schedule, coverage, planner, and notifications - Improve UserFilterModal with intake schedules, stock badges, and click-to-open - Improve dashboard taken-by badges with per-intake bell icons - Add Escape key support to ConfirmModal and MobileEditModal - Fix Lightbox close button positioning near image - Add read-only mode support for MobileEditModal - DB migrations: 0008 (is_obsolete, obsolete_at), 0009 (medication_start_date) - All user-facing text uses i18n keys (en + de) * test: fix tests for obsolete medications and UI changes - Backend: add is_obsolete, obsolete_at, medication_start_date columns to test schemas - Backend: add test medication inserts in planner tests for active-med filtering - Frontend: update useMedications URL to include includeObsolete param - Frontend: fix MobileEditModal selectors and validation assertions - Frontend: add onClearUser prop to UserFilterModal test renders - Frontend: fix MedicationsPage and DashboardPage test assertions
85 lines
2.2 KiB
TypeScript
85 lines
2.2 KiB
TypeScript
import { useCallback, useState } from "react";
|
|
import type { Medication } from "../types";
|
|
|
|
export interface UseMedicationsReturn {
|
|
meds: Medication[];
|
|
setMeds: React.Dispatch<React.SetStateAction<Medication[]>>;
|
|
loading: boolean;
|
|
saving: boolean;
|
|
setSaving: React.Dispatch<React.SetStateAction<boolean>>;
|
|
uploadingImage: boolean;
|
|
loadMeds: () => void;
|
|
deleteMed: (id: number, editingId: number | null, resetForm: () => void) => Promise<void>;
|
|
uploadMedImage: (medId: number, file: File) => Promise<void>;
|
|
deleteMedImage: (medId: number) => Promise<void>;
|
|
}
|
|
|
|
export function useMedications(): UseMedicationsReturn {
|
|
const [meds, setMeds] = useState<Medication[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [saving, setSaving] = useState(false);
|
|
const [uploadingImage, setUploadingImage] = useState(false);
|
|
|
|
const loadMeds = useCallback(() => {
|
|
setLoading(true);
|
|
fetch("/api/medications?includeObsolete=true", { credentials: "include" })
|
|
.then((res) => res.json())
|
|
.then((data) => setMeds(Array.isArray(data) ? data : []))
|
|
.catch(() => setMeds([]))
|
|
.finally(() => setLoading(false));
|
|
}, []);
|
|
|
|
const deleteMed = useCallback(
|
|
async (id: number, editingId: number | null, resetForm: () => void) => {
|
|
await fetch(`/api/medications/${id}`, { method: "DELETE", credentials: "include" }).catch(() => null);
|
|
if (editingId === id) resetForm();
|
|
loadMeds();
|
|
},
|
|
[loadMeds]
|
|
);
|
|
|
|
const uploadMedImage = useCallback(
|
|
async (medId: number, file: File) => {
|
|
setUploadingImage(true);
|
|
const formData = new FormData();
|
|
formData.append("file", file);
|
|
|
|
try {
|
|
const res = await fetch(`/api/medications/${medId}/image`, {
|
|
method: "POST",
|
|
body: formData,
|
|
credentials: "include",
|
|
});
|
|
if (res.ok) {
|
|
loadMeds();
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
setUploadingImage(false);
|
|
},
|
|
[loadMeds]
|
|
);
|
|
|
|
const deleteMedImage = useCallback(
|
|
async (medId: number) => {
|
|
await fetch(`/api/medications/${medId}/image`, { method: "DELETE", credentials: "include" }).catch(() => null);
|
|
loadMeds();
|
|
},
|
|
[loadMeds]
|
|
);
|
|
|
|
return {
|
|
meds,
|
|
setMeds,
|
|
loading,
|
|
saving,
|
|
setSaving,
|
|
uploadingImage,
|
|
loadMeds,
|
|
deleteMed,
|
|
uploadMedImage,
|
|
deleteMedImage,
|
|
};
|
|
}
|