You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the best approach to handle setting useMutation url query values? I noticed you have variables, but those seem to only work with the mutationFn, and I have a component that I re-use where I dont implement that on case by case basis.
const useMutation = <Result, Variables>(
url: string,
options?: Options<Result, Variables>,
method?: "POST" | "PUT" | "DELETE"
) => {
const { baseUrl } = useEnv();
const { error, accessForbidden } = useMessaging();
const { isLoaded, userId, sessionId, getToken } = useAuth();
const [isValidationError, setIsValidationError] = React.useState<
boolean | undefined
>();
const mutationFn = async (variables: Variables) => {
setIsValidationError(false);
const r = await fetch(`${baseUrl}${url}/`, {
body: JSON.stringify(variables),
method: method || "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${await getToken()}`,
},
});
if (r.status === 400) {
var t = JSON.parse(await r.text(), reviveDateTime);
if (t.errors.GENERAL) {
setIsValidationError(true);
error(t.errors.GENERAL[0]);
return;
}
}
// did we get a good status back?
if (!r.ok) {
error("There was an unexpected server error, please try again");
throw "Error";
}
return await JSON.parse(await r.text(), reviveDateTime);
};
return {
isValidationError,
...useMutationOriginal<Result, unknown, Variables>(mutationFn, options),
};
};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
What is the best approach to handle setting useMutation url query values? I noticed you have variables, but those seem to only work with the mutationFn, and I have a component that I re-use where I dont implement that on case by case basis.
Beta Was this translation helpful? Give feedback.
All reactions