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 { IRSFormDTO } from '@/features/rsform/backend/api';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { ILibraryItem, LibraryItemType } from '../models/library';
|
2025-02-04 23:33:35 +03:00
|
|
|
import { IUpdateLibraryItemDTO, libraryApi } from './api';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
|
|
|
export const useUpdateItem = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
|
|
mutationKey: [libraryApi.baseKey, 'update-item'],
|
|
|
|
mutationFn: libraryApi.updateItem,
|
|
|
|
onSuccess: (data: ILibraryItem) => {
|
2025-01-29 14:51:34 +03:00
|
|
|
const itemKey = libraryApi.getItemQueryOptions({ itemID: data.id, itemType: data.item_type }).queryKey;
|
|
|
|
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
|
|
|
|
prev?.map(item => (item.id === data.id ? data : item))
|
|
|
|
);
|
2025-01-30 19:55:24 +03:00
|
|
|
client.setQueryData(itemKey, (prev: IRSFormDTO | IOperationSchemaDTO | undefined) =>
|
2025-01-29 14:51:34 +03:00
|
|
|
!prev ? undefined : { ...prev, ...data }
|
|
|
|
);
|
|
|
|
if (data.item_type === LibraryItemType.RSFORM) {
|
2025-01-30 19:55:24 +03:00
|
|
|
const schema: IRSFormDTO | undefined = client.getQueryData(itemKey);
|
2025-01-29 14:51:34 +03:00
|
|
|
if (schema) {
|
|
|
|
return Promise.allSettled(
|
|
|
|
schema.oss.map(item =>
|
|
|
|
client.invalidateQueries({ queryKey: ossApi.getOssQueryOptions({ itemID: item.id }).queryKey })
|
|
|
|
)
|
2025-01-23 19:41:31 +03:00
|
|
|
);
|
2025-01-29 14:51:34 +03:00
|
|
|
}
|
|
|
|
}
|
2025-01-23 19:41:31 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
2025-02-11 20:15:34 +03:00
|
|
|
updateItem: (data: IUpdateLibraryItemDTO) => mutation.mutateAsync(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|