From a0d35dfd61aea22346853fa563a2bc2e8b974fc3 Mon Sep 17 00:00:00 2001 From: plebbitor Date: Sat, 22 Apr 2023 17:37:53 +0200 Subject: [PATCH] fix posting logic --- src/components/ReplyModal.jsx | 23 +++++------------------ src/components/views/Catalog.jsx | 4 ++-- src/components/views/Thread.jsx | 29 ++++++++--------------------- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/src/components/ReplyModal.jsx b/src/components/ReplyModal.jsx index bf7d0dda..7b7c8499 100644 --- a/src/components/ReplyModal.jsx +++ b/src/components/ReplyModal.jsx @@ -31,8 +31,6 @@ const ReplyModal = ({ isOpen, closeModal }) => { const commentRef = useRef(); const linkRef = useRef(); - const onChallengeVerificationRef = useRef(); - const [errorMessage, setErrorMessage] = useState(null); const [successMessage] = useState(null); useError(errorMessage, [errorMessage]); @@ -68,7 +66,7 @@ const ReplyModal = ({ isOpen, closeModal }) => { const [publishCommentOptions, setPublishCommentOptions] = useState({ subplebbitAddress: selectedAddress, onChallenge, - onChallengeVerification: onChallengeVerificationRef.current, + onChallengeVerification, onError: (error) => { setErrorMessage(error); }, @@ -84,9 +82,6 @@ const ReplyModal = ({ isOpen, closeModal }) => { localStorage.setItem("toastMessage", `Comment pending with index ${index}.`); } }, [index]); - - - onChallengeVerificationRef.current = onChallengeVerification; const resetFields = () => { @@ -96,15 +91,6 @@ const ReplyModal = ({ isOpen, closeModal }) => { }; - useEffect(() => { - setPublishCommentOptions((prevPublishCommentOptions) => ({ - ...prevPublishCommentOptions, - subplebbitAddress: selectedAddress, - onChallengeVerification: onChallengeVerificationRef.current, - })); - }, [selectedAddress]); - - const handleSubmit = async (event) => { event.preventDefault(); @@ -143,9 +129,10 @@ const ReplyModal = ({ isOpen, closeModal }) => { challengeImg.onload = () => { setIsCaptchaOpen(true); - const handleKeyDown = (event) => { + const handleKeyDown = async (event) => { if (event.key === 'Enter') { - resolve(captchaResponse); + const currentCaptchaResponse = captchaResponse; + resolve(currentCaptchaResponse); setIsCaptchaOpen(false); document.removeEventListener('keydown', handleKeyDown); event.preventDefault(); @@ -155,7 +142,7 @@ const ReplyModal = ({ isOpen, closeModal }) => { setCaptchaResponse(''); document.addEventListener('keydown', handleKeyDown); - useGeneralStore.getState().setResolveCaptchaPromise(resolve); + setResolveCaptchaPromise(resolve); }; challengeImg.onerror = () => { diff --git a/src/components/views/Catalog.jsx b/src/components/views/Catalog.jsx index a9ae3001..639053d8 100644 --- a/src/components/views/Catalog.jsx +++ b/src/components/views/Catalog.jsx @@ -20,7 +20,7 @@ import useSuccess from '../../hooks/useSuccess'; const Catalog = () => { const { - setCaptchaResponse, + captchaResponse, setCaptchaResponse, setChallengesArray, defaultSubplebbits, setIsCaptchaOpen, @@ -175,7 +175,7 @@ const Catalog = () => { const handleKeyDown = async (event) => { if (event.key === 'Enter') { - const currentCaptchaResponse = useGeneralStore.getState().captchaResponse; + const currentCaptchaResponse = captchaResponse; resolve(currentCaptchaResponse); setIsCaptchaOpen(false); document.removeEventListener('keydown', handleKeyDown); diff --git a/src/components/views/Thread.jsx b/src/components/views/Thread.jsx index fb204a83..e1ee2b8d 100644 --- a/src/components/views/Thread.jsx +++ b/src/components/views/Thread.jsx @@ -47,8 +47,6 @@ const Thread = () => { const commentRef = useRef(); const linkRef = useRef(); - const onChallengeVerificationRef = useRef(); - const [isReplyOpen, setIsReplyOpen] = useState(false); const navigate = useNavigate(); const [prevScrollPos, setPrevScrollPos] = useState(0); @@ -121,7 +119,7 @@ const Thread = () => { const [publishCommentOptions, setPublishCommentOptions] = useState({ subplebbitAddress: selectedAddress, onChallenge, - onChallengeVerification: onChallengeVerificationRef.current, + onChallengeVerification, onError: (error) => { setErrorMessage(error); }, @@ -137,9 +135,6 @@ const Thread = () => { setSuccessMessage('Comment pending with index ' + index + '.'); } }, [index]); - - - onChallengeVerificationRef.current = onChallengeVerification; const resetFields = () => { @@ -149,15 +144,6 @@ const Thread = () => { }; - useEffect(() => { - setPublishCommentOptions((prevPublishCommentOptions) => ({ - ...prevPublishCommentOptions, - subplebbitAddress: selectedAddress, - onChallengeVerification: onChallengeVerificationRef.current, - })); - }, [selectedAddress]); - - const handleSubmit = async (event) => { event.preventDefault(); @@ -195,9 +181,10 @@ const Thread = () => { challengeImg.onload = () => { setIsCaptchaOpen(true); - const handleKeyDown = (event) => { + const handleKeyDown = async (event) => { if (event.key === 'Enter') { - resolve(captchaResponse); + const currentCaptchaResponse = captchaResponse; + resolve(currentCaptchaResponse); setIsCaptchaOpen(false); document.removeEventListener('keydown', handleKeyDown); event.preventDefault(); @@ -562,10 +549,10 @@ const Thread = () => { ) : ( - {account.author.address.slice(0, 10) + "(...)"} + {account?.author?.address.slice(0, 10) + "(...)"} ) } @@ -795,10 +782,10 @@ const Thread = () => { ) : ( - {account.author.address.slice(0, 6) + "(...)"} + {account?.author?.address.slice(0, 6) + "(...)"} ) }