refactor: Удален endpoint загрузки файлов
Удален неиспользуемый endpoint загрузки файлов и соответствующий мутатор. Логика скачивания перенесена во frontend.
This commit is contained in:
parent
b53ab902f8
commit
4dca2314cc
@ -1,6 +1,5 @@
|
|||||||
import { createApi } from "@reduxjs/toolkit/query/react";
|
import { createApi } from "@reduxjs/toolkit/query/react";
|
||||||
import { baseQueryWithAuth } from "./baseQuery.js";
|
import { baseQueryWithAuth } from "./baseQuery.js";
|
||||||
import { isPlainObject } from "@reduxjs/toolkit";
|
|
||||||
|
|
||||||
export const appointmentFilesApi = createApi({
|
export const appointmentFilesApi = createApi({
|
||||||
reducerPath: 'appointmentFilesApi',
|
reducerPath: 'appointmentFilesApi',
|
||||||
@ -15,12 +14,6 @@ export const appointmentFilesApi = createApi({
|
|||||||
providesTags: ['AppointmentFile'],
|
providesTags: ['AppointmentFile'],
|
||||||
refetchOnMountOrArgChange: 5,
|
refetchOnMountOrArgChange: 5,
|
||||||
}),
|
}),
|
||||||
downloadAppointmentFile: builder.mutation({
|
|
||||||
query: (fileId) => ({
|
|
||||||
url: `/appointment_files/${fileId}/file/`,
|
|
||||||
method: 'GET',
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
deleteAppointmentFile: builder.mutation({
|
deleteAppointmentFile: builder.mutation({
|
||||||
query: (fileId) => ({
|
query: (fileId) => ({
|
||||||
url: `/appointment_files/${fileId}/`,
|
url: `/appointment_files/${fileId}/`,
|
||||||
@ -46,20 +39,10 @@ export const appointmentFilesApi = createApi({
|
|||||||
invalidatesTags: ['AppointmentFile'],
|
invalidatesTags: ['AppointmentFile'],
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
middleware: (defaultMiddleware) =>
|
|
||||||
defaultMiddleware({
|
|
||||||
serializableCheck: {
|
|
||||||
isSerializable: (value) => {
|
|
||||||
if (value instanceof Blob) return true; // Игнорируем Blob
|
|
||||||
return isPlainObject(value) || typeof value !== 'object';
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
useGetAppointmentFilesQuery,
|
useGetAppointmentFilesQuery,
|
||||||
useDownloadAppointmentFileMutation,
|
|
||||||
useDeleteAppointmentFileMutation,
|
useDeleteAppointmentFileMutation,
|
||||||
useUploadAppointmentFileMutation,
|
useUploadAppointmentFileMutation,
|
||||||
} = appointmentFilesApi;
|
} = appointmentFilesApi;
|
||||||
@ -1,8 +1,9 @@
|
|||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { setSelectedAppointment } from "../../../Redux/Slices/appointmentsSlice.js";
|
import { setSelectedAppointment } from "../../../Redux/Slices/appointmentsSlice.js";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import { useGetAppointmentFilesQuery, useDownloadAppointmentFileMutation } from "../../../Api/appointmentFilesApi.js";
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import {useGetAppointmentFilesQuery} from "../../../Api/appointmentFilesApi.js";
|
||||||
|
import {baseQueryWithAuth} from "../../../Api/baseQuery.js";
|
||||||
|
|
||||||
const useAppointmentViewUI = () => {
|
const useAppointmentViewUI = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -13,7 +14,6 @@ const useAppointmentViewUI = () => {
|
|||||||
{ skip: !selectedAppointment?.id }
|
{ skip: !selectedAppointment?.id }
|
||||||
);
|
);
|
||||||
|
|
||||||
const [downloadAppointmentFile] = useDownloadAppointmentFileMutation();
|
|
||||||
const [downloadingFiles, setDownloadingFiles] = useState({});
|
const [downloadingFiles, setDownloadingFiles] = useState({});
|
||||||
|
|
||||||
const modalWidth = 700;
|
const modalWidth = 700;
|
||||||
@ -73,18 +73,40 @@ const useAppointmentViewUI = () => {
|
|||||||
const downloadFile = async (fileId, fileName) => {
|
const downloadFile = async (fileId, fileName) => {
|
||||||
try {
|
try {
|
||||||
setDownloadingFiles((prev) => ({ ...prev, [fileId]: true }));
|
setDownloadingFiles((prev) => ({ ...prev, [fileId]: true }));
|
||||||
const blob = await downloadAppointmentFile(fileId).unwrap();
|
// Выполняем запрос с использованием fetch, применяя baseQueryWithAuth для аутентификации
|
||||||
const url = window.URL.createObjectURL(blob);
|
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");
|
const link = document.createElement("a");
|
||||||
link.href = url;
|
link.href = downloadUrl;
|
||||||
link.setAttribute("download", fileName || "file");
|
link.setAttribute("download", fileName || "file");
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
link.remove();
|
link.remove();
|
||||||
window.URL.revokeObjectURL(url);
|
window.URL.revokeObjectURL(downloadUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Ошибка при скачивании файла:", error);
|
console.error("Ошибка при скачивании файла:", error);
|
||||||
// Можно добавить уведомление об ошибке, например, с antd message
|
// Можно добавить уведомление, например, с antd message
|
||||||
} finally {
|
} finally {
|
||||||
setDownloadingFiles((prev) => ({ ...prev, [fileId]: false }));
|
setDownloadingFiles((prev) => ({ ...prev, [fileId]: false }));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user