MCPcopy Create free account
hub / github.com/code100x/cms / CommentInputForm

Function CommentInputForm

src/components/comment/CommentInputForm.tsx:10–123  ·  view source on GitHub ↗
({
  contentId,
  parentId = undefined,
}: {
  contentId: number;
  parentId?: number | undefined;
})

Source from the content-addressed store, hash-verified

8import { usePathname } from 'next/navigation';
9
10const CommentInputForm = ({
11 contentId,
12 parentId = undefined,
13}: {
14 contentId: number;
15 parentId?: number | undefined;
16}) => {
17 const currentPath = usePathname();
18 const [isButtonDisabled, setButtonDisabled] = useState(true);
19 const [commentText, setCommentText] = useState('');
20 const formRef = React.useRef<HTMLFormElement>(null);
21 const textareaRef = React.useRef<HTMLTextAreaElement>(null);
22 const { execute, isLoading, fieldErrors } = useAction(createMessage, {
23 onSuccess: () => {
24 toast.success(`${parentId ? 'Replied' : 'Commented'}`);
25 formRef.current?.reset();
26 },
27 onError: (error) => {
28 toast.error(error);
29 },
30 });
31 const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
32 e.preventDefault();
33 const formData = new FormData(e.target as HTMLFormElement);
34 const content = formData.get('content') as string;
35
36 execute({
37 content,
38 contentId,
39 parentId,
40 currentPath,
41 });
42 setCommentText('');
43 };
44
45 const isAllSpaces = (str: string): boolean => (/^\s*$/).test(str);
46
47 const isCommentValid = () => {
48 return !isAllSpaces(commentText);
49 };
50
51 // Function to adjust the height of the textarea
52 const adjustTextareaHeight = () => {
53 if (textareaRef.current) {
54 textareaRef.current.style.height = 'auto'; // Reset the height
55 textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; // Set the height based on scroll height
56 }
57 };
58
59 useEffect(() => {
60 if (!isCommentValid() || isLoading) {
61 setButtonDisabled(true);
62 } else {
63 setButtonDisabled(false);
64 }
65 }, [commentText]);
66
67 // Effect to handle the initial and dynamic height adjustment

Callers

nothing calls this directly

Calls 3

useActionFunction · 0.90
isCommentValidFunction · 0.85
adjustTextareaHeightFunction · 0.85

Tested by

no test coverage detected