mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
feat(post form): add content length check
This commit is contained in:
@@ -154,4 +154,10 @@
|
|||||||
|
|
||||||
.uploadButton span {
|
.uploadButton span {
|
||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 5px;
|
||||||
|
color: red;
|
||||||
}
|
}
|
||||||
@@ -59,6 +59,19 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
const comment = useComment({ commentCid: postCid });
|
const comment = useComment({ commentCid: postCid });
|
||||||
const address = comment?.author?.address;
|
const address = comment?.author?.address;
|
||||||
|
|
||||||
|
const [lengthError, setLengthError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const checkContentLength = useRef(
|
||||||
|
_.debounce((content: string, t: Function) => {
|
||||||
|
const length = content.trim().length;
|
||||||
|
if (length > 2000) {
|
||||||
|
setLengthError(`${t('error')}: ${t('comment_field_too_long', { length })}`);
|
||||||
|
} else {
|
||||||
|
setLengthError(null);
|
||||||
|
}
|
||||||
|
}, 1000),
|
||||||
|
).current;
|
||||||
|
|
||||||
const resetFields = () => {
|
const resetFields = () => {
|
||||||
if (textRef.current) {
|
if (textRef.current) {
|
||||||
textRef.current.value = '';
|
textRef.current.value = '';
|
||||||
@@ -97,6 +110,9 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
const currentContent = textRef.current?.value.trim() || '';
|
const currentContent = textRef.current?.value.trim() || '';
|
||||||
const currentUrl = urlRef.current?.value.trim() || '';
|
const currentUrl = urlRef.current?.value.trim() || '';
|
||||||
|
|
||||||
|
checkContentLength.cancel();
|
||||||
|
setLengthError(null);
|
||||||
|
|
||||||
if (!currentTitle && !currentContent && !currentUrl) {
|
if (!currentTitle && !currentContent && !currentUrl) {
|
||||||
alert(t('empty_comment_alert'));
|
alert(t('empty_comment_alert'));
|
||||||
return;
|
return;
|
||||||
@@ -106,6 +122,11 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentContent.length > 2000) {
|
||||||
|
alert(t('error') + ': ' + t('field_too_long'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ((isInAllView || isInSubscriptionsView) && !publishPostOptions.subplebbitAddress) {
|
if ((isInAllView || isInSubscriptionsView) && !publishPostOptions.subplebbitAddress) {
|
||||||
alert(t('no_board_selected_warning'));
|
alert(t('no_board_selected_warning'));
|
||||||
return;
|
return;
|
||||||
@@ -190,12 +211,16 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
const handleContentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
const formattedContent = formatMarkdown(e.target.value);
|
const formattedContent = formatMarkdown(e.target.value);
|
||||||
isInPostView ? setPublishReplyOptions({ content: formattedContent }) : setPublishPostOptions({ content: formattedContent });
|
isInPostView ? setPublishReplyOptions({ content: formattedContent }) : setPublishPostOptions({ content: formattedContent });
|
||||||
|
checkContentLength(formattedContent, t);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onPublishReply = () => {
|
const onPublishReply = () => {
|
||||||
const currentContent = textRef.current?.value.trim() || '';
|
const currentContent = textRef.current?.value.trim() || '';
|
||||||
const currentUrl = urlRef.current?.value.trim() || '';
|
const currentUrl = urlRef.current?.value.trim() || '';
|
||||||
|
|
||||||
|
checkContentLength.cancel();
|
||||||
|
setLengthError(null);
|
||||||
|
|
||||||
if (!currentContent && !currentUrl) {
|
if (!currentContent && !currentUrl) {
|
||||||
alert(t('empty_comment_alert'));
|
alert(t('empty_comment_alert'));
|
||||||
return;
|
return;
|
||||||
@@ -206,6 +231,11 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentContent.length > 2000) {
|
||||||
|
alert(t('error') + ': ' + t('field_too_long'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
publishReply();
|
publishReply();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -316,6 +346,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
|
|||||||
<td>{t('comment')}</td>
|
<td>{t('comment')}</td>
|
||||||
<td>
|
<td>
|
||||||
<textarea cols={48} rows={4} wrap='soft' ref={textRef} onChange={handleContentChange} />
|
<textarea cols={48} rows={4} wrap='soft' ref={textRef} onChange={handleContentChange} />
|
||||||
|
{lengthError && <div className={styles.error}>{lengthError}</div>}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user