ConceptPortal-public/rsconcept/frontend/src/features/library/backend/use-set-editors.tsx

45 lines
1.5 KiB
TypeScript
Raw Normal View History

import { useMutation, useQueryClient } from '@tanstack/react-query';
2025-02-26 00:16:41 +03:00
import { type IOperationSchemaDTO } from '@/features/oss';
import { type IRSFormDTO } from '@/features/rsform';
2025-02-12 21:36:25 +03:00
import { KEYS } from '@/backend/configuration';
import { libraryApi } from './api';
export const useSetEditors = () => {
const client = useQueryClient();
const mutation = useMutation({
2025-02-19 19:26:29 +03:00
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-location'],
mutationFn: libraryApi.setEditors,
onSuccess: (_, variables) => {
const ossKey = KEYS.composite.ossItem({ itemID: variables.itemID });
const ossData: IOperationSchemaDTO | undefined = client.getQueryData(ossKey);
if (ossData) {
client.setQueryData(ossKey, { ...ossData, editors: variables.editors });
return Promise.allSettled(
2025-04-06 15:49:43 +03:00
ossData.operations
.map(item => {
if (!item.result) {
return;
}
const itemKey = KEYS.composite.rsItem({ itemID: item.result });
return client.invalidateQueries({ queryKey: itemKey });
})
.filter(item => !!item)
);
}
const rsKey = KEYS.composite.rsItem({ itemID: variables.itemID });
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
!prev ? undefined : { ...prev, editors: variables.editors }
);
2025-02-19 19:26:29 +03:00
},
onError: () => client.invalidateQueries()
});
return {
2025-02-12 13:07:55 +03:00
setEditors: (data: { itemID: number; editors: number[] }) => mutation.mutateAsync(data)
};
};