diff --git a/src/components/private-route/private-route.tsx b/src/components/private-route/private-route.tsx index 2016640..1f43f28 100644 --- a/src/components/private-route/private-route.tsx +++ b/src/components/private-route/private-route.tsx @@ -11,7 +11,7 @@ export function PrivateRouteAuthorized(props: PrivateRouteProps): JSX.Element { const {children} = props; const isAuthorized = useAppSelector((state) => state.authorizationStatus) === AuthorizationStatus.Authorized; return ( - isAuthorized + isAuthorized ? children : ); diff --git a/src/components/review-form/review-form.tsx b/src/components/review-form/review-form.tsx index 58a5b0a..52d3a62 100644 --- a/src/components/review-form/review-form.tsx +++ b/src/components/review-form/review-form.tsx @@ -1,6 +1,9 @@ import React, {JSX} from 'react'; import {useState} from 'react'; import {minCommentLength, maxCommentLength} from '../../const.ts'; +import {store} from '../../store'; +import {sendReview} from '../../store/api-actions.ts'; +import {useAppSelector} from '../../hooks'; export default function ReviewForm(): JSX.Element { const [formData, setFormData] = useState({ @@ -8,20 +11,45 @@ export default function ReviewForm(): JSX.Element { rating: 0 }); + const offerId = useAppSelector((state) => state.detailOffer)!.id; + const handleSubmit: React.FormEventHandler = (evt) => { + evt.preventDefault(); + store.dispatch( + sendReview({ + offerId, + rating: formData.rating, + comment: formData.review, + }), + ).then(() => { + setFormData({rating: 0, review: ''}); + }); + }; + const isValid = formData.review.length >= minCommentLength && formData.review.length <= maxCommentLength && formData.rating !== null; - const handleFieldChange = (evt: React.ChangeEvent) => { - const {name, value} = evt.target; - setFormData({...formData, [name]: value}); + const handleFieldChange = (evt: React.ChangeEvent) => { + const { name, value } = evt.target; + setFormData({ + ...formData, + [name]: name === 'rating' ? Number(value) : value, + }); }; function renderRatingInput(value: number, title: string) { return ( <> - +