2025-01-27 15:03:48 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-01-30 19:55:38 +03:00
|
|
|
import { IRSFormDTO, rsformsApi } from '@/backend/rsform/api';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-02-07 15:30:47 +03:00
|
|
|
import { IVersionUpdateDTO, libraryApi } from './api';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
|
|
|
export const useVersionUpdate = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: [libraryApi.baseKey, 'update-version'],
|
|
|
|
mutationFn: libraryApi.versionUpdate,
|
2025-01-28 19:47:24 +03:00
|
|
|
onSuccess: data => {
|
2025-01-27 15:03:48 +03:00
|
|
|
client.setQueryData(
|
2025-01-28 19:47:24 +03:00
|
|
|
rsformsApi.getRSFormQueryOptions({ itemID: data.item }).queryKey,
|
2025-01-30 19:55:38 +03:00
|
|
|
(prev: IRSFormDTO | undefined) =>
|
2025-01-28 19:47:24 +03:00
|
|
|
!prev
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
...prev,
|
|
|
|
versions: prev.versions.map(version =>
|
|
|
|
version.id === data.id
|
|
|
|
? { ...version, description: data.description, version: data.version }
|
|
|
|
: version
|
|
|
|
)
|
|
|
|
}
|
2025-01-27 15:03:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
2025-02-07 15:30:47 +03:00
|
|
|
versionUpdate: (data: IVersionUpdateDTO, onSuccess?: () => void) => mutation.mutate(data, { onSuccess })
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|
|
|
|
};
|