2025-01-27 15:03:48 +03:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
2025-02-12 15:13:37 +03:00
|
|
|
import { useUpdateTimestamp } from '@/features/library';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
2025-02-12 21:36:25 +03:00
|
|
|
import { KEYS } from '@/backend/configuration';
|
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
import { rsformsApi } from './api';
|
|
|
|
|
|
|
|
export const useProduceStructure = () => {
|
|
|
|
const client = useQueryClient();
|
|
|
|
const { updateTimestamp } = useUpdateTimestamp();
|
|
|
|
const mutation = useMutation({
|
2025-02-19 19:26:29 +03:00
|
|
|
mutationKey: [KEYS.global_mutation, rsformsApi.baseKey, 'produce-structure'],
|
2025-01-27 15:03:48 +03:00
|
|
|
mutationFn: rsformsApi.produceStructure,
|
|
|
|
onSuccess: data => {
|
2025-01-28 19:47:24 +03:00
|
|
|
client.setQueryData(rsformsApi.getRSFormQueryOptions({ itemID: data.schema.id }).queryKey, data.schema);
|
2025-01-27 15:03:48 +03:00
|
|
|
updateTimestamp(data.schema.id);
|
2025-01-29 14:52:07 +03:00
|
|
|
|
|
|
|
return Promise.allSettled([
|
2025-02-12 20:53:31 +03:00
|
|
|
client.invalidateQueries({ queryKey: [KEYS.oss] }),
|
2025-01-29 14:52:07 +03:00
|
|
|
client.invalidateQueries({
|
|
|
|
queryKey: [rsformsApi.baseKey],
|
|
|
|
predicate: query => query.queryKey.length > 2 && query.queryKey[2] !== data.schema.id
|
|
|
|
})
|
|
|
|
]);
|
2025-02-19 19:26:29 +03:00
|
|
|
},
|
|
|
|
onError: () => client.invalidateQueries()
|
2025-01-27 15:03:48 +03:00
|
|
|
});
|
|
|
|
return {
|
2025-02-22 12:20:47 +03:00
|
|
|
produceStructure: (data: { itemID: number; cstID: number }) =>
|
2025-02-11 20:16:11 +03:00
|
|
|
mutation.mutateAsync(data).then(response => response.cst_list)
|
2025-01-27 15:03:48 +03:00
|
|
|
};
|
|
|
|
};
|