Skip to content

Commit

Permalink
Merge branch 'main' into fix-svg-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
devsargam authored Oct 8, 2024
2 parents a671b2c + 86bb203 commit 125fec0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 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
11 changes: 2 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 125fec0

Please sign in to comment.