From dd9fb868550f73191a9aae9ee64cf1f560aba423 Mon Sep 17 00:00:00 2001 From: denispan <6231990@gmail.com> Date: Fri, 5 Apr 2024 10:25:40 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.tsx | 4 +- .../comments-list/comments-list.tsx | 37 ++++++++++++++++ .../{login-form => form-login}/login-form.tsx | 0 src/components/form-review/form-review.tsx | 40 ++++++++++++++--- src/components/header/header.tsx | 2 +- src/components/rating-star/rating-star.tsx | 12 +++-- src/components/review/review.tsx | 4 +- src/const.ts | 4 +- src/hooks/favorites.ts | 7 +++ src/pages/login/login.tsx | 2 +- src/pages/offer/offer.tsx | 27 +++++------- src/store/index.ts | 2 + src/store/slices/comments.ts | 19 ++++++-- src/store/slices/favorites.ts | 44 +++++++++++++++++++ src/store/thunks/comments.ts | 24 ++++++++++ src/store/thunks/offers.ts | 9 ++-- src/types/comment.ts | 9 +++- 17 files changed, 203 insertions(+), 43 deletions(-) create mode 100644 src/components/comments-list/comments-list.tsx rename src/components/{login-form => form-login}/login-form.tsx (100%) create mode 100644 src/hooks/favorites.ts create mode 100644 src/store/slices/favorites.ts create mode 100644 src/store/thunks/comments.ts diff --git a/src/app.tsx b/src/app.tsx index f2d1a12..c6f73d4 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,7 +1,7 @@ import Main from './pages/main/main.tsx'; import {BrowserRouter, Navigate, Route, Routes} from 'react-router-dom'; import NotFound from './pages/not-found/not-found.tsx'; -import {AppRoute, AuthStatus, CITIES, DEFAULT_CITY_SLUG} from './const.ts'; +import {AppRoute, CITIES, DEFAULT_CITY_SLUG} from './const.ts'; import Favorites from './pages/favorites/favorites.tsx'; import Offer from './pages/offer/offer.tsx'; import PrivateRoute from './components/private-route/private-route.tsx'; @@ -51,7 +51,7 @@ function App() { /> } + element={} /> { + fetchComments(offerId); + }, [postCommentStatus, offerId]); + + return ( + <> +

+ Reviews · {commentsCount} +

+
    + { + commentsCount > 0 && + comments.map((comment) => + ) + } +
+ + ); +} + +export default CommentsList; diff --git a/src/components/login-form/login-form.tsx b/src/components/form-login/login-form.tsx similarity index 100% rename from src/components/login-form/login-form.tsx rename to src/components/form-login/login-form.tsx diff --git a/src/components/form-review/form-review.tsx b/src/components/form-review/form-review.tsx index b0145b6..c1e6120 100644 --- a/src/components/form-review/form-review.tsx +++ b/src/components/form-review/form-review.tsx @@ -1,18 +1,44 @@ import {MIN_LENGTH_COMMENT, MIN_STARS_COMMENT, RATING_STARS} from '../../const.ts'; +import {StarTitle, StarValue} from '../rating-star/rating-star.tsx'; import RatingStar from '../rating-star/rating-star.tsx'; -import type {StarTitle} from '../rating-star/rating-star.tsx'; -import {FormEvent, useState} from 'react'; +import {FormEvent, useRef, useState} from 'react'; +import {useActionCreators} from '../../hooks/store.ts'; +import {commentsActions} from '../../store/slices/comments.ts'; +import {OfferFullInfo} from '../../types/offer.ts'; -function FormReview() { - const [ratingStar, setRatingStar] = useState(0); - const [textAreaValue, setTextAreaValue] = useState(''); +type FormReviewProps = { + offerId: OfferFullInfo['id']; +} + +type InitialForm = { + comment: string; + rating: StarValue; +} + +const INITIAL_FORM_VALUE: InitialForm = { + comment: '', + rating: RATING_STARS['unknown'], +} + +function FormReview({offerId}: FormReviewProps) { + const [textAreaValue, setTextAreaValue] = useState(INITIAL_FORM_VALUE.comment); + const [ratingStar, setRatingStar] = useState(INITIAL_FORM_VALUE.rating); + const {postComment} = useActionCreators(commentsActions); + const formRef = useRef(null); const onFormSubmit = (evt: FormEvent) => { evt.preventDefault(); + postComment({offerId, body: {comment: textAreaValue, rating: ratingStar}}) + .unwrap() + .then(() => { + setTextAreaValue(INITIAL_FORM_VALUE.comment); + setRatingStar(INITIAL_FORM_VALUE.rating); + }); }; return (
onFormSubmit(evt)} className="reviews__form form" action="#" @@ -20,19 +46,21 @@ function FormReview() { >
- {Object.entries(RATING_STARS).map(([starTitle, starValue]) => + {Object.entries(RATING_STARS).slice(1).map(([starTitle, starValue]) => ( ) )}