2025-01-23 19:41:31 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { IOperationSchemaDTO, 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 { ILibraryItem, LibraryItemID } from '../models/library';
|
2025-01-23 19:41:31 +03:00
|
|
|
import { libraryApi } from './api';
|
|
|
|
|
|
|
|
export const useSetOwner = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: [libraryApi.baseKey, 'set-owner'],
|
|
|
|
mutationFn: libraryApi.setOwner,
|
|
|
|
onSuccess: (_, variables) => {
|
2025-01-29 14:51:34 +03:00
|
|
|
const ossKey = ossApi.getOssQueryOptions({ itemID: variables.itemID }).queryKey;
|
2025-01-30 19:55:24 +03:00
|
|
|
const ossData: IOperationSchemaDTO | undefined = client.getQueryData(ossKey);
|
2025-01-29 14:51:34 +03:00
|
|
|
if (ossData) {
|
|
|
|
client.setQueryData(ossKey, { ...ossData, owner: variables.owner });
|
|
|
|
return Promise.allSettled([
|
|
|
|
client.invalidateQueries({ queryKey: libraryApi.libraryListKey }),
|
|
|
|
...ossData.items
|
|
|
|
.map(item => {
|
|
|
|
if (!item.result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const itemKey = rsformsApi.getRSFormQueryOptions({ itemID: item.result }).queryKey;
|
|
|
|
return client.invalidateQueries({ queryKey: itemKey });
|
|
|
|
})
|
|
|
|
.filter(item => !!item)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rsKey = rsformsApi.getRSFormQueryOptions({ itemID: variables.itemID }).queryKey;
|
|
|
|
client.setQueryData(rsKey, prev => (!prev ? undefined : { ...prev, owner: variables.owner }));
|
2025-01-23 19:41:31 +03:00
|
|
|
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
|
|
|
|
prev?.map(item => (item.id === variables.itemID ? { ...item, owner: variables.owner } : item))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2025-02-11 20:15:34 +03:00
|
|
|
setOwner: (data: { itemID: LibraryItemID; owner: UserID }) => mutation.mutateAsync(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|