Created
December 2, 2019 18:25
-
-
Save jsoneaday/096d2e664158eff0af995c95b57fa91c to your computer and use it in GitHub Desktop.
useMutation demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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