Skip to content

Commit

Permalink
fix(core): add fallback mutation function for useOnError (#5883)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemir authored Apr 24, 2024
1 parent 1c9a95f commit 0a76576
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
7 changes: 7 additions & 0 deletions .changeset/dull-spies-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/core": patch
---

fix: development errors being logged when `useOnError` is called without an auth provider

When there's no `authProvider` set, the `useOnError` hook will log `"no mutationFn found"` to the console in development because of missing `onError` property. This PR fixes the issue by providing a dummy `onError` function when `authProvider` is not set.
38 changes: 22 additions & 16 deletions packages/core/src/hooks/auth/useOnError/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,30 @@ export function useOnError({
v3LegacyAuthProviderCompatible: Boolean(v3LegacyAuthProviderCompatible),
});

const mutation = useMutation({
const mutation = useMutation<OnErrorResponse, unknown, unknown, unknown>({
mutationKey: keys().auth().action("onError").get(preferLegacyKeys),
mutationFn: onErrorFromContext,
onSuccess: ({ logout: shouldLogout, redirectTo }) => {
if (shouldLogout) {
logout({ redirectPath: redirectTo });
return;
}

if (redirectTo) {
if (routerType === "legacy") {
replace(redirectTo);
} else {
go({ to: redirectTo, type: "replace" });
...(onErrorFromContext
? {
mutationFn: onErrorFromContext,
onSuccess: ({ logout: shouldLogout, redirectTo }) => {
if (shouldLogout) {
logout({ redirectPath: redirectTo });
return;
}

if (redirectTo) {
if (routerType === "legacy") {
replace(redirectTo);
} else {
go({ to: redirectTo, type: "replace" });
}
return;
}
},
}
return;
}
},
: {
mutationFn: () => ({}) as Promise<OnErrorResponse>,
}),
meta: {
...getXRay("useOnError", preferLegacyKeys),
},
Expand Down

0 comments on commit 0a76576

Please sign in to comment.