2025-01-23 19:41:31 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { ossApi } from '@/features/oss/backend/api';
|
|
|
|
import { rsformsApi } from '@/features/rsform/backend/api';
|
|
|
|
import { UserID } from '@/features/users/models/user';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { LibraryItemID } from '../models/library';
|
2025-01-23 19:41:31 +03:00
|
|
|
import { libraryApi } from './api';
|
|
|
|
|
|
|
|
export const useSetEditors = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: [libraryApi.baseKey, 'set-location'],
|
|
|
|
mutationFn: libraryApi.setEditors,
|
|
|
|
onSuccess: (_, variables) => {
|
2025-01-28 19:45:31 +03:00
|
|
|
const ossKey = ossApi.getOssQueryOptions({ itemID: variables.itemID }).queryKey;
|
|
|
|
const ossData = client.getQueryData(ossKey);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
const itemKey = rsformsApi.getRSFormQueryOptions({ itemID: item.result }).queryKey;
|
|
|
|
return client.invalidateQueries({ queryKey: itemKey });
|
|
|
|
})
|
|
|
|
.filter(item => !!item)
|
|
|
|
);
|
2025-01-28 19:45:31 +03:00
|
|
|
}
|
2025-01-29 14:51:34 +03:00
|
|
|
|
|
|
|
const rsKey = rsformsApi.getRSFormQueryOptions({ itemID: variables.itemID }).queryKey;
|
|
|
|
client.setQueryData(rsKey, prev => (!prev ? undefined : { ...prev, editors: variables.editors }));
|
2025-01-23 19:41:31 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2025-01-28 19:45:31 +03:00
|
|
|
setEditors: (data: { itemID: LibraryItemID; editors: UserID[] }) => mutation.mutate(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|