mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-08-17 14:20:36 +03:00
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import { useUpdateTimestamp } from '@/features/library/backend/use-update-timestamp';
|
|
|
|
import { KEYS } from '@/backend/configuration';
|
|
import { type RO } from '@/utils/meta';
|
|
|
|
import { ossApi } from './api';
|
|
import { type IOperationSchemaDTO, type IOssLayout } from './types';
|
|
|
|
export const useUpdateLayout = () => {
|
|
const client = useQueryClient();
|
|
const { updateTimestamp } = useUpdateTimestamp();
|
|
const mutation = useMutation({
|
|
mutationKey: [KEYS.global_mutation, ossApi.baseKey, 'update-layout'],
|
|
mutationFn: ossApi.updateLayout,
|
|
onSuccess: (_, variables) => {
|
|
updateTimestamp(variables.itemID);
|
|
client.setQueryData(
|
|
ossApi.getOssQueryOptions({ itemID: variables.itemID }).queryKey,
|
|
(prev: RO<IOperationSchemaDTO> | undefined) =>
|
|
!prev
|
|
? prev
|
|
: {
|
|
...prev,
|
|
layout: variables.data
|
|
}
|
|
);
|
|
},
|
|
onError: () => client.invalidateQueries()
|
|
});
|
|
return {
|
|
updateLayout: (data: {
|
|
itemID: number; //
|
|
data: IOssLayout;
|
|
isSilent?: boolean;
|
|
}) => mutation.mutateAsync(data)
|
|
};
|
|
};
|