GuestReviews.js 6.37 KB
import Image from "next/image";
import { useRouter } from "next/router";
import React, { useState } from "react";
import { Button, Modal } from "react-bootstrap";
import { useSelector } from "react-redux";
import StarRatings from "react-star-ratings";
import { postReviewEndUser } from "../../redux/actions/reviewsAction";
import { dateFormatFn } from "../../services/imageHandling";

const GuestReviews = ({ activityById }) => {
  const [rating, setRating] = useState(0);
  const [comments, setcomments] = useState();
  const [readMoreText, setreadMoreText] = useState();
  const [showModal, setshowModal] = useState(false);
  const { endUser } = useSelector(state => state.endUser);
  const { reviews } = useSelector(state => state.reviews);

  const router = useRouter();
  console.log("reviews", reviews);
  const handleRatingChange = newRating => {
    setRating(newRating);
  };

  return (
    <>
      <section className="guest-reviews-session">
        <div className="container">
          <div className="row">
            <div className="col-md-12">
              <div className="row">
                <div className="col-12">
                  <div className="head-btn">
                    <div className="head01">
                      <div className="title">Guest reviews</div>
                    </div>
                    {/* <a href="" className="view-all-reviews-btn">
                      View All Reviews
                    </a> */}
                  </div>
                </div>
              </div>
              <div className="row">
                <div className="col-12">
                  <div className="product-reviews">
                    <span className="rating">8.8</span>
                    <span>Fabulous</span>
                    <span className="review">{` ${reviews.length} reviews`}</span>
                  </div>
                </div>
              </div>
              <div className="row">
                {reviews &&
                  reviews.map(data => {
                    return (
                      <div className="col-md-6">
                        <div className="guest-reviews-detail">
                          <div className="head">
                            <div className="name">{data.attributes.endUser.data.attributes.name}</div>
                            <div className="month">{dateFormatFn(data.attributes.createdAt)}</div>
                          </div>
                          <StarRatings
                            className="col-3 mx-2"
                            rating={data.attributes.rating}
                            starRatedColor="yellow" // Set the rated color to yellow
                            starHoverColor="yellow" // Set the hover color to yellow
                            changeRating={() => {}}
                            numberOfStars={5}
                            name="rating"
                            starDimension="20px" // Set star width and height
                          />
                          <div className="review-content">
                            "{data.attributes.comments.length > 80 ? `${data.attributes.comments.slice(0, 80)}...` : data.attributes.comments}
                            {data.attributes.comments.length > 80 && (
                              <a
                                onClick={() => {
                                  setreadMoreText(data);
                                  setshowModal(true);
                                }}
                              >
                                Read More
                              </a>
                            )}
                          </div>
                          {/* View All */}
                        </div>
                      </div>
                    );
                  })}
              </div>
              {endUser && (
                <div className="row">
                  <div className="head01">
                    <div className="title">Post a review:</div>
                  </div>
                  <div>
                    <label className=" mb-3 ">Rating: </label>{" "}
                    <StarRatings
                      className="col-3 mx-2"
                      rating={rating}
                      starRatedColor="yellow" // Set the rated color to yellow
                      starHoverColor="yellow" // Set the hover color to yellow
                      changeRating={handleRatingChange}
                      numberOfStars={5}
                      name="rating"
                      starDimension="20px" // Set star width and height
                    />
                  </div>
                  <textarea
                    // style={{ width: "50%" }}
                    rows={4}
                    className="p-2 mx-2 col-md-6"
                    placeholder="Comments"
                    name="comments"
                    value={comments}
                    onChange={e => {
                      e.preventDefault();
                      // console.log(e.target.value);4
                      setcomments(e.target.value);
                      // setrejectionReasonText(e.target.value);
                    }}
                  />
                  <div className="view-all-btn my-3">
                    <Button
                      disabled={rating == 0 || comments == ""}
                      variant="primary"
                      onClick={async e => {
                        e.preventDefault();
                        const res = await postReviewEndUser({ endUserId: endUser.id, activityId: router.query.id, comments, rating });
                        console.log("res", res);
                        setRating(0);
                        setcomments("");
                      }}
                    >
                      Submit
                    </Button>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
        {readMoreText && (
          <Modal
            centered
            show={showModal}
            onHide={() => {
              setshowModal();
              setreadMoreText(null);
            }}
          >
            <Modal.Header closeButton>Review from {readMoreText.attributes.endUser.data.attributes.name}</Modal.Header>
            <Modal.Body>
              <div>{readMoreText.attributes.comments}</div>
            </Modal.Body>
          </Modal>
        )}
      </section>
    </>
  );
};

export default GuestReviews;