2026-05-07 03:40:29 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
2026-06-20 14:51:43 +03:30
|
|
|
import { useTranslations } from 'next-intl';
|
2026-05-07 03:46:18 +03:30
|
|
|
import { AppointmentsStrip } from '@/components/ui/treatment/AppointmentsStrip';
|
|
|
|
|
import { FdiToothChart } from '@/components/ui/treatment/FdiToothChart';
|
2026-06-28 17:14:02 +03:30
|
|
|
import { LabCasesDispatchPanel } from '@/components/ui/treatment/LabCasesDispatchPanel';
|
2026-05-07 03:46:18 +03:30
|
|
|
import { PastTreatmentsPanel } from '@/components/ui/treatment/PastTreatmentsPanel';
|
2026-06-28 17:14:02 +03:30
|
|
|
import { TreatmentDetailsEditor } from '@/components/ui/treatment/TreatmentDetailsEditor';
|
2026-05-19 23:43:43 +03:30
|
|
|
import { TreatmentPreviewCard } from '@/components/ui/treatment/TreatmentPreviewCard';
|
|
|
|
|
import {
|
|
|
|
|
TreatmentPreviewDialog,
|
|
|
|
|
type TreatmentPreviewMode,
|
|
|
|
|
} from '@/components/ui/treatment/TreatmentPreviewDialog';
|
|
|
|
|
import { ToastStack } from '@/components/ui/shared/Toast';
|
2026-06-28 17:14:02 +03:30
|
|
|
import { treatmentTypeLabelKey } from '@/components/ui/treatment/treatmentTypeDisplay';
|
2026-05-07 03:40:29 +03:30
|
|
|
import {
|
2026-05-19 22:29:29 +03:30
|
|
|
addCalendarDays,
|
|
|
|
|
compareLocalDayStart,
|
|
|
|
|
isSameLocalCalendarDay,
|
|
|
|
|
startOfLocalDay,
|
|
|
|
|
} from '@/components/appointments/appointmentTime';
|
|
|
|
|
import { appointmentsApi } from '@/lib/api/appointments';
|
2026-06-28 17:14:02 +03:30
|
|
|
import { treatmentCatalogApi } from '@/lib/api/treatment-catalog';
|
2026-05-19 22:29:29 +03:30
|
|
|
import { treatmentsApi } from '@/lib/api/treatments';
|
2026-05-18 14:08:07 +03:30
|
|
|
import { pickAutoAppointment } from '@/components/shared/treatmentSelection';
|
2026-05-19 22:29:29 +03:30
|
|
|
import { canEditTreatment, canViewTreatment } from '@/components/shared/permissions';
|
|
|
|
|
import { formatApiErrorMessage } from '@/components/shared/formatApiError';
|
2026-05-19 23:43:43 +03:30
|
|
|
import { useToast } from '@/lib/hooks/useToast';
|
2026-05-07 03:40:29 +03:30
|
|
|
import type { Organization } from '@/types/organization';
|
2026-05-19 22:29:29 +03:30
|
|
|
import type { AppointmentRecord } from '@/types/appointment';
|
2026-05-07 03:40:29 +03:30
|
|
|
import type {
|
|
|
|
|
FdiToothId,
|
2026-06-28 17:14:02 +03:30
|
|
|
LabCaseDraft,
|
2026-05-07 03:40:29 +03:30
|
|
|
LinkedOrganizationOption,
|
2026-06-28 17:14:02 +03:30
|
|
|
PastLabCase,
|
2026-05-07 03:40:29 +03:30
|
|
|
PastTreatment,
|
2026-05-19 22:29:29 +03:30
|
|
|
PastTreatmentCase,
|
2026-05-07 03:40:29 +03:30
|
|
|
TreatmentAppointment,
|
2026-06-28 17:14:02 +03:30
|
|
|
TreatmentDetailDraft,
|
2026-05-07 03:40:29 +03:30
|
|
|
} from '@/types/treatment';
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
function newDetail(): TreatmentDetailDraft {
|
2026-05-07 03:40:29 +03:30
|
|
|
return {
|
|
|
|
|
clientId:
|
|
|
|
|
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
|
|
|
|
? crypto.randomUUID()
|
2026-06-28 17:14:02 +03:30
|
|
|
: `detail-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
|
2026-05-07 14:20:50 +03:30
|
|
|
treatmentType: 'consultation',
|
2026-05-07 03:40:29 +03:30
|
|
|
teeth: [],
|
|
|
|
|
comment: '',
|
|
|
|
|
attachmentMetas: [],
|
|
|
|
|
sendToOrganizationIds: [],
|
|
|
|
|
sentAt: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
function newLabCaseDraft(): LabCaseDraft {
|
|
|
|
|
return {
|
|
|
|
|
clientId:
|
|
|
|
|
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
|
|
|
|
? crypto.randomUUID()
|
|
|
|
|
: `lab-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
|
|
|
|
|
destinationOrganizationId: null,
|
|
|
|
|
labComment: '',
|
|
|
|
|
detailClientIds: [],
|
|
|
|
|
sentAt: null,
|
|
|
|
|
sends: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
function mapAppointment(record: AppointmentRecord): TreatmentAppointment {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
patientId: record.patientId,
|
|
|
|
|
patientFirstName: record.patient.firstName,
|
|
|
|
|
patientLastName: record.patient.lastName,
|
|
|
|
|
providerUserId: record.providerUserId,
|
|
|
|
|
startAt: record.startAt,
|
|
|
|
|
endAt: record.endAt,
|
|
|
|
|
purpose: record.purpose,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
function mapDetailFromApi(d: PastTreatmentCase): TreatmentDetailDraft {
|
2026-05-19 22:29:29 +03:30
|
|
|
return {
|
2026-06-28 15:34:56 +03:30
|
|
|
clientId: d.clientId,
|
|
|
|
|
id: d.id,
|
|
|
|
|
treatmentType: d.treatmentType,
|
|
|
|
|
teeth: d.teeth,
|
|
|
|
|
comment: d.notes ?? '',
|
|
|
|
|
attachmentMetas: d.attachmentMetas ?? [],
|
|
|
|
|
labCaseId: d.labCaseId ?? null,
|
|
|
|
|
sendToOrganizationIds: d.destinationOrganizationId ? [d.destinationOrganizationId] : [],
|
|
|
|
|
sends: d.sends ?? [],
|
|
|
|
|
sentAt: d.sentAt ?? null,
|
2026-05-19 22:29:29 +03:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
function mapLabCaseDraftFromApi(lc: PastLabCase): LabCaseDraft {
|
|
|
|
|
return {
|
|
|
|
|
clientId: lc.clientId,
|
|
|
|
|
id: lc.id,
|
|
|
|
|
destinationOrganizationId: lc.destinationOrganizationId,
|
|
|
|
|
labComment: lc.labComment ?? '',
|
|
|
|
|
detailClientIds: lc.details.map((d) => d.clientId),
|
|
|
|
|
sentAt: lc.sentAt ?? null,
|
|
|
|
|
sends: lc.sends ?? [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function serializeDetails(details: TreatmentDetailDraft[]) {
|
2026-05-19 22:29:29 +03:30
|
|
|
return JSON.stringify(
|
2026-06-28 17:14:02 +03:30
|
|
|
details.map((d) => ({
|
|
|
|
|
clientId: d.clientId,
|
|
|
|
|
id: d.id,
|
|
|
|
|
treatmentType: d.treatmentType,
|
|
|
|
|
teeth: d.teeth,
|
|
|
|
|
comment: d.comment,
|
|
|
|
|
attachmentMetas: d.attachmentMetas,
|
2026-05-19 22:29:29 +03:30
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
function detailsToPreviewTreatment(
|
|
|
|
|
details: TreatmentDetailDraft[],
|
2026-05-19 23:43:43 +03:30
|
|
|
meta: { title: string; patientId: string; treatmentAt: string; status: string; id?: string },
|
|
|
|
|
): PastTreatment {
|
|
|
|
|
return {
|
|
|
|
|
id: meta.id ?? 'current-draft',
|
|
|
|
|
patientId: meta.patientId,
|
|
|
|
|
title: meta.title,
|
|
|
|
|
treatmentAt: meta.treatmentAt,
|
|
|
|
|
status: meta.status,
|
2026-06-28 17:14:02 +03:30
|
|
|
details: details.map((d, idx) => ({
|
|
|
|
|
id: d.id ?? d.clientId ?? `draft-${idx + 1}`,
|
|
|
|
|
clientId: d.clientId,
|
|
|
|
|
treatmentType: d.treatmentType,
|
|
|
|
|
teeth: d.teeth,
|
|
|
|
|
notes: d.comment || null,
|
|
|
|
|
attachmentMetas: d.attachmentMetas,
|
|
|
|
|
labCaseId: d.labCaseId ?? null,
|
|
|
|
|
destinationOrganizationId: d.sendToOrganizationIds[0] ?? null,
|
|
|
|
|
sends: d.sends ?? [],
|
|
|
|
|
sentAt: d.sentAt ?? null,
|
2026-05-19 23:43:43 +03:30
|
|
|
})),
|
2026-06-28 15:34:56 +03:30
|
|
|
labCases: [],
|
2026-05-19 23:43:43 +03:30
|
|
|
documents: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
interface TreatmentWorkspaceProps {
|
|
|
|
|
userId: string;
|
|
|
|
|
currentOrganization: Organization | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWorkspaceProps) {
|
2026-06-20 14:51:43 +03:30
|
|
|
const t = useTranslations('treatment');
|
2026-05-19 23:43:43 +03:30
|
|
|
const { showError, showSuccess, messages: toastMessages } = useToast();
|
2026-05-19 22:29:29 +03:30
|
|
|
const canView = canViewTreatment(currentOrganization);
|
2026-05-07 03:40:29 +03:30
|
|
|
const canEdit = canEditTreatment(currentOrganization);
|
|
|
|
|
|
|
|
|
|
const [stripHidden, setStripHidden] = useState(false);
|
2026-05-18 12:50:25 +03:30
|
|
|
const todayStart = useMemo(() => startOfLocalDay(new Date()), []);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
const [selectedDay, setSelectedDay] = useState(() => startOfLocalDay(new Date()));
|
|
|
|
|
const [appointments, setAppointments] = useState<TreatmentAppointment[]>([]);
|
|
|
|
|
const [apptsLoading, setApptsLoading] = useState(false);
|
|
|
|
|
const [selectionLocked, setSelectionLocked] = useState(false);
|
|
|
|
|
const [selectedAppointmentId, setSelectedAppointmentId] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const [history, setHistory] = useState<PastTreatment[]>([]);
|
|
|
|
|
const [historyLoading, setHistoryLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const [orgs, setOrgs] = useState<LinkedOrganizationOption[]>([]);
|
2026-06-28 17:14:02 +03:30
|
|
|
const [labDependentCodes, setLabDependentCodes] = useState<Set<string>>(new Set());
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const [details, setDetails] = useState<TreatmentDetailDraft[]>(() => [newDetail()]);
|
|
|
|
|
const [labCaseDrafts, setLabCaseDrafts] = useState<LabCaseDraft[]>([]);
|
|
|
|
|
const [activeDetailId, setActiveDetailId] = useState<string>(() => details[0].clientId);
|
|
|
|
|
const [activeLabCaseId, setActiveLabCaseId] = useState<string | null>(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
const selectionLockedRef = useRef(selectionLocked);
|
|
|
|
|
selectionLockedRef.current = selectionLocked;
|
|
|
|
|
|
|
|
|
|
const [saveBusy, setSaveBusy] = useState(false);
|
2026-06-28 17:14:02 +03:30
|
|
|
const [saveLabBusy, setSaveLabBusy] = useState(false);
|
2026-05-07 03:40:29 +03:30
|
|
|
const [sendBusyId, setSendBusyId] = useState<string | null>(null);
|
2026-06-28 17:14:02 +03:30
|
|
|
const [uploadBusyDetailId, setUploadBusyDetailId] = useState<string | null>(null);
|
2026-05-07 14:20:50 +03:30
|
|
|
const [organizationSearch, setOrganizationSearch] = useState('');
|
|
|
|
|
const [recentOrganizationIds, setRecentOrganizationIds] = useState<string[]>([]);
|
2026-05-19 23:43:43 +03:30
|
|
|
|
|
|
|
|
const [previewOpen, setPreviewOpen] = useState(false);
|
|
|
|
|
const [previewTreatment, setPreviewTreatment] = useState<PastTreatment | null>(null);
|
|
|
|
|
const [previewMode, setPreviewMode] = useState<TreatmentPreviewMode>('readonly');
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const isDetailLocked = useCallback(
|
|
|
|
|
(detail: TreatmentDetailDraft) =>
|
|
|
|
|
labCaseDrafts.some((lc) => lc.sentAt && lc.detailClientIds.includes(detail.clientId)),
|
|
|
|
|
[labCaseDrafts],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const isDirty = useMemo(() => {
|
|
|
|
|
if (savedSnapshot === null) {
|
2026-06-28 17:14:02 +03:30
|
|
|
return details.length !== 1 || details[0].comment !== '' || details[0].teeth.length > 0;
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
2026-06-28 17:14:02 +03:30
|
|
|
return serializeDetails(details) !== savedSnapshot;
|
|
|
|
|
}, [details, savedSnapshot]);
|
2026-05-19 22:29:29 +03:30
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
const selectedAppointment = useMemo(
|
|
|
|
|
() => appointments.find((a) => a.id === selectedAppointmentId) ?? null,
|
|
|
|
|
[appointments, selectedAppointmentId],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-18 12:50:25 +03:30
|
|
|
const isViewingPastDay = useMemo(
|
|
|
|
|
() => compareLocalDayStart(selectedDay, todayStart) < 0,
|
|
|
|
|
[selectedDay, todayStart],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const canEditTreatmentForDay = canEdit && Boolean(selectedAppointment) && !isViewingPastDay;
|
2026-05-18 12:50:25 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const activeDetail = useMemo(
|
|
|
|
|
() => details.find((d) => d.clientId === activeDetailId) ?? details[0],
|
|
|
|
|
[details, activeDetailId],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const selectedTeethSet = useMemo(() => new Set(activeDetail?.teeth ?? []), [activeDetail?.teeth]);
|
2026-05-19 22:29:29 +03:30
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
const currentDraftPreview = useMemo<PastTreatment | null>(() => {
|
|
|
|
|
if (!selectedAppointment) return null;
|
2026-06-28 17:14:02 +03:30
|
|
|
return detailsToPreviewTreatment(details, {
|
2026-06-20 14:51:43 +03:30
|
|
|
title: t('draftTitle', {
|
|
|
|
|
patientName: `${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`,
|
|
|
|
|
}),
|
2026-05-07 14:20:50 +03:30
|
|
|
patientId: selectedAppointment.patientId,
|
|
|
|
|
treatmentAt: new Date().toISOString(),
|
|
|
|
|
status: 'draft',
|
2026-05-19 23:43:43 +03:30
|
|
|
});
|
2026-06-28 17:14:02 +03:30
|
|
|
}, [details, selectedAppointment, t]);
|
2026-05-07 14:20:50 +03:30
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
setSelectionLocked(false);
|
|
|
|
|
}, [selectedDay]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setApptsLoading(true);
|
|
|
|
|
void (async () => {
|
|
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
const dayStart = startOfLocalDay(selectedDay);
|
|
|
|
|
const dayEnd = addCalendarDays(dayStart, 1);
|
|
|
|
|
const response = await appointmentsApi.list({
|
|
|
|
|
from: dayStart.toISOString(),
|
|
|
|
|
to: dayEnd.toISOString(),
|
|
|
|
|
});
|
2026-05-07 03:40:29 +03:30
|
|
|
if (cancelled) return;
|
2026-05-19 22:29:29 +03:30
|
|
|
const list = response.data
|
|
|
|
|
.filter((a) => a.providerUserId === userId)
|
|
|
|
|
.map(mapAppointment);
|
2026-05-07 03:40:29 +03:30
|
|
|
setAppointments(list);
|
|
|
|
|
if (!selectionLockedRef.current) {
|
|
|
|
|
setSelectedAppointmentId(pickAutoAppointment(list, selectedDay));
|
|
|
|
|
}
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorLoadAppointments')));
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setApptsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [userId, selectedDay, showError, t]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const today = startOfLocalDay(new Date());
|
|
|
|
|
if (!isSameLocalCalendarDay(selectedDay, today) || selectionLocked) return;
|
|
|
|
|
|
|
|
|
|
const id = window.setInterval(() => {
|
|
|
|
|
setSelectedAppointmentId((prev) => {
|
|
|
|
|
const next = pickAutoAppointment(appointments, selectedDay);
|
|
|
|
|
return next ?? prev;
|
|
|
|
|
});
|
|
|
|
|
}, 60_000);
|
|
|
|
|
|
|
|
|
|
return () => window.clearInterval(id);
|
|
|
|
|
}, [selectedDay, appointments, selectionLocked]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
void (async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
try {
|
2026-06-28 17:14:02 +03:30
|
|
|
const [orgsResponse, catalogResponse] = await Promise.all([
|
|
|
|
|
treatmentsApi.listLinkedOrganizations(),
|
|
|
|
|
treatmentCatalogApi.list(),
|
|
|
|
|
]);
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
setOrgs(orgsResponse.data);
|
|
|
|
|
setLabDependentCodes(
|
|
|
|
|
new Set(catalogResponse.data.filter((entry) => entry.labDependent).map((entry) => entry.code)),
|
|
|
|
|
);
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorLoadOrgs')));
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
|
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [showError, t]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedAppointment) {
|
|
|
|
|
setHistory([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setHistoryLoading(true);
|
|
|
|
|
void (async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
try {
|
|
|
|
|
const response = await treatmentsApi.listPatientHistory(selectedAppointment.patientId);
|
2026-05-19 23:43:43 +03:30
|
|
|
if (!cancelled) setHistory(response.data);
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorLoadHistory')));
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setHistoryLoading(false);
|
2026-05-07 03:40:29 +03:30
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [selectedAppointment?.patientId, showError, t]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-19 23:43:43 +03:30
|
|
|
const appointmentId = selectedAppointment?.id;
|
|
|
|
|
if (!appointmentId) return;
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
let cancelled = false;
|
|
|
|
|
void (async () => {
|
|
|
|
|
try {
|
2026-05-19 23:43:43 +03:30
|
|
|
const response = await treatmentsApi.getDraft(appointmentId);
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
2026-06-28 15:34:56 +03:30
|
|
|
if (response.data?.details?.length) {
|
|
|
|
|
const mapped = response.data.details.map(mapDetailFromApi);
|
2026-06-28 17:14:02 +03:30
|
|
|
setDetails(mapped);
|
|
|
|
|
setActiveDetailId((prev) => {
|
|
|
|
|
const stillExists = mapped.some((d) => d.clientId === prev);
|
2026-05-19 23:43:43 +03:30
|
|
|
return stillExists ? prev : mapped[0].clientId;
|
|
|
|
|
});
|
2026-06-28 17:14:02 +03:30
|
|
|
setSavedSnapshot(serializeDetails(mapped));
|
2026-05-19 23:43:43 +03:30
|
|
|
} else {
|
2026-06-28 17:14:02 +03:30
|
|
|
const first = newDetail();
|
|
|
|
|
setDetails([first]);
|
|
|
|
|
setActiveDetailId(first.clientId);
|
|
|
|
|
setSavedSnapshot(serializeDetails([first]));
|
2026-05-19 23:43:43 +03:30
|
|
|
}
|
2026-06-28 17:14:02 +03:30
|
|
|
|
|
|
|
|
const mappedLabCases = (response.data?.labCases ?? []).map(mapLabCaseDraftFromApi);
|
|
|
|
|
setLabCaseDrafts(mappedLabCases);
|
|
|
|
|
setActiveLabCaseId(mappedLabCases[0]?.clientId ?? null);
|
2026-05-19 23:43:43 +03:30
|
|
|
setOrganizationSearch('');
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorLoadDraft')));
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [selectedAppointment?.id, showError, t]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const confirmDiscardIfDirty = useCallback(() => {
|
|
|
|
|
if (!isDirty) return true;
|
2026-06-20 14:51:43 +03:30
|
|
|
return window.confirm(t('confirmDiscard'));
|
|
|
|
|
}, [isDirty, t]);
|
2026-05-19 22:29:29 +03:30
|
|
|
|
|
|
|
|
const onPickAppointment = useCallback(
|
|
|
|
|
(id: string) => {
|
|
|
|
|
if (!confirmDiscardIfDirty()) return;
|
|
|
|
|
setSelectionLocked(true);
|
|
|
|
|
setSelectedAppointmentId(id);
|
|
|
|
|
},
|
|
|
|
|
[confirmDiscardIfDirty],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onSelectDay = useCallback(
|
|
|
|
|
(day: Date) => {
|
|
|
|
|
if (!confirmDiscardIfDirty()) return;
|
|
|
|
|
setSelectedDay(day);
|
|
|
|
|
},
|
|
|
|
|
[confirmDiscardIfDirty],
|
|
|
|
|
);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const uploadForDetail = useCallback(
|
|
|
|
|
async (detailClientId: string, files: FileList | File[]) => {
|
2026-05-19 23:43:43 +03:30
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
|
|
|
|
const list = files instanceof FileList ? Array.from(files) : files;
|
|
|
|
|
if (!list.length) return;
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
setUploadBusyDetailId(detailClientId);
|
2026-05-19 22:29:29 +03:30
|
|
|
try {
|
|
|
|
|
const uploaded = await treatmentsApi.uploadCaseAttachments(
|
|
|
|
|
selectedAppointment.id,
|
2026-06-28 17:14:02 +03:30
|
|
|
detailClientId,
|
2026-05-19 23:43:43 +03:30
|
|
|
list,
|
2026-05-19 22:29:29 +03:30
|
|
|
);
|
2026-06-28 17:14:02 +03:30
|
|
|
setDetails((prev) =>
|
|
|
|
|
prev.map((d) =>
|
|
|
|
|
d.clientId === detailClientId
|
|
|
|
|
? { ...d, attachmentMetas: [...d.attachmentMetas, ...uploaded.data] }
|
|
|
|
|
: d,
|
2026-05-19 23:43:43 +03:30
|
|
|
),
|
|
|
|
|
);
|
2026-06-20 14:51:43 +03:30
|
|
|
showSuccess(t('successFilesUploaded', { count: uploaded.data.length }));
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorUpload')));
|
2026-05-19 22:29:29 +03:30
|
|
|
} finally {
|
2026-06-28 17:14:02 +03:30
|
|
|
setUploadBusyDetailId(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
},
|
2026-06-20 14:51:43 +03:30
|
|
|
[canEditTreatmentForDay, selectedAppointment, showSuccess, showError, t],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const persistDraft = useCallback(async () => {
|
2026-05-19 23:43:43 +03:30
|
|
|
if (!selectedAppointment) throw new Error('No appointment selected');
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const response = await treatmentsApi.saveDraft(selectedAppointment.id, {
|
2026-06-28 17:14:02 +03:30
|
|
|
details: details.map(({ clientId, id, treatmentType, teeth, comment, attachmentMetas }) => ({
|
2026-05-19 22:29:29 +03:30
|
|
|
clientId,
|
|
|
|
|
id,
|
|
|
|
|
treatmentType,
|
|
|
|
|
teeth,
|
|
|
|
|
comment,
|
|
|
|
|
attachmentIds: attachmentMetas.map((a) => a.id),
|
|
|
|
|
})),
|
|
|
|
|
});
|
2026-06-28 15:34:56 +03:30
|
|
|
const mapped = response.data.details.map(mapDetailFromApi);
|
2026-06-28 17:14:02 +03:30
|
|
|
setDetails(mapped);
|
|
|
|
|
setActiveDetailId((prev) => {
|
|
|
|
|
const stillExists = mapped.some((d) => d.clientId === prev);
|
2026-05-19 22:29:29 +03:30
|
|
|
return stillExists ? prev : mapped[0]?.clientId ?? prev;
|
|
|
|
|
});
|
2026-06-28 17:14:02 +03:30
|
|
|
setSavedSnapshot(serializeDetails(mapped));
|
2026-05-19 22:29:29 +03:30
|
|
|
return response.data;
|
2026-06-28 17:14:02 +03:30
|
|
|
}, [details, selectedAppointment]);
|
|
|
|
|
|
|
|
|
|
const persistLabCases = useCallback(
|
|
|
|
|
async (savedTreatment: PastTreatment) => {
|
|
|
|
|
if (!selectedAppointment) throw new Error('No appointment selected');
|
|
|
|
|
|
|
|
|
|
const detailIdByClientId = new Map(
|
|
|
|
|
savedTreatment.details.map((d) => [d.clientId, d.id]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const payload = labCaseDrafts.map((lc) => ({
|
|
|
|
|
clientId: lc.clientId,
|
|
|
|
|
id: lc.id,
|
|
|
|
|
destinationOrganizationId: lc.destinationOrganizationId ?? undefined,
|
|
|
|
|
labComment: lc.labComment.trim() || undefined,
|
|
|
|
|
treatmentDetailIds: lc.detailClientIds
|
|
|
|
|
.map((clientId) => detailIdByClientId.get(clientId))
|
|
|
|
|
.filter((id): id is string => Boolean(id)),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (payload.length === 0) {
|
|
|
|
|
return savedTreatment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await treatmentsApi.saveLabCases(selectedAppointment.id, {
|
|
|
|
|
labCases: payload,
|
|
|
|
|
});
|
|
|
|
|
const mapped = response.data.labCases.map(mapLabCaseDraftFromApi);
|
|
|
|
|
setLabCaseDrafts(mapped);
|
|
|
|
|
setActiveLabCaseId((prev) => {
|
|
|
|
|
if (prev && mapped.some((lc) => lc.clientId === prev)) return prev;
|
|
|
|
|
return mapped[0]?.clientId ?? null;
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
},
|
|
|
|
|
[labCaseDrafts, selectedAppointment],
|
|
|
|
|
);
|
2026-05-19 22:29:29 +03:30
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
const handleSaveAll = useCallback(async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
2026-05-07 03:40:29 +03:30
|
|
|
setSaveBusy(true);
|
|
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
await persistDraft();
|
2026-06-20 14:51:43 +03:30
|
|
|
showSuccess(t('successDraftSaved'));
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorSaveDraft')));
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
setSaveBusy(false);
|
|
|
|
|
}
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [canEditTreatmentForDay, selectedAppointment, persistDraft, showSuccess, showError, t]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const handleSaveLabCases = useCallback(async () => {
|
|
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
|
|
|
|
setSaveLabBusy(true);
|
|
|
|
|
try {
|
|
|
|
|
const saved = await persistDraft();
|
|
|
|
|
await persistLabCases(saved);
|
|
|
|
|
showSuccess(t('successLabShipmentsSaved'));
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
showError(formatApiErrorMessage(error, t('errorSaveLabShipments')));
|
|
|
|
|
} finally {
|
|
|
|
|
setSaveLabBusy(false);
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
canEditTreatmentForDay,
|
|
|
|
|
selectedAppointment,
|
|
|
|
|
persistDraft,
|
|
|
|
|
persistLabCases,
|
|
|
|
|
showSuccess,
|
|
|
|
|
showError,
|
|
|
|
|
t,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const handleSendLabCase = useCallback(
|
|
|
|
|
async (labCase: LabCaseDraft) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
2026-06-28 17:14:02 +03:30
|
|
|
if (!labCase.destinationOrganizationId) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(t('errorChooseOrg'));
|
2026-05-07 03:40:29 +03:30
|
|
|
return;
|
|
|
|
|
}
|
2026-06-28 17:14:02 +03:30
|
|
|
if (labCase.detailClientIds.length === 0) {
|
|
|
|
|
showError(t('errorLabCaseNeedsDetails'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSendBusyId(labCase.clientId);
|
2026-05-07 03:40:29 +03:30
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
const saved = await persistDraft();
|
2026-06-28 17:14:02 +03:30
|
|
|
const afterLabCases = await persistLabCases(saved);
|
2026-06-28 15:34:56 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const refreshedLabCase = afterLabCases.labCases.find(
|
|
|
|
|
(lc) => lc.clientId === labCase.clientId || lc.id === labCase.id,
|
2026-06-28 15:34:56 +03:30
|
|
|
);
|
2026-06-28 17:14:02 +03:30
|
|
|
if (!refreshedLabCase?.id) throw new Error(t('errorSendCase'));
|
2026-06-28 15:34:56 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const response = await treatmentsApi.sendLabCase(refreshedLabCase.id);
|
2026-06-28 15:34:56 +03:30
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
setLabCaseDrafts((prev) =>
|
|
|
|
|
prev.map((lc) =>
|
|
|
|
|
lc.clientId === labCase.clientId
|
2026-05-19 22:29:29 +03:30
|
|
|
? {
|
2026-06-28 17:14:02 +03:30
|
|
|
...lc,
|
|
|
|
|
id: response.data.id,
|
2026-05-19 22:29:29 +03:30
|
|
|
sentAt: response.data.sentAt,
|
2026-06-28 17:14:02 +03:30
|
|
|
destinationOrganizationId: response.data.destinationOrganizationId,
|
2026-05-19 23:43:43 +03:30
|
|
|
sends: response.data.sends,
|
2026-05-19 22:29:29 +03:30
|
|
|
}
|
2026-06-28 17:14:02 +03:30
|
|
|
: lc,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
setRecentOrganizationIds((prev) => {
|
2026-06-28 17:14:02 +03:30
|
|
|
const orgId = labCase.destinationOrganizationId!;
|
|
|
|
|
return [orgId, ...prev.filter((id) => id !== orgId)].slice(0, 10);
|
2026-05-07 14:20:50 +03:30
|
|
|
});
|
2026-06-20 14:51:43 +03:30
|
|
|
showSuccess(t('successCaseSent'));
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
2026-06-20 14:51:43 +03:30
|
|
|
showError(formatApiErrorMessage(error, t('errorSendCase')));
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
setSendBusyId(null);
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-06-28 17:14:02 +03:30
|
|
|
[
|
|
|
|
|
canEditTreatmentForDay,
|
|
|
|
|
selectedAppointment,
|
|
|
|
|
persistDraft,
|
|
|
|
|
persistLabCases,
|
|
|
|
|
showSuccess,
|
|
|
|
|
showError,
|
|
|
|
|
t,
|
|
|
|
|
],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
const openPreview = useCallback((treatment: PastTreatment, mode: TreatmentPreviewMode) => {
|
|
|
|
|
setPreviewTreatment(treatment);
|
|
|
|
|
setPreviewMode(mode);
|
|
|
|
|
setPreviewOpen(true);
|
|
|
|
|
}, []);
|
2026-05-19 23:43:43 +03:30
|
|
|
|
|
|
|
|
const openCurrentDraftPreview = useCallback(() => {
|
|
|
|
|
if (!currentDraftPreview) return;
|
|
|
|
|
openPreview(currentDraftPreview, canEditTreatmentForDay ? 'editable' : 'readonly');
|
|
|
|
|
}, [currentDraftPreview, canEditTreatmentForDay, openPreview]);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canView) {
|
2026-05-07 03:40:29 +03:30
|
|
|
return (
|
|
|
|
|
<div className="surface-card p-6 max-w-xl">
|
2026-06-20 14:51:43 +03:30
|
|
|
<h2 className="text-lg font-semibold text-text-primary">{t('noPermissionTitle')}</h2>
|
2026-06-28 17:14:02 +03:30
|
|
|
<p className="text-sm text-text-secondary mt-2">{t('noPermissionBody')}</p>
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-05-19 23:43:43 +03:30
|
|
|
<div className="space-y-4">
|
2026-05-07 03:40:29 +03:30
|
|
|
<header className="space-y-1">
|
2026-06-20 14:51:43 +03:30
|
|
|
<h1 className="text-2xl font-semibold text-text-primary">{t('title')}</h1>
|
2026-05-07 03:40:29 +03:30
|
|
|
<p className="text-sm text-text-secondary">
|
2026-06-28 17:14:02 +03:30
|
|
|
{canEdit ? t('subtitleEditPhase4') : t('subtitleReadOnly')}
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
|
|
|
|
</header>
|
|
|
|
|
|
2026-05-19 23:43:43 +03:30
|
|
|
<ToastStack {...toastMessages} />
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
<AppointmentsStrip
|
|
|
|
|
stripHidden={stripHidden}
|
|
|
|
|
onToggleStripHidden={() => setStripHidden((s) => !s)}
|
|
|
|
|
selectedDay={selectedDay}
|
2026-05-19 22:29:29 +03:30
|
|
|
onSelectDay={onSelectDay}
|
2026-05-07 03:40:29 +03:30
|
|
|
appointments={appointments}
|
|
|
|
|
selectedAppointmentId={selectedAppointmentId}
|
|
|
|
|
onSelectAppointment={onPickAppointment}
|
|
|
|
|
loading={apptsLoading}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-05-18 12:50:25 +03:30
|
|
|
{isViewingPastDay && (
|
|
|
|
|
<p className="text-sm text-text-secondary rounded-[var(--radius-md)] border border-border/60 bg-background-secondary/50 px-3 py-2">
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('pastDayNotice')}
|
2026-05-18 12:50:25 +03:30
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-19 23:43:43 +03:30
|
|
|
<div className="grid grid-cols-1 xl:grid-cols-[minmax(300px,380px)_minmax(0,1fr)] gap-4 items-start">
|
|
|
|
|
<div className="space-y-3 min-w-0 xl:max-w-[380px]">
|
2026-05-07 03:40:29 +03:30
|
|
|
{selectedAppointment ? (
|
2026-05-19 23:43:43 +03:30
|
|
|
<div className="surface-card p-3 space-y-0.5">
|
2026-06-20 14:51:43 +03:30
|
|
|
<p className="text-[10px] uppercase tracking-wide text-text-muted">{t('selectedPatient')}</p>
|
2026-05-19 23:43:43 +03:30
|
|
|
<p className="text-base font-semibold text-text-primary">
|
2026-05-07 03:40:29 +03:30
|
|
|
{selectedAppointment.patientFirstName} {selectedAppointment.patientLastName}
|
|
|
|
|
</p>
|
2026-05-19 23:43:43 +03:30
|
|
|
<p className="text-[11px] text-text-secondary">
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('purposeLabel')}{' '}
|
|
|
|
|
<span className="capitalize text-text-primary">
|
2026-06-28 17:14:02 +03:30
|
|
|
{t(
|
|
|
|
|
treatmentTypeLabelKey(selectedAppointment.purpose) as 'typeConsultation',
|
|
|
|
|
)}
|
2026-06-20 14:51:43 +03:30
|
|
|
</span>
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-05-19 23:43:43 +03:30
|
|
|
<div className="surface-card p-3 text-sm text-text-muted">
|
2026-06-20 14:51:43 +03:30
|
|
|
{apptsLoading ? t('loadingAppointments') : t('selectDayWithAppointment')}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-19 23:43:43 +03:30
|
|
|
<TreatmentPreviewCard
|
|
|
|
|
draft={currentDraftPreview}
|
|
|
|
|
disabled={!selectedAppointment}
|
|
|
|
|
onPreview={openCurrentDraftPreview}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
<PastTreatmentsPanel
|
|
|
|
|
items={history}
|
|
|
|
|
loading={historyLoading}
|
2026-06-28 17:14:02 +03:30
|
|
|
onReviewTreatment={(item) => openPreview(item, 'readonly')}
|
2026-05-07 14:20:50 +03:30
|
|
|
/>
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
|
2026-05-19 23:43:43 +03:30
|
|
|
<div className="space-y-3 min-w-0 w-full">
|
2026-05-07 03:40:29 +03:30
|
|
|
<FdiToothChart
|
|
|
|
|
selected={selectedTeethSet}
|
2026-05-19 23:43:43 +03:30
|
|
|
onToggle={(fdi) => {
|
2026-06-28 17:14:02 +03:30
|
|
|
if (!canEditTreatmentForDay || isDetailLocked(activeDetail)) return;
|
|
|
|
|
setDetails((prev) =>
|
|
|
|
|
prev.map((d) => {
|
|
|
|
|
if (d.clientId !== activeDetailId) return d;
|
|
|
|
|
const set = new Set(d.teeth);
|
2026-05-19 23:43:43 +03:30
|
|
|
if (set.has(fdi)) set.delete(fdi);
|
|
|
|
|
else set.add(fdi);
|
2026-06-28 17:14:02 +03:30
|
|
|
return { ...d, teeth: [...set].sort() as FdiToothId[] };
|
2026-05-19 23:43:43 +03:30
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}}
|
2026-06-28 17:14:02 +03:30
|
|
|
disabled={!canEditTreatmentForDay || isDetailLocked(activeDetail)}
|
2026-05-07 03:40:29 +03:30
|
|
|
/>
|
|
|
|
|
|
2026-06-28 17:14:02 +03:30
|
|
|
<TreatmentDetailsEditor
|
|
|
|
|
details={details}
|
|
|
|
|
activeDetailId={activeDetailId}
|
|
|
|
|
onActiveDetailChange={setActiveDetailId}
|
|
|
|
|
onDetailsChange={setDetails}
|
|
|
|
|
isDetailLocked={isDetailLocked}
|
2026-05-19 23:43:43 +03:30
|
|
|
disabled={!canEditTreatmentForDay}
|
|
|
|
|
canEdit={canEdit}
|
|
|
|
|
isDirty={isDirty}
|
|
|
|
|
saveBusy={saveBusy}
|
2026-06-28 17:14:02 +03:30
|
|
|
uploadBusy={uploadBusyDetailId === activeDetailId}
|
|
|
|
|
onAddDetail={() => {
|
|
|
|
|
const next = newDetail();
|
|
|
|
|
setDetails((prev) => [...prev, next]);
|
|
|
|
|
setActiveDetailId(next.clientId);
|
|
|
|
|
}}
|
|
|
|
|
onPreview={openCurrentDraftPreview}
|
|
|
|
|
onSave={() => void handleSaveAll()}
|
|
|
|
|
onUploadFiles={(files) => void uploadForDetail(activeDetailId, files ?? [])}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<LabCasesDispatchPanel
|
|
|
|
|
details={details}
|
|
|
|
|
labCases={labCaseDrafts}
|
|
|
|
|
labDependentCodes={labDependentCodes}
|
|
|
|
|
activeLabCaseId={activeLabCaseId}
|
|
|
|
|
onActiveLabCaseChange={setActiveLabCaseId}
|
|
|
|
|
onLabCasesChange={setLabCaseDrafts}
|
|
|
|
|
disabled={!canEditTreatmentForDay}
|
|
|
|
|
canEdit={canEdit}
|
2026-05-19 23:43:43 +03:30
|
|
|
orgs={orgs}
|
|
|
|
|
organizationSearch={organizationSearch}
|
|
|
|
|
onOrganizationSearchChange={setOrganizationSearch}
|
|
|
|
|
recentOrganizationIds={recentOrganizationIds}
|
|
|
|
|
onRecentOrganizationPick={(orgId) => {
|
2026-06-28 17:14:02 +03:30
|
|
|
if (!activeLabCaseId) return;
|
|
|
|
|
setLabCaseDrafts((prev) =>
|
|
|
|
|
prev.map((lc) =>
|
|
|
|
|
lc.clientId === activeLabCaseId && !lc.sentAt
|
|
|
|
|
? { ...lc, destinationOrganizationId: orgId }
|
|
|
|
|
: lc,
|
|
|
|
|
),
|
2026-05-19 23:43:43 +03:30
|
|
|
);
|
|
|
|
|
}}
|
2026-06-28 17:14:02 +03:30
|
|
|
sendBusyId={sendBusyId}
|
|
|
|
|
saveLabBusy={saveLabBusy}
|
|
|
|
|
onAddLabCase={() => {
|
|
|
|
|
const next = newLabCaseDraft();
|
|
|
|
|
setLabCaseDrafts((prev) => [...prev, next]);
|
|
|
|
|
setActiveLabCaseId(next.clientId);
|
2026-05-19 23:43:43 +03:30
|
|
|
}}
|
2026-06-28 17:14:02 +03:30
|
|
|
onSaveLabCases={() => void handleSaveLabCases()}
|
|
|
|
|
onSendLabCase={(lc) => void handleSendLabCase(lc)}
|
2026-05-19 23:43:43 +03:30
|
|
|
/>
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-05-08 14:07:21 +03:30
|
|
|
|
2026-05-19 23:43:43 +03:30
|
|
|
<TreatmentPreviewDialog
|
|
|
|
|
open={previewOpen}
|
|
|
|
|
onClose={() => setPreviewOpen(false)}
|
|
|
|
|
treatment={
|
|
|
|
|
previewMode === 'editable' && currentDraftPreview ? currentDraftPreview : previewTreatment
|
|
|
|
|
}
|
|
|
|
|
mode={previewMode}
|
|
|
|
|
orgs={orgs}
|
2026-06-28 17:14:02 +03:30
|
|
|
uploadBusyCaseId={uploadBusyDetailId}
|
|
|
|
|
onAttach={(caseKey, files) => uploadForDetail(caseKey, files)}
|
2026-05-19 23:43:43 +03:30
|
|
|
/>
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|