diff --git a/web-app/src/Api/appointmentFilesApi.js b/web-app/src/Api/appointmentFilesApi.js index 17fd2e1..af23c6b 100644 --- a/web-app/src/Api/appointmentFilesApi.js +++ b/web-app/src/Api/appointmentFilesApi.js @@ -1,6 +1,5 @@ import { createApi } from "@reduxjs/toolkit/query/react"; import { baseQueryWithAuth } from "./baseQuery.js"; -import { isPlainObject } from "@reduxjs/toolkit"; export const appointmentFilesApi = createApi({ reducerPath: 'appointmentFilesApi', @@ -15,12 +14,6 @@ export const appointmentFilesApi = createApi({ providesTags: ['AppointmentFile'], refetchOnMountOrArgChange: 5, }), - downloadAppointmentFile: builder.mutation({ - query: (fileId) => ({ - url: `/appointment_files/${fileId}/file/`, - method: 'GET', - }), - }), deleteAppointmentFile: builder.mutation({ query: (fileId) => ({ url: `/appointment_files/${fileId}/`, @@ -46,20 +39,10 @@ export const appointmentFilesApi = createApi({ invalidatesTags: ['AppointmentFile'], }), }), - middleware: (defaultMiddleware) => - defaultMiddleware({ - serializableCheck: { - isSerializable: (value) => { - if (value instanceof Blob) return true; // Игнорируем Blob - return isPlainObject(value) || typeof value !== 'object'; - }, - }, - }), }); export const { useGetAppointmentFilesQuery, - useDownloadAppointmentFileMutation, useDeleteAppointmentFileMutation, useUploadAppointmentFileMutation, } = appointmentFilesApi; \ No newline at end of file diff --git a/web-app/src/Components/Widgets/AppointmentViewModal/useAppointmentViewUI.js b/web-app/src/Components/Widgets/AppointmentViewModal/useAppointmentViewUI.js index fe1d90d..aeeaf1c 100644 --- a/web-app/src/Components/Widgets/AppointmentViewModal/useAppointmentViewUI.js +++ b/web-app/src/Components/Widgets/AppointmentViewModal/useAppointmentViewUI.js @@ -1,8 +1,9 @@ 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"; +import {useGetAppointmentFilesQuery} from "../../../Api/appointmentFilesApi.js"; +import {baseQueryWithAuth} from "../../../Api/baseQuery.js"; const useAppointmentViewUI = () => { const dispatch = useDispatch(); @@ -13,7 +14,6 @@ const useAppointmentViewUI = () => { { skip: !selectedAppointment?.id } ); - const [downloadAppointmentFile] = useDownloadAppointmentFileMutation(); const [downloadingFiles, setDownloadingFiles] = useState({}); const modalWidth = 700; @@ -73,18 +73,40 @@ const useAppointmentViewUI = () => { const downloadFile = async (fileId, fileName) => { try { setDownloadingFiles((prev) => ({ ...prev, [fileId]: true })); - const blob = await downloadAppointmentFile(fileId).unwrap(); - const url = window.URL.createObjectURL(blob); + // Выполняем запрос с использованием fetch, применяя baseQueryWithAuth для аутентификации + const { url, ...options } = await baseQueryWithAuth( + { + url: `/appointment_files/${fileId}/file/`, + method: 'GET', + credentials: 'include', + }, + { getState: () => ({}) }, + {} + ); + + // Поскольку baseQueryWithAuth может вернуть объект с полной URL, используем его + const response = await fetch(url, { + ...options, + method: 'GET', + credentials: 'include', + }); + + if (!response.ok) { + throw new Error(`Ошибка HTTP: ${response.status} ${response.statusText}`); + } + + const blob = await response.blob(); + const downloadUrl = window.URL.createObjectURL(blob); const link = document.createElement("a"); - link.href = url; + link.href = downloadUrl; link.setAttribute("download", fileName || "file"); document.body.appendChild(link); link.click(); link.remove(); - window.URL.revokeObjectURL(url); + window.URL.revokeObjectURL(downloadUrl); } catch (error) { console.error("Ошибка при скачивании файла:", error); - // Можно добавить уведомление об ошибке, например, с antd message + // Можно добавить уведомление, например, с antd message } finally { setDownloadingFiles((prev) => ({ ...prev, [fileId]: false })); }