Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added Max Length Message Validation on File Upload's Description Input #894

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions packages/react/src/views/AttachmentPreview/AttachmentPreview.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import React, { useContext, useState, useRef } from 'react';
import { css } from '@emotion/react';
import { Box, Icon, Button, Input, Modal } from '@embeddedchat/ui-elements';
import {
Box,
Icon,
Button,
Input,
Modal,
useTheme,
} from '@embeddedchat/ui-elements';
import useAttachmentWindowStore from '../../store/attachmentwindow';
import CheckPreviewType from './CheckPreviewType';
import RCContext from '../../context/RCInstance';
import { useMessageStore, useMemberStore } from '../../store';
import useSettingsStore from '../../store/settingsStore';
import getAttachmentPreviewStyles from './AttachmentPreview.styles';
import { parseEmoji } from '../../lib/emoji';
import MembersList from '../Mentions/MembersList';
Expand All @@ -13,6 +21,7 @@ import useSearchMentionUser from '../../hooks/useSearchMentionUser';

const AttachmentPreview = () => {
const { RCInstance, ECOptions } = useContext(RCContext);
const { theme } = useTheme();
const styles = getAttachmentPreviewStyles();

const toggle = useAttachmentWindowStore((state) => state.toggle);
Expand All @@ -24,6 +33,7 @@ const AttachmentPreview = () => {
const [filteredMembers, setFilteredMembers] = useState([]);
const [mentionIndex, setMentionIndex] = useState(-1);
const [startReadMentionUser, setStartReadMentionUser] = useState(false);
const [isMsgLong, setIsMsgLong] = useState(false);

const [fileName, setFileName] = useState(data?.name);

Expand All @@ -45,12 +55,6 @@ const AttachmentPreview = () => {
setShowMembersList
);

const handleFileDescription = (e) => {
const description = e.target.value;
messageRef.current.value = parseEmoji(description);
searchMentionUser(description);
};

const submit = async () => {
setIsPending(true);
await RCInstance.sendAttachment(
Expand All @@ -65,6 +69,29 @@ const AttachmentPreview = () => {
setIsPending(false);
}
};

const msgMaxLength = useSettingsStore((state) => state.messageLimit);
const descAboveMaxLengthMsg = `Cannot upload file, description is over the ${msgMaxLength} character limit`;

const checkIfMsgLong = (description) => {
if (description.length > msgMaxLength) {
setIsMsgLong(true);
return;
}
submit();
};

const handleFileDescription = (e) => {
const description = e.target.value;
messageRef.current.value = parseEmoji(description);
if (isMsgLong) {
if (description.length <= msgMaxLength) {
setIsMsgLong(false);
}
}
searchMentionUser(description);
};

return (
<Modal onClose={toggle}>
<Modal.Header>
Expand Down Expand Up @@ -147,10 +174,29 @@ const AttachmentPreview = () => {
onChange={(e) => {
handleFileDescription(e);
}}
css={styles.input}
css={css`
${styles.input};
border-color: ${isMsgLong
? theme.colors.destructive
: null};
color: ${isMsgLong ? theme.colors.destructive : null};
`}
placeholder="Description"
ref={messageRef}
/>
<Box>
{isMsgLong && (
<Box
css={css`
color: ${theme.colors.destructive};
font-size: 12px;
margin-top: 5px;
`}
>
{descAboveMaxLengthMsg}
</Box>
)}
</Box>
</Box>
</Box>
</Box>
Expand All @@ -168,7 +214,7 @@ const AttachmentPreview = () => {
<Button
disabled={isPending}
onClick={() => {
submit();
checkIfMsgLong(messageRef.current.value);
}}
>
{isPending ? 'Sending...' : 'Send'}
Expand Down
Loading