52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import { type IOperationSchemaDTO } from '@/features/oss';
|
|
import { type IRSFormDTO } from '@/features/rsform';
|
|
|
|
import { KEYS } from '@/backend/configuration';
|
|
|
|
import { libraryApi } from './api';
|
|
import { type ILibraryItem } from './types';
|
|
import { useLibraryListKey } from './use-library';
|
|
|
|
export const useSetOwner = () => {
|
|
const client = useQueryClient();
|
|
const libraryKey = useLibraryListKey();
|
|
const mutation = useMutation({
|
|
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-owner'],
|
|
mutationFn: libraryApi.setOwner,
|
|
onSuccess: (_, variables) => {
|
|
const ossKey = KEYS.composite.ossItem({ itemID: variables.itemID });
|
|
const ossData: IOperationSchemaDTO | undefined = client.getQueryData(ossKey);
|
|
if (ossData) {
|
|
client.setQueryData(ossKey, { ...ossData, owner: variables.owner });
|
|
return Promise.allSettled([
|
|
client.invalidateQueries({ queryKey: libraryApi.libraryListKey }),
|
|
...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, owner: variables.owner }
|
|
);
|
|
client.setQueryData(libraryKey, (prev: ILibraryItem[] | undefined) =>
|
|
prev?.map(item => (item.id === variables.itemID ? { ...item, owner: variables.owner } : item))
|
|
);
|
|
},
|
|
onError: () => client.invalidateQueries()
|
|
});
|
|
|
|
return {
|
|
setOwner: (data: { itemID: number; owner: number }) => mutation.mutateAsync(data)
|
|
};
|
|
};
|