refactor: Удален endpoint загрузки файлов

Удален неиспользуемый endpoint загрузки файлов и соответствующий мутатор. Логика скачивания перенесена во frontend.
This commit is contained in:
Андрей Дувакин 2025-06-04 19:09:38 +05:00
parent b53ab902f8
commit 4dca2314cc
2 changed files with 29 additions and 24 deletions

View File

@ -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;

View File

@ -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 }));
}