diff --git a/web-app/src/Api/appointmentFilesApi.js b/web-app/src/Api/appointmentFilesApi.js new file mode 100644 index 0000000..1f72772 --- /dev/null +++ b/web-app/src/Api/appointmentFilesApi.js @@ -0,0 +1,55 @@ +import { createApi } from "@reduxjs/toolkit/query/react"; +import { baseQueryWithAuth } from "./baseQuery.js"; + +export const appointmentFilesApi = createApi({ + reducerPath: 'appointmentFilesApi', + baseQuery: baseQueryWithAuth, + tagTypes: ['AppointmentFile'], + endpoints: (builder) => ({ + getAppointmentFiles: builder.query({ + query: (appointmentId) => `/appointment-files/${appointmentId}/`, + providesTags: ['AppointmentFile'], + refetchOnMountOrArgChange: 5, + }), + downloadAppointmentFile: builder.query({ + query: (fileId) => ({ + url: `/appointment-files/${fileId}/file`, + responseHandler: async (response) => { + const blob = await response.blob(); + const contentDisposition = response.headers.get('content-disposition'); + const filename = contentDisposition + ? contentDisposition.match(/filename="(.+)"/)?.[1] || `file_${fileId}` + : `file_${fileId}`; + return { blob, filename }; + }, + cache: 'no-cache', + }), + }), + uploadAppointmentFile: builder.mutation({ + query: ({ appointmentId, file }) => { + const formData = new FormData(); + formData.append('file', file); + return { + url: `/appointment-files/${appointmentId}/upload`, + method: 'POST', + body: formData, + }; + }, + invalidatesTags: ['AppointmentFile'], + }), + deleteAppointmentFile: builder.mutation({ + query: (fileId) => ({ + url: `/appointment-files/${fileId}/`, + method: 'DELETE', + }), + invalidatesTags: ['AppointmentFile'], + }), + }), +}); + +export const { + useGetAppointmentFilesQuery, + useDownloadAppointmentFileQuery, + useUploadAppointmentFileMutation, + useDeleteAppointmentFileMutation, +} = appointmentFilesApi; \ No newline at end of file