fix(Post Form): use defaultValue for Name when displayName is defined

This commit is contained in:
plebeius.eth
2023-09-22 21:43:10 +02:00
parent a7a845eada
commit c779c34243
4 changed files with 276 additions and 298 deletions
+36 -58
View File
@@ -8,10 +8,10 @@ import useError from '../../hooks/useError';
import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
import useGeneralStore from '../../hooks/stores/useGeneralStore';
const ReplyModal = ({ isOpen, closeModal }) => {
const {
captchaResponse, setCaptchaResponse,
captchaResponse,
setCaptchaResponse,
setChallengesArray,
setIsCaptchaOpen,
setPendingComment,
@@ -22,9 +22,10 @@ const ReplyModal = ({ isOpen, closeModal }) => {
selectedParentCid,
selectedShortCid,
selectedStyle,
selectedText, setSelectedText,
selectedText,
setSelectedText,
triggerInsertion,
} = useGeneralStore(state => state);
} = useGeneralStore((state) => state);
const { anonymousMode } = useAnonModeStore();
@@ -43,7 +44,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
useAnonMode(selectedParentCid, anonymousMode && executeAnonMode);
useEffect(() => {
const handleResize = () => setIsMobile(window.innerWidth <= 480);
window.addEventListener('resize', handleResize);
@@ -54,7 +54,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
};
}, [setIsMobile]);
const onModalOpen = () => {
if (commentRef.current) {
if (selectedText) {
@@ -66,13 +65,12 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
};
const insertAtCursor = (inputElement, valueToInsert) => {
const startPos = inputElement.selectionStart || inputElement.value.length;
const endPos = startPos + valueToInsert.length;
inputElement.setRangeText(valueToInsert, startPos, startPos, 'end');
inputElement.setSelectionRange(endPos, endPos);
}
};
useEffect(() => {
if (replyQuoteCid && commentRef.current) {
@@ -81,13 +79,11 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, [triggerInsertion, replyQuoteCid]);
const getSelectedText = useCallback(() => {
const text = document.getSelection().toString();
setSelectedText(text ? `>${text}\n` : '');
}, [setSelectedText]);
useEffect(() => {
if (isOpen) {
setTimeout(getSelectedText, 0);
@@ -96,7 +92,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, [isOpen, getSelectedText, setSelectedText]);
const onChallengeVerification = (challengeVerification) => {
if (challengeVerification.challengeSuccess === true) {
return;
@@ -106,23 +101,21 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
};
const onChallenge = async (challenges, comment) => {
setPendingComment(comment);
let challengeAnswers = [];
try {
challengeAnswers = await getChallengeAnswersFromUser(challenges)
}
catch (error) {
setNewErrorMessage(error.message); console.log(error);
challengeAnswers = await getChallengeAnswersFromUser(challenges);
} catch (error) {
setNewErrorMessage(error.message);
console.log(error);
}
if (challengeAnswers) {
await comment.publishChallengeAnswers(challengeAnswers)
await comment.publishChallengeAnswers(challengeAnswers);
}
};
useEffect(() => {
setPublishCommentOptions((prevPublishCommentOptions) => ({
...prevPublishCommentOptions,
@@ -130,17 +123,16 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}));
}, [selectedAddress]);
const [publishCommentOptions, setPublishCommentOptions] = useState({
subplebbitAddress: selectedAddress,
onChallenge,
onChallengeVerification,
onError: (error) => {
setNewErrorMessage(error.message); console.log(error);
setNewErrorMessage(error.message);
console.log(error);
},
});
const { publishComment, index } = usePublishComment(publishCommentOptions);
useEffect(() => {
@@ -149,7 +141,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, [index, setPendingCommentIndex]);
const resetFields = useCallback(() => {
if (nameRef.current) {
nameRef.current.value = '';
@@ -162,15 +153,11 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, []);
const handleSubmit = async (event) => {
event.preventDefault();
if (
commentRef.current.value === "" &&
linkRef.current.value === ""
) {
setNewErrorMessage("Please enter a comment or link.");
if (commentRef.current.value === '' && linkRef.current.value === '') {
setNewErrorMessage('Please enter a comment or link.');
return;
}
@@ -178,7 +165,7 @@ const ReplyModal = ({ isOpen, closeModal }) => {
...prevPublishCommentOptions,
author: {
displayName: nameRef.current.value || undefined,
...(anonymousMode ? {} : {address: account?.author.address}),
...(anonymousMode ? {} : { address: account?.author.address }),
},
content: commentRef.current.value || undefined,
link: linkRef.current.value || undefined,
@@ -188,7 +175,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
setTriggerPublishComment(true);
};
const updateSigner = useCallback(async () => {
if (anonymousMode) {
setExecuteAnonMode(true);
@@ -204,13 +190,13 @@ const ReplyModal = ({ isOpen, closeModal }) => {
const signerPrivateKey = storedSigners[selectedParentCid].privateKey;
try {
signer = await account?.plebbit.createSigner({type: 'ed25519', privateKey: signerPrivateKey});
signer = await account?.plebbit.createSigner({ type: 'ed25519', privateKey: signerPrivateKey });
} catch (error) {
console.log(error);
}
}
setPublishCommentOptions(prevPublishCommentOptions => {
setPublishCommentOptions((prevPublishCommentOptions) => {
const newPublishCommentOptions = {
...prevPublishCommentOptions,
signer,
@@ -235,7 +221,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, [updateSigner, anonymousMode]);
useEffect(() => {
if (publishCommentOptions && triggerPublishComment) {
(async () => {
@@ -248,7 +233,6 @@ const ReplyModal = ({ isOpen, closeModal }) => {
}
}, [publishCommentOptions, triggerPublishComment, publishComment, resetFields, closeModal]);
const getChallengeAnswersFromUser = async (challenges) => {
setChallengesArray(challenges);
@@ -283,49 +267,43 @@ const ReplyModal = ({ isOpen, closeModal }) => {
});
};
return (
<StyledModal
isOpen={isOpen}
onAfterOpen={onModalOpen}
onRequestClose={closeModal}
contentLabel="Reply Modal"
contentLabel='Reply Modal'
shouldCloseOnEsc={true}
shouldCloseOnOverlayClick={isMobile}
selectedStyle={selectedStyle}
overlayClassName="overlay"
style={isMobile ? ({ overlay: { backgroundColor: "rgba(0,0,0,.25)" }}) : ({ overlay: { backgroundColor: "rgba(0,0,0,0)" }})
}
overlayClassName='overlay'
style={isMobile ? { overlay: { backgroundColor: 'rgba(0,0,0,.25)' } } : { overlay: { backgroundColor: 'rgba(0,0,0,0)' } }}
>
<Draggable handle=".modal-header" nodeRef={nodeRef} disabled={isMobile}>
<div className="modal-content" ref={nodeRef}>
<div className="modal-header">
<Draggable handle='.modal-header' nodeRef={nodeRef} disabled={isMobile}>
<div className='modal-content' ref={nodeRef}>
<div className='modal-header'>
Reply to c/{selectedShortCid}
<button className="icon" onClick={() => closeModal()} title="close" />
<button className='icon' onClick={() => closeModal()} title='close' />
</div>
<div id="form">
<div id='form'>
<div>
{account && account?.author && account?.author.displayName ? (
<input id="name" type="text" value={account?.author?.displayName} ref={nameRef} disabled />
{account?.author.displayName ? (
<input id='name' type='text' defaultValue={account?.author?.displayName} ref={nameRef} />
) : (
<input id="name" type="text" placeholder="Anonymous" ref={nameRef} />
<input id='name' type='text' placeholder='Anonymous' ref={nameRef} />
)}
</div>
<div>
<input id="name" type="text" placeholder="Embed link" ref={linkRef} />
<input id='name' type='text' placeholder='Embed link' ref={linkRef} />
</div>
<div className="textarea-wrapper">
<span className="fixed-text">{`c/${selectedShortCid}`}</span>
<textarea className="textarea"
rows="4"
placeholder="Comment"
defaultValue={selectedText}
wrap="soft"
ref={commentRef}
/>
<div className='textarea-wrapper'>
<span className='fixed-text'>{`c/${selectedShortCid}`}</span>
<textarea className='textarea' rows='4' placeholder='Comment' defaultValue={selectedText} wrap='soft' ref={commentRef} />
</div>
<div>
<button id="next" onClick={handleSubmit}>Post</button>
<button id='next' onClick={handleSubmit}>
Post
</button>
</div>
</div>
</div>
+2 -2
View File
@@ -868,8 +868,8 @@ const Board = () => {
<tr data-type='Name'>
<td id='td-name'>Name</td>
<td>
{account && account?.author && account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} value={account?.author?.displayName} ref={nameRef} disabled />
{account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} defaultValue={account?.author?.displayName} ref={nameRef} />
) : (
<input name='name' type='text' placeholder='Anonymous' tabIndex={1} ref={nameRef} />
)}
+2 -2
View File
@@ -1637,8 +1637,8 @@ const Catalog = () => {
<tr data-type='Name'>
<td id='td-name'>Name</td>
<td>
{account && account?.author && account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} value={account?.author?.displayName} ref={nameRef} disabled />
{account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} defaultValue={account?.author?.displayName} ref={nameRef} />
) : (
<input name='name' type='text' placeholder='Anonymous' tabIndex={1} ref={nameRef} />
)}
+2 -2
View File
@@ -839,8 +839,8 @@ const Thread = () => {
<tr data-type='Name'>
<td id='td-name'>Name</td>
<td>
{account && account?.author && account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} value={account?.author?.displayName} ref={nameRef} disabled />
{account?.author.displayName ? (
<input name='name' type='text' tabIndex={1} defaultValue={account?.author?.displayName} ref={nameRef} />
) : (
<input name='name' type='text' placeholder='Anonymous' tabIndex={1} ref={nameRef} />
)}