fair-rose
fair-rose2mo ago

Refecth query on error using useForm in REST and antd

I have this:
const { form, formProps, saveButtonProps, onFinish, queryResult } = useForm({
id,
action: RESOURCES_ACTION_EDIT,
resource: RESOURCES.COMPARISONS,
onMutationSuccess: () => {
const message = translate("common.success");
const description = translate("pages.pricingAuditComparisions.edit.success");
notification.success({ message, description });
},
onMutationError: () => {
const message = translate("common.error");
const description = translate("pages.pricingAuditComparisions.edit.error");
notification.error({ message, description });
},
});
const { form, formProps, saveButtonProps, onFinish, queryResult } = useForm({
id,
action: RESOURCES_ACTION_EDIT,
resource: RESOURCES.COMPARISONS,
onMutationSuccess: () => {
const message = translate("common.success");
const description = translate("pages.pricingAuditComparisions.edit.success");
notification.success({ message, description });
},
onMutationError: () => {
const message = translate("common.error");
const description = translate("pages.pricingAuditComparisions.edit.error");
notification.error({ message, description });
},
});
And want to check when is error to refetch data if is a 401
if (queryResult?.isError && queryResult?.isFetched && queryResult?.failureReason?.statusCode === 401) {
console.log('401');

refect()
}
if (queryResult?.isError && queryResult?.isFetched && queryResult?.failureReason?.statusCode === 401) {
console.log('401');

refect()
}
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @Sebastos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, it seems you are trying to refetch data when a 401 error occurs. However, the code snippet you provided does not include a refetch function. In the useForm hook, you can access the refetch function from the queryResult object. Here is an example of how you can do this:
const { form, formProps, saveButtonProps, onFinish, queryResult } = useForm({
id,
action: RESOURCES_ACTION_EDIT,
resource: RESOURCES.COMPARISONS,
onMutationSuccess: () => {
const message = translate("common.success");
const description = translate("pages.pricingAuditComparisions.edit.success");
notification.success({ message, description });
},
onMutationError: () => {
const message = translate("common.error");
const description = translate("pages.pricingAuditComparisions.edit.error");
notification.error({ message, description });
},
});

if (queryResult?.isError && queryResult?.isFetched && queryResult?.failureReason?.statusCode === 401) {
console.log('401');

queryResult.refetch();
}
const { form, formProps, saveButtonProps, onFinish, queryResult } = useForm({
id,
action: RESOURCES_ACTION_EDIT,
resource: RESOURCES.COMPARISONS,
onMutationSuccess: () => {
const message = translate("common.success");
const description = translate("pages.pricingAuditComparisions.edit.success");
notification.success({ message, description });
},
onMutationError: () => {
const message = translate("common.error");
const description = translate("pages.pricingAuditComparisions.edit.error");
notification.error({ message, description });
},
});

if (queryResult?.isError && queryResult?.isFetched && queryResult?.failureReason?.statusCode === 401) {
console.log('401');

queryResult.refetch();
}
Please note that this is based on the information provided in the knowledge sources and may need to be adjusted based on your specific use case. For more information on how to use the useForm hook in refine, you can refer to the official documentation (https://refine.dev/docs/api-references/hooks/form/useForm). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@689846866837373077> kapa.ai is still learning and improving, please let us know how it did by reacting below