mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 13:00:39 +03:00
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
![]() |
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||
|
|
||
|
import { DataCallback } from '@/backend/apiTransport';
|
||
|
import { rsformsApi } from '@/backend/rsform/api';
|
||
|
import { ILibraryItem } from '@/models/library';
|
||
|
|
||
|
import { ILibraryUpdateDTO, libraryApi } from './api';
|
||
|
|
||
|
export const useUpdateItem = () => {
|
||
|
const client = useQueryClient();
|
||
|
const mutation = useMutation({
|
||
|
mutationKey: [libraryApi.baseKey, 'update-item'],
|
||
|
mutationFn: libraryApi.updateItem,
|
||
|
onSuccess: (data: ILibraryItem) => {
|
||
|
client
|
||
|
.cancelQueries({ queryKey: [libraryApi.libraryListKey] })
|
||
|
.then(async () => {
|
||
|
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
|
||
|
prev?.map(item => (item.id === data.id ? data : item))
|
||
|
);
|
||
|
await client.invalidateQueries({
|
||
|
queryKey: [rsformsApi.getRSFormQueryOptions({ itemID: data.id }).queryKey]
|
||
|
});
|
||
|
})
|
||
|
.catch(console.error);
|
||
|
}
|
||
|
});
|
||
|
return {
|
||
|
updateItem: (
|
||
|
data: ILibraryUpdateDTO, //
|
||
|
onSuccess?: DataCallback<ILibraryItem>
|
||
|
) => mutation.mutate(data, { onSuccess })
|
||
|
};
|
||
|
};
|