2025-01-27 15:03:48 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-26 00:16:41 +03:00
|
|
|
import { type IRSFormDTO } from '@/features/rsform';
|
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';
|
2025-04-20 16:21:06 +03:00
|
|
|
import { type IUpdateVersionDTO } from './types';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-04-20 16:21:06 +03:00
|
|
|
export const useUpdateVersion = () => {
|
2025-01-27 15:03:48 +03:00
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
2025-02-19 19:26:29 +03:00
|
|
|
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'update-version'],
|
2025-04-20 16:21:06 +03:00
|
|
|
mutationFn: libraryApi.updateVersion,
|
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
|
|
|
);
|
2025-04-09 20:09:23 +03:00
|
|
|
client.setQueryData(
|
|
|
|
KEYS.composite.rsItem({ itemID: variables.itemID, version: variables.version.id }),
|
|
|
|
(prev: IRSFormDTO | undefined) =>
|
|
|
|
!prev
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
...prev,
|
|
|
|
versions: prev.versions.map(version =>
|
|
|
|
version.id === data.id
|
|
|
|
? { ...version, description: data.description, version: data.version }
|
|
|
|
: version
|
|
|
|
)
|
|
|
|
}
|
|
|
|
);
|
2025-02-19 19:26:29 +03:00
|
|
|
},
|
|
|
|
onError: () => client.invalidateQueries()
|
2025-01-27 15:03:48 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-04-20 16:21:06 +03:00
|
|
|
updateVersion: (data: { itemID: number; version: IUpdateVersionDTO }) => mutation.mutateAsync(data)
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|
|
|
|
};
|