2025-01-23 19:41:31 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-01-30 19:55:24 +03:00
|
|
|
import { IOperationSchemaDTO, ossApi } from '@/backend/oss/api';
|
2025-01-29 14:51:34 +03:00
|
|
|
import { ILibraryItem, LibraryItemType } from '@/models/library';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
2025-01-30 19:55:24 +03:00
|
|
|
import { IRSFormDTO } from '../rsform/api';
|
2025-01-23 19:41:31 +03:00
|
|
|
import { ILibraryUpdateDTO, libraryApi } from './api';
|
|
|
|
|
|
|
|
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-01-28 19:45:31 +03:00
|
|
|
updateItem: (data: ILibraryUpdateDTO) => mutation.mutate(data)
|
2025-01-23 19:41:31 +03:00
|
|
|
};
|
|
|
|
};
|