diff --git a/src/views/submit/submit.tsx b/src/views/submit/submit.tsx index 8f8df37c..78329e18 100644 --- a/src/views/submit/submit.tsx +++ b/src/views/submit/submit.tsx @@ -1,4 +1,4 @@ -import { useEffect } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { Link, useNavigate, useParams } from 'react-router-dom'; import { usePublishComment, useSubplebbit } from '@plebbit/plebbit-react-hooks'; import { useTranslation } from 'react-i18next'; @@ -55,32 +55,52 @@ const Submit = () => { const paramsSubplebbitAddress = params.subplebbitAddress; const subplebbit = useSubplebbit({ subplebbitAddress: paramsSubplebbitAddress }); const navigate = useNavigate(); + const [readyToPublish, setReadyToPublish] = useState(false); - const { subplebbitAddress, title, link, publishCommentOptions, setSubmitStore, resetSubmitStore } = useSubmitStore(); + const titleRef = useRef(null); + const linkRef = useRef(null); + const contentRef = useRef(null); + const subplebbitAddressRef = useRef(null); + + const { subplebbitAddress, publishCommentOptions, setSubmitStore, resetSubmitStore } = useSubmitStore(); const { index, publishComment } = usePublishComment(publishCommentOptions); - + const onPublish = () => { - if (!title) { + if (!titleRef.current?.value) { alert(`Missing title`); return; } - if (link && !isValidURL(link)) { + if (linkRef.current?.value && !isValidURL(linkRef.current?.value)) { alert(`Invalid URL`); return; } - if (!subplebbitAddress) { + if (!subplebbitAddressRef.current?.value) { alert(`Missing community address`); return; } - if (!isValidENS(subplebbitAddress) && !isValidIPFS(subplebbitAddress)) { + if (!isValidENS(subplebbitAddressRef.current?.value) && !isValidIPFS(subplebbitAddressRef.current?.value)) { alert(`Invalid community address`); return; } - publishComment(); + setSubmitStore({ + subplebbitAddress: subplebbitAddressRef.current?.value, + title: titleRef.current?.value, + content: contentRef.current?.value || undefined, + link: linkRef.current?.value || undefined, + }); + + setReadyToPublish(true); }; + useEffect(() => { + if (readyToPublish) { + publishComment(); + setReadyToPublish(false); + } + }, [readyToPublish, publishComment]); + const subLocation = ( e.preventDefault()}> {subplebbit?.title || subplebbit?.shortAddress} @@ -110,7 +130,7 @@ const Submit = () => { setSubmitStore({ link: e.target.value || undefined, subplebbitAddress: !subplebbitAddress ? params.subplebbitAddress : undefined })} + ref={linkRef} />
{t('submit_url_description')}
@@ -120,7 +140,7 @@ const Submit = () => {