import { forwardRef, useCallback, useEffect, useImperativeHandle, useState, } from 'react'; import {useAuth} from '@common/auth/use-auth'; import { CreateReviewPayload, useCreateReview, } from '@app/reviews/requests/use-create-review'; import {useForm} from 'react-hook-form'; import {Form} from '@common/ui/forms/form'; import {Avatar} from '@common/ui/images/avatar'; import {Trans} from '@common/i18n/trans'; import {StarSelector} from '@app/reviews/review-list/star-selector'; import {Button} from '@common/ui/buttons/button'; import {FormTextField} from '@common/ui/forms/input-field/text-field/text-field'; import {Reviewable} from '@app/reviews/reviewable'; import {toast} from '@common/ui/toast/toast'; import {message} from '@common/i18n/message'; import clsx from 'clsx'; import {Review} from '@app/titles/models/review'; export interface NewReviewFormActions { openReviewPanel: () => void; } interface Props { reviewable: Reviewable; currentReview?: Review; className?: string; disabled?: boolean; } export const NewReviewForm = forwardRef( ({reviewable, currentReview, className, disabled}, ref) => { const [isExpanded, setIsExpanded] = useState(false); const {user} = useAuth(); const form = useForm({ defaultValues: { score: 8, }, }); useEffect(() => { if (currentReview) { form.setValue('title', currentReview.title); form.setValue('body', currentReview.body); form.setValue('score', currentReview.score); } }, [form, currentReview]); const openReviewPanel = useCallback(() => { setIsExpanded(true); }, []); useImperativeHandle( ref, () => ({ openReviewPanel, }), [openReviewPanel], ); const createReview = useCreateReview(form); return (
{ if (disabled) return; createReview.mutate( { ...newValues, reviewable, }, { onSuccess: () => { toast(message('Review posted')); setIsExpanded(false); }, }, ); }} >
{user?.display_name} ), }} />
{ form.setValue('score', newScore); }} />
{!isExpanded && ( )}
{isExpanded && (
} labelSuffix={} autoFocus minLength={10} required /> } labelSuffix={} inputElementType="textarea" rows={5} minLength={100} required />
)}
); }, );