From d020f49286cddcec255f6c5e0e1a025b2a512984 Mon Sep 17 00:00:00 2001 From: hahuynhvan Date: Wed, 2 Oct 2024 20:28:36 +0700 Subject: [PATCH 1/2] review --- client/package.json | 2 + client/src/components/CommentSection.jsx | 260 ++++++++++++++++++++ client/src/components/Home/FeaturedBook.jsx | 2 +- client/src/components/MyNavbar.jsx | 4 +- client/src/components/index.js | 2 + client/src/pages/main/SingleBook.jsx | 46 +++- client/src/pages/main/UserInfo.jsx | 74 ++++-- client/src/utils/index.js | 41 +++ controllers/book.controller.js | 11 +- controllers/order_item.controller.js | 42 ++++ controllers/review.controller.js | 52 +++- controllers/user.controller.js | 13 +- middleware/authentication.js | 6 +- models/index.js | 7 + models/oder_item.model.js | 32 +++ models/order.model.js | 25 +- models/review.model.js | 15 +- routes/oder_item.router.js | 26 ++ routes/review.router.js | 7 + server.js | 3 + utils/createTokenUser.js | 6 - utils/jwt.js | 2 +- 22 files changed, 606 insertions(+), 72 deletions(-) create mode 100644 client/src/components/CommentSection.jsx create mode 100644 controllers/order_item.controller.js create mode 100644 models/oder_item.model.js create mode 100644 routes/oder_item.router.js diff --git a/client/package.json b/client/package.json index 200943e..c88426b 100644 --- a/client/package.json +++ b/client/package.json @@ -23,6 +23,7 @@ "bootstrap-icons": "^1.11.3", "dayjs": "^1.11.13", "dotenv": "^16.4.5", + "mdb-react-ui-kit": "^9.0.0", "polished": "^4.3.1", "react": "^18.3.1", "react-bootstrap": "^2.10.4", @@ -30,6 +31,7 @@ "react-icons": "^5.2.1", "react-redux": "^9.1.2", "react-router-dom": "^6.24.0", + "react-star-ratings": "^2.3.0", "react-toastify": "^10.0.5", "styled-components": "^6.1.12" }, diff --git a/client/src/components/CommentSection.jsx b/client/src/components/CommentSection.jsx new file mode 100644 index 0000000..2d234d7 --- /dev/null +++ b/client/src/components/CommentSection.jsx @@ -0,0 +1,260 @@ +import React, { useEffect, useState } from 'react' +import { + MDBCard, + MDBCardBody, + MDBCardImage, + MDBCol, + MDBContainer, + MDBRow, + MDBTypography, +} from 'mdb-react-ui-kit' +import { + quaternaryBgColor, + quaternaryBgColorLight, +} from '../assets/js/variables' +import styled from 'styled-components' +import { defaultAvatar } from '../assets/images' +import { formatVNTimeZoneDate, ratingTitle } from '../utils' +import { FaStar } from 'react-icons/fa' +import { useSelector } from 'react-redux' +import { customFetch } from '../utils/axios' +import StarRatings from 'react-star-ratings' +import { toast } from 'react-toastify' +import Loading from './Loading' + +export default function CommentSection({ book_id, reviews }) { + const [loading, setLoading] = useState(false) + const [values, setValues] = useState({ + rating: 5, + comment: '', + }) + const [title, setTitle] = useState(ratingTitle(values.rating)) + const { user, isLoading } = useSelector((store) => store.user) + const [userReview, setUserReview] = useState(null) + + const handleRatingChange = (e) => { + setValues({ ...values, rating: e }) + } + const handleChange = (e) => { + const name = e.target.name + const value = e.target.value + setValues({ ...values, [name]: value }) + } + + const handleSubmit = async (e) => { + e.preventDefault() + try { + setLoading(true) + const review = { + rating: values.rating, + comment: values.comment, + book_id, + } + + await customFetch.post('/reviews', review) + setValues({ + rating: 5, + comment: '', + }) + toast.success('Đánh giá đã được gửi') + } catch (error) { + console.log(error) + toast.warn(error?.response?.data?.msg) + } finally { + setLoading(false) + } + } + + useEffect(() => { + const fetchUserReview = async () => { + if (user) { + try { + const response = await customFetch( + `/reviews/getCurrentUserReviewSingleBook/${book_id}` + ) + setUserReview(response.data.review) + } catch (error) { + console.error('Error fetching user review:', error) + } + } + } + + fetchUserReview() + }, [book_id, loading, user]) + + useEffect(() => { + setTitle(ratingTitle(values.rating)) + }, [values.rating]) + + if(loading) + return + return ( + + + + + + + {userReview && user ? ( +
+ + Đánh giá của bạn + +
+
+ + + {userReview?.user?.name} + +

+ {formatVNTimeZoneDate(userReview?.created_at)} +

+
+
+
+ + {userReview?.rating} + + +
+ {ratingTitle(userReview?.rating)} +
+
+
{userReview?.comment}
+
+
+
+ ) : ( +
+ +
+ +
{title}
+
+