import { Form, MarkdownInput, SubmitButton } from '../components/form' import * as Yup from 'yup' import { gql, useMutation } from '@apollo/client' import styles from './reply.module.css' import { COMMENTS } from '../fragments/comments' import { useMe } from './me' import TextareaAutosize from 'react-textarea-autosize' import { useEffect, useState } from 'react' import Link from 'next/link' import FeeButton from './fee-button' export const CommentSchema = Yup.object({ text: Yup.string().required('required').trim() }) export function ReplyOnAnotherPage ({ parentId }) { return ( reply on another page ) } export default function Reply ({ parentId, meComments, onSuccess, replyOpen }) { const [reply, setReply] = useState(replyOpen) const me = useMe() const [hasImgLink, setHasImgLink] = useState() useEffect(() => { setReply(replyOpen || !!localStorage.getItem('reply-' + parentId + '-' + 'text')) }, []) const [createComment] = useMutation( gql` ${COMMENTS} mutation createComment($text: String!, $parentId: ID!) { createComment(text: $text, parentId: $parentId) { ...CommentFields comments { ...CommentsRecursive } } }`, { update (cache, { data: { createComment } }) { cache.modify({ id: `Item:${parentId}`, fields: { comments (existingCommentRefs = []) { const newCommentRef = cache.writeFragment({ data: createComment, fragment: COMMENTS, fragmentName: 'CommentsRecursive' }) return [newCommentRef, ...existingCommentRefs] }, ncomments (existingNComments = 0) { return existingNComments + 1 }, meComments (existingMeComments = 0) { return existingMeComments + 1 } } }) } } ) // const cost = me?.freeComments ? 0 : Math.pow(10, meComments) return (
{replyOpen ?
: (
setReply(!reply)} > {reply ? 'cancel' : 'reply'}
)}
{ const { error } = await createComment({ variables: { ...values, parentId } }) if (error) { throw new Error({ message: error.toString() }) } resetForm({ text: '' }) setReply(replyOpen || false) setHasImgLink(false) }} storageKeyPrefix={'reply-' + parentId} > {me.freeComments} free comments left : null} /> {reply &&
}
) } export function ReplySkeleton () { return (
) }