Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Created December 2, 2019 18:25
Show Gist options
  • Save jsoneaday/096d2e664158eff0af995c95b57fa91c to your computer and use it in GitHub Desktop.
Save jsoneaday/096d2e664158eff0af995c95b57fa91c to your computer and use it in GitHub Desktop.
useMutation demo
const [execPostThread] = useMutation<any, any>(PostThreadMutation, {
refetchQueries: [
{
query: QueryMe
}
]
});
useEffect(() => {
if (askToPostThread && allowPostThread) {
setAskToPostThread(false);
execPostThread({
variables: {
threadId: localThread !== null ? localThread.id : "0",
userId: userProfile ? userProfile!.id : "0",
title,
body,
videoUrl,
bounty,
categoryId: selectedCategory!.id
}
})
.then(({ data }) => {
if (data.postThread && data.postThread.id > 0) {
const dataPostThread = data.postThread;
let message = "Thread posted successfully";
if (postBtnLabel === "Edit") {
message = "Thread edited successfully";
}
setValidationSuccessMsg(message);
setLocalThread(dataPostThread);
setTitle(dataPostThread.title);
setBody(dataPostThread.body);
setBounty(dataPostThread.bounty);
setPostBtnLabel("Edit");
if (window.location.href.includes("/postthread")) {
setThreadUrl("/thread/" + dataPostThread.id);
} else {
setThreadUrl("");
}
}
})
.catch(err => {
setSummaryValidationErrors([cleanGraphQlError(err.message)]);
});
}
}, [askToPostThread, allowPostThread]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment