Portal/rsconcept/frontend/src/features/library/backend/useVersionDelete.tsx

30 lines
962 B
TypeScript
Raw Normal View History

2025-01-23 19:41:31 +03:00
import { useMutation, useQueryClient } from '@tanstack/react-query';
2025-02-20 20:22:05 +03:00
import { type IRSFormDTO } from '@/features/rsform/backend/types';
2025-01-23 19:41:31 +03:00
2025-02-12 21:36:03 +03:00
import { KEYS } from '@/backend/configuration';
2025-01-23 19:41:31 +03:00
import { libraryApi } from './api';
export const useVersionDelete = () => {
const client = useQueryClient();
const mutation = useMutation({
2025-02-19 19:15:57 +03:00
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'delete-version'],
2025-01-23 19:41:31 +03:00
mutationFn: libraryApi.versionDelete,
onSuccess: (_, variables) => {
client.setQueryData(KEYS.composite.rsItem({ itemID: variables.itemID }), (prev: IRSFormDTO | undefined) =>
!prev
? undefined
: {
...prev,
versions: prev.versions.filter(version => version.id !== variables.versionID)
}
2025-01-23 19:41:31 +03:00
);
},
onError: () => client.invalidateQueries()
2025-01-23 19:41:31 +03:00
});
return {
versionDelete: (data: { itemID: number; versionID: number }) => mutation.mutateAsync(data)
2025-01-23 19:41:31 +03:00
};
};