Skip to content

Commit

Permalink
Added checks so that comments are not blank. (#1441)
Browse files Browse the repository at this point in the history
* added checks for blank comments

* added checks for blank comments
  • Loading branch information
hamzahshahbazkhan authored Oct 8, 2024
1 parent e1d7133 commit 86bb203
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/components/comment/CommentInputForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { Button } from '../ui/button';
import { useAction } from '@/hooks/useAction';
import { createMessage } from '@/actions/comment';
Expand All @@ -15,6 +15,8 @@ const CommentInputForm = ({
parentId?: number | undefined;
}) => {
const currentPath = usePathname();
const [isButtonDisabled, setButtonDisabled] = useState(true);
const [commentText, setCommentText] = useState('');
const formRef = React.useRef<HTMLFormElement>(null);
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
const { execute, isLoading, fieldErrors } = useAction(createMessage, {
Expand All @@ -29,7 +31,6 @@ const CommentInputForm = ({
const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);

const content = formData.get('content') as string;

execute({
Expand All @@ -38,7 +39,14 @@ const CommentInputForm = ({
parentId,
currentPath,
});
setCommentText('');
};

const isAllSpaces = (str: string): boolean => /^\s*$/.test(str);

const isCommentValid = () => {
return !isAllSpaces(commentText);
}

// Function to adjust the height of the textarea
const adjustTextareaHeight = () => {
Expand All @@ -48,6 +56,14 @@ const CommentInputForm = ({
}
};

useEffect(() => {
if (!isCommentValid() || isLoading) {
setButtonDisabled(true)
} else {
setButtonDisabled(false)
}
}, [commentText])

// Effect to handle the initial and dynamic height adjustment
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
Expand Down Expand Up @@ -78,9 +94,6 @@ const CommentInputForm = ({
};
}, []);

const handleTextChange = () => {
adjustTextareaHeight();
};
return (
<form
className="flex flex-col gap-4 rounded-xl"
Expand All @@ -90,15 +103,21 @@ const CommentInputForm = ({
<textarea
ref={textareaRef}
id="content"
rows = {1}
name="content"
className="w-full resize-none border-b border-primary/25 bg-transparent p-4 focus:outline-none focus:ring-0"
placeholder={parentId ? 'Add a reply...' : 'Add a comment...'}
onChange={handleTextChange} // Adjust height on text change
onChange={(e) => {
adjustTextareaHeight();
setCommentText(e.target.value);
}} // Adjust height on text change
/>
<FormErrors id="content" errors={fieldErrors} />
<Button type="submit" disabled={isLoading} className="w-fit">

<Button type="submit" disabled={isButtonDisabled} className="w-fit">
{parentId ? 'Reply' : 'Comment'}
</Button>

</form>
);
};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
}

0 comments on commit 86bb203

Please sign in to comment.