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 IOperationSchemaDTO } from '@/features/oss';
|
|
|
|
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';
|
|
|
|
|
2025-01-23 19:41:31 +03:00
|
|
|
import { libraryApi } from './api';
|
|
|
|
|
|
|
|
export const useSetEditors = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
2025-02-19 19:15:57 +03:00
|
|
|
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-location'],
|
2025-01-23 19:41:31 +03:00
|
|
|
mutationFn: libraryApi.setEditors,
|
|
|
|
onSuccess: (_, variables) => {
|
2025-02-12 20:53:01 +03:00
|
|
|
const ossKey = KEYS.composite.ossItem({ itemID: variables.itemID });
|
|
|
|
const ossData: IOperationSchemaDTO | undefined = client.getQueryData(ossKey);
|
2025-01-28 19:45:31 +03:00
|
|
|
if (ossData) {
|
|
|
|
client.setQueryData(ossKey, { ...ossData, editors: variables.editors });
|
2025-01-29 14:51:34 +03:00
|
|
|
return Promise.allSettled(
|
|
|
|
ossData.items
|
|
|
|
.map(item => {
|
|
|
|
if (!item.result) {
|
|
|
|
return;
|
|
|
|
}
|
2025-02-12 20:53:01 +03:00
|
|
|
const itemKey = KEYS.composite.rsItem({ itemID: item.result });
|
2025-01-29 14:51:34 +03:00
|
|
|
return client.invalidateQueries({ queryKey: itemKey });
|
|
|
|
})
|
|
|
|
.filter(item => !!item)
|
|
|
|
);
|
2025-01-28 19:45:31 +03:00
|
|
|
}
|
2025-01-29 14:51:34 +03:00
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
const rsKey = KEYS.composite.rsItem({ itemID: variables.itemID });
|
|
|
|
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
|
|
|
|
!prev ? undefined : { ...prev, editors: variables.editors }
|
|
|
|
);
|
2025-02-19 19:25:11 +03:00
|
|
|
},
|
|
|
|
onError: () => client.invalidateQueries()
|
2025-01-23 19:41:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2025-02-12 13:07:26 +03:00
|
|
|
setEditors: (data: { itemID: number; editors: number[] }) => mutation.mutateAsync(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|