2025-01-27 15:03:48 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-07-23 16:13:29 +03:00
|
|
|
import { useUpdateTimestamp } from '@/features/library/backend/use-update-timestamp';
|
|
|
|
|
2025-02-12 20:53:31 +03:00
|
|
|
import { KEYS } from '@/backend/configuration';
|
2025-08-05 16:54:52 +03:00
|
|
|
import { PARAMETER } from '@/utils/constants';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-02-12 20:53:31 +03:00
|
|
|
import { ossApi } from './api';
|
2025-04-20 16:21:06 +03:00
|
|
|
import { type IDeleteOperationDTO } from './types';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-04-20 16:21:06 +03:00
|
|
|
export const useDeleteOperation = () => {
|
2025-01-27 15:03:48 +03:00
|
|
|
const client = useQueryClient();
|
2025-07-23 16:13:29 +03:00
|
|
|
const { updateTimestamp } = useUpdateTimestamp();
|
2025-01-27 15:03:48 +03:00
|
|
|
const mutation = useMutation({
|
2025-04-20 18:06:46 +03:00
|
|
|
mutationKey: [KEYS.global_mutation, ossApi.baseKey, 'delete-operation'],
|
2025-04-20 16:21:06 +03:00
|
|
|
mutationFn: ossApi.deleteOperation,
|
2025-08-05 16:54:52 +03:00
|
|
|
onSuccess: async (data, variables) => {
|
|
|
|
if (variables.beforeUpdate) {
|
|
|
|
variables.beforeUpdate();
|
|
|
|
await new Promise(resolve => setTimeout(resolve, PARAMETER.minimalTimeout));
|
|
|
|
}
|
2025-07-23 16:13:29 +03:00
|
|
|
updateTimestamp(data.id, data.time_update);
|
2025-01-28 19:47:24 +03:00
|
|
|
client.setQueryData(ossApi.getOssQueryOptions({ itemID: data.id }).queryKey, data);
|
2025-06-05 15:46:15 +03:00
|
|
|
await Promise.allSettled([
|
2025-02-12 20:53:31 +03:00
|
|
|
client.invalidateQueries({ queryKey: KEYS.composite.libraryList }),
|
|
|
|
client.invalidateQueries({ queryKey: [KEYS.rsform] })
|
2025-01-29 14:52:07 +03:00
|
|
|
]);
|
2025-02-19 19:26:29 +03:00
|
|
|
},
|
|
|
|
onError: () => client.invalidateQueries()
|
2025-01-27 15:03:48 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-08-05 16:54:52 +03:00
|
|
|
deleteOperation: (data: { itemID: number; data: IDeleteOperationDTO; beforeUpdate?: () => void }) => {
|
|
|
|
mutation.mutate(data);
|
|
|
|
}
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|
|
|
|
};
|