2025-01-23 19:41:31 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
import { KEYS } from '@/backend/configuration';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
import { ossApi } from './api';
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type ITargetOperation } from './types';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
2025-04-20 16:20:43 +03:00
|
|
|
export const useExecuteOperation = () => {
|
2025-01-23 19:41:31 +03:00
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
2025-04-20 18:06:18 +03:00
|
|
|
mutationKey: [KEYS.global_mutation, ossApi.baseKey, 'execute-operation'],
|
2025-04-20 16:20:43 +03:00
|
|
|
mutationFn: ossApi.executeOperation,
|
2025-01-29 14:51:34 +03:00
|
|
|
onSuccess: data => {
|
2025-01-28 19:45:31 +03:00
|
|
|
client.setQueryData(ossApi.getOssQueryOptions({ itemID: data.id }).queryKey, data);
|
2025-01-29 14:51:34 +03:00
|
|
|
return Promise.allSettled([
|
2025-02-12 20:53:01 +03:00
|
|
|
client.invalidateQueries({ queryKey: KEYS.composite.libraryList }),
|
|
|
|
client.invalidateQueries({ queryKey: [KEYS.rsform] })
|
2025-01-29 14:51:34 +03:00
|
|
|
]);
|
2025-02-19 19:25:11 +03:00
|
|
|
},
|
|
|
|
onError: () => client.invalidateQueries()
|
2025-01-23 19:41:31 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-04-20 16:20:43 +03:00
|
|
|
executeOperation: (data: { itemID: number; data: ITargetOperation }) => mutation.mutateAsync(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|