From bef2c3fc15a4f7bd004736d0a3a2e8ca682d3b95 Mon Sep 17 00:00:00 2001 From: Gurikov Maxim Date: Sat, 23 Nov 2024 17:34:47 +0500 Subject: [PATCH 1/5] done task --- src/Pages/login-page/login-page.tsx | 6 ++--- src/Pages/offer-page/offer-page.tsx | 11 ++++++-- src/api/api.ts | 5 +++- src/components/offer/offer-card.tsx | 2 +- src/components/reviews/review-form.tsx | 18 ++++++++++++- src/components/reviews/reviews.tsx | 7 ++++- src/components/spinner/Spinner.tsx | 4 +-- src/components/spinner/spinner.css | 6 +++++ src/dataTypes/enums/api-routes.ts | 1 + src/dataTypes/enums/app-routes.ts | 1 + src/dataTypes/review.ts | 6 +++++ src/store/actions.ts | 37 ++++++++++++++++++++++++++ src/store/store.ts | 7 +++++ 13 files changed, 99 insertions(+), 12 deletions(-) diff --git a/src/Pages/login-page/login-page.tsx b/src/Pages/login-page/login-page.tsx index 96cd2f6..933830b 100644 --- a/src/Pages/login-page/login-page.tsx +++ b/src/Pages/login-page/login-page.tsx @@ -45,8 +45,7 @@ export function LoginPage(): React.JSX.Element { name="email" placeholder="Email" onChange={(event) => - setLoginInfo({ ...loginInfo, email: event.target.value }) - } + setLoginInfo({ ...loginInfo, email: event.target.value })} required /> @@ -61,8 +60,7 @@ export function LoginPage(): React.JSX.Element { setLoginInfo({ ...loginInfo, password: event.target.value, - }) - } + })} required /> diff --git a/src/Pages/offer-page/offer-page.tsx b/src/Pages/offer-page/offer-page.tsx index e23f346..33a3b79 100644 --- a/src/Pages/offer-page/offer-page.tsx +++ b/src/Pages/offer-page/offer-page.tsx @@ -4,7 +4,7 @@ import { OffersList } from '../../components/offer/offers-list.tsx'; import { Reviews } from '../../components/reviews/reviews.tsx'; import { Map } from '../../components/map/map.tsx'; import React, { useEffect } from 'react'; -import { useParams } from 'react-router-dom'; +import { Navigate, useParams } from 'react-router-dom'; import { OfferInsideItems } from '../../components/offer/offer-inside-items.tsx'; import { OfferHost } from '../../components/offer/offer-host.tsx'; import { capitalize, pluralizeAndCombine } from '../../utils/string-utils.ts'; @@ -15,9 +15,11 @@ import { store, useAppSelector } from '../../store/store.ts'; import { fetchNearbyOffers, fetchOffer, + fetchReviews, setCurrentOffer, } from '../../store/actions.ts'; import { Spinner } from '../../components/spinner/Spinner.tsx'; +import { AppRoutes } from '../../dataTypes/enums/app-routes.ts'; export function OfferPage(): React.JSX.Element { const offerId = useParams().id; @@ -25,12 +27,17 @@ export function OfferPage(): React.JSX.Element { store.dispatch(setCurrentOffer(null)); store.dispatch(fetchOffer(offerId!)); store.dispatch(fetchNearbyOffers(offerId!)); + store.dispatch(fetchReviews(offerId!)); }, [offerId]); const nearbyOffers = useAppSelector((state) => state.nearbyOffers).slice( 0, 3, ); const currentOffer = useAppSelector((state) => state.currentOffer); + const currentReviews = useAppSelector((state) => state.currentReviews); + if (currentOffer === undefined) { + return ; + } return (
@@ -89,7 +96,7 @@ export function OfferPage(): React.JSX.Element {

- + { setAuthorizationStatus(AuthorizationStatus.Unauthorized), ); } + if (error.response && error.response.status === 404) { + store.dispatch(setCurrentOffer(undefined)); + } throw error; }, ); diff --git a/src/components/offer/offer-card.tsx b/src/components/offer/offer-card.tsx index baed48e..2fa3889 100644 --- a/src/components/offer/offer-card.tsx +++ b/src/components/offer/offer-card.tsx @@ -40,7 +40,7 @@ export function OfferCard({ onMouseLeave={handleMouseLeave} className={cn( 'place-card', - { cities__card: isOnMainPage }, + { 'cities__card': isOnMainPage }, { 'near-places__card': !isOnMainPage }, )} > diff --git a/src/components/reviews/review-form.tsx b/src/components/reviews/review-form.tsx index e9a25ba..c9d0916 100644 --- a/src/components/reviews/review-form.tsx +++ b/src/components/reviews/review-form.tsx @@ -1,4 +1,6 @@ import { useState } from 'react'; +import { store, useAppSelector } from '../../store/store.ts'; +import { postReview } from '../../store/actions.ts'; type UserReview = { comment?: string; @@ -7,16 +9,29 @@ type UserReview = { export function ReviewForm(): React.JSX.Element { const [review, setReview] = useState(); + const offerId = useAppSelector((state) => state.currentOffer)!.id; const onRatingChange: React.ChangeEventHandler = ( event, ): void => setReview({ ...review, rating: +event.target.value }); const onCommentChange: React.ChangeEventHandler = ( event, ): void => setReview({ ...review, comment: event.target.value }); + const handleSubmit: React.MouseEventHandler = ( + event, + ): void => { + event.preventDefault(); + store.dispatch( + postReview({ + offerId: offerId, + rating: review?.rating || 5, + comment: review?.comment || '', + }), + ); + }; const isValid = review?.comment && review?.comment?.length >= 50 && review?.rating; return ( -
+ @@ -128,6 +143,7 @@ export function ReviewForm(): React.JSX.Element {