2025-01-27 15:03:48 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-12 20:53:31 +03:00
|
|
|
import { IRSFormDTO } from '@/features/rsform/backend/types';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-02-12 21:36:25 +03:00
|
|
|
import { KEYS } from '@/backend/configuration';
|
|
|
|
|
2025-02-12 20:53:31 +03:00
|
|
|
import { libraryApi } from './api';
|
|
|
|
import { IVersionUpdateDTO } from './types';
|
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-02-17 14:40:18 +03:00
|
|
|
onSuccess: (data, variables) => {
|
|
|
|
client.setQueryData(KEYS.composite.rsItem({ itemID: variables.itemID }), (prev: IRSFormDTO | undefined) =>
|
2025-02-12 20:53:31 +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-17 14:40:18 +03:00
|
|
|
versionUpdate: (data: { itemID: number; version: IVersionUpdateDTO }) => mutation.mutateAsync(data)
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|
|
|
|
};
|