2025-01-23 19:41:31 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
import { IOperationSchemaDTO } from '@/features/oss/backend/types';
|
|
|
|
import { IRSFormDTO } from '@/features/rsform/backend/types';
|
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';
|
2025-02-18 19:39:54 +03:00
|
|
|
import { ILibraryItem } from './types';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
|
|
|
export const useSetLocation = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: [libraryApi.baseKey, 'set-location'],
|
|
|
|
mutationFn: libraryApi.setLocation,
|
|
|
|
onSuccess: (_, variables) => {
|
2025-02-12 20:53:01 +03:00
|
|
|
const ossKey = KEYS.composite.ossItem({ itemID: variables.itemID });
|
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, location: variables.location });
|
|
|
|
return Promise.allSettled([
|
|
|
|
client.invalidateQueries({ queryKey: libraryApi.libraryListKey }),
|
|
|
|
...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-02-12 20:53:01 +03:00
|
|
|
const rsKey = KEYS.composite.rsItem({ itemID: variables.itemID });
|
|
|
|
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
|
|
|
|
!prev ? undefined : { ...prev, location: variables.location }
|
|
|
|
);
|
2025-01-23 19:41:31 +03:00
|
|
|
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
|
|
|
|
prev?.map(item => (item.id === variables.itemID ? { ...item, location: variables.location } : item))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2025-02-12 20:53:01 +03:00
|
|
|
setLocation: (data: { itemID: number; location: string }) => mutation.mutateAsync(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|