import { useState } from 'react' import { Paper, Flex, Textarea, ActionIcon } from '@mantine/core' import { useForm, hasLength } from '@mantine/form' import { IconWriting } from '@tabler/icons-react' import { useCreatePostCommentMutation } from '../app/api/comments' import type { Comment, Post } from '../app/types/common' import type { CreateCommentData } from '../app/types/comments' const CreateComment = ({ post, addNewComment }: { post: Post, addNewComment: (comment: Comment) => void }) => { const [errorMsg, setErrorMsg] = useState('') const [createComment, { isLoading }] = useCreatePostCommentMutation() const submitComment = async (values: CreateCommentData) => { await createComment({ postId: post.id, data: values }).unwrap().then((comment) => { // Display the new comment addNewComment(comment) setErrorMsg('') }).catch((error) => { if (!error.data) { setErrorMsg('Failed to access the API') } else { setErrorMsg(error.data.msg) } }) } const commentForm = useForm({ initialValues: { message: '', }, validate: { message: hasLength({ min: 3, max: 512 }, 'Message must be between 3 and 512 characters'), } }) return (