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-02-10 01:32:16 +03:00
|
|
|
import { ILibraryItem, LibraryItemType } from '../models/library';
|
2025-02-12 21:36:03 +03:00
|
|
|
|
2025-02-12 20:53:01 +03:00
|
|
|
import { libraryApi } from './api';
|
|
|
|
import { IUpdateLibraryItemDTO } from './types';
|
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-02-12 20:53:01 +03:00
|
|
|
const itemKey =
|
|
|
|
data.item_type === LibraryItemType.RSFORM
|
|
|
|
? KEYS.composite.rsItem({ itemID: data.id })
|
|
|
|
: KEYS.composite.ossItem({ itemID: data.id });
|
2025-01-29 14:51:34 +03:00
|
|
|
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(
|
2025-02-12 20:53:01 +03:00
|
|
|
schema.oss.map(item => client.invalidateQueries({ queryKey: KEYS.composite.ossItem({ itemID: item.id }) }))
|
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
|
|
|
};
|
|
|
|
};
|