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

33 lines
1.1 KiB
TypeScript
Raw Normal View History

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