114 lines
3.8 KiB
JavaScript
114 lines
3.8 KiB
JavaScript
import { useDispatch, useSelector } from "react-redux";
|
||
import { setSelectedAppointment } from "../../../Redux/Slices/appointmentsSlice.js";
|
||
import dayjs from "dayjs";
|
||
import { useGetAppointmentFilesQuery, useDownloadAppointmentFileMutation } from "../../../Api/appointmentFilesApi.js";
|
||
import { useState } from "react";
|
||
|
||
const useAppointmentViewUI = () => {
|
||
const dispatch = useDispatch();
|
||
const { selectedAppointment } = useSelector((state) => state.appointmentsUI);
|
||
|
||
const { data: files = [], isLoading: isFilesLoading } = useGetAppointmentFilesQuery(
|
||
selectedAppointment?.id,
|
||
{ skip: !selectedAppointment?.id }
|
||
);
|
||
|
||
const [downloadAppointmentFile] = useDownloadAppointmentFileMutation();
|
||
const [downloadingFiles, setDownloadingFiles] = useState({});
|
||
|
||
const modalWidth = 700;
|
||
const blockStyle = { marginBottom: 16 };
|
||
const footerRowStyle = { marginTop: 16 };
|
||
const footerButtonStyle = { marginRight: 8 };
|
||
|
||
const labels = {
|
||
title: "Просмотр приема",
|
||
patient: "Пациент:",
|
||
birthday: "Дата рождения:",
|
||
email: "Email:",
|
||
phone: "Телефон:",
|
||
type: "Тип приема:",
|
||
appointmentTime: "Время приема:",
|
||
daysUntilNext: "Дней до следующего приема:",
|
||
results: "Результаты приема:",
|
||
closeButton: "Закрыть",
|
||
notSpecified: "Не указан",
|
||
resultsNotSpecified: "Не указаны",
|
||
files: "Файлы:",
|
||
noFiles: "Файлы отсутствуют",
|
||
download: "Скачать",
|
||
downloading: "Загрузка...",
|
||
};
|
||
|
||
const visible = !!selectedAppointment;
|
||
|
||
const getDateString = (date) => {
|
||
return date ? new Date(date).toLocaleDateString("ru-RU") : labels.notSpecified;
|
||
};
|
||
|
||
const getAppointmentTime = (datetime) => {
|
||
return datetime
|
||
? dayjs(datetime).format("DD.MM.YYYY HH:mm")
|
||
: labels.notSpecified;
|
||
};
|
||
|
||
const getPatientName = (patient) => {
|
||
return patient
|
||
? `${patient.last_name} ${patient.first_name}`
|
||
: labels.notSpecified;
|
||
};
|
||
|
||
const getPatientField = (field) => {
|
||
return field || labels.notSpecified;
|
||
};
|
||
|
||
const getResults = (results) => {
|
||
return results || labels.resultsNotSpecified;
|
||
};
|
||
|
||
const onCancel = () => {
|
||
dispatch(setSelectedAppointment(null));
|
||
};
|
||
|
||
const downloadFile = async (fileId, fileName) => {
|
||
try {
|
||
setDownloadingFiles((prev) => ({ ...prev, [fileId]: true }));
|
||
const blob = await downloadAppointmentFile(fileId).unwrap();
|
||
const url = window.URL.createObjectURL(blob);
|
||
const link = document.createElement("a");
|
||
link.href = url;
|
||
link.setAttribute("download", fileName || "file");
|
||
document.body.appendChild(link);
|
||
link.click();
|
||
link.remove();
|
||
window.URL.revokeObjectURL(url);
|
||
} catch (error) {
|
||
console.error("Ошибка при скачивании файла:", error);
|
||
// Можно добавить уведомление об ошибке, например, с antd message
|
||
} finally {
|
||
setDownloadingFiles((prev) => ({ ...prev, [fileId]: false }));
|
||
}
|
||
};
|
||
|
||
return {
|
||
modalWidth,
|
||
blockStyle,
|
||
footerRowStyle,
|
||
footerButtonStyle,
|
||
labels,
|
||
selectedAppointment,
|
||
visible,
|
||
getDateString,
|
||
getAppointmentTime,
|
||
getPatientName,
|
||
getPatientField,
|
||
getResults,
|
||
onCancel,
|
||
files,
|
||
isFilesLoading,
|
||
downloadingFiles,
|
||
downloadFile,
|
||
};
|
||
};
|
||
|
||
export default useAppointmentViewUI; |