Portal/rsconcept/frontend/src/features/library/backend/useUpdateItem.tsx

42 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-01-23 19:41:31 +03:00
import { useMutation, useQueryClient } from '@tanstack/react-query';
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';
import { libraryApi } from './api';
2025-02-18 19:39:54 +03:00
import { ILibraryItem, IUpdateLibraryItemDTO, LibraryItemType } from './types';
2025-01-23 19:41:31 +03:00
export const useUpdateItem = () => {
const client = useQueryClient();
const mutation = useMutation({
2025-02-19 19:15:57 +03:00
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'update-item'],
2025-01-23 19:41:31 +03:00
mutationFn: libraryApi.updateItem,
onSuccess: (data: ILibraryItem) => {
const itemKey =
data.item_type === LibraryItemType.RSFORM
? KEYS.composite.rsItem({ itemID: data.id })
: KEYS.composite.ossItem({ itemID: data.id });
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === data.id ? data : item))
);
client.setQueryData(itemKey, (prev: IRSFormDTO | IOperationSchemaDTO | undefined) =>
!prev ? undefined : { ...prev, ...data }
);
if (data.item_type === LibraryItemType.RSFORM) {
const schema: IRSFormDTO | undefined = client.getQueryData(itemKey);
if (schema) {
return Promise.allSettled(
schema.oss.map(item => client.invalidateQueries({ queryKey: KEYS.composite.ossItem({ itemID: item.id }) }))
2025-01-23 19:41:31 +03:00
);
}
}
},
onError: () => client.invalidateQueries()
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
};
};