Blame view

components/listing/ListingItems.js 5.59 KB
.  
jaymehta committed
1
import { Empty } from "antd";
2
import Image from "next/image";
jaymehta committed
3
import { useRouter } from "next/router";
.  
jaymehta committed
4
import React, { useEffect, useState } from "react";
5
import { Button } from "react-bootstrap";
.  
jaymehta committed
6
import { Loader } from "react-bootstrap-typeahead";
.  
jaymehta committed
7
import { useDispatch, useSelector } from "react-redux";
8
import { cleanImage } from "../../services/imageHandling";
jaymehta committed
9
import WishlistComponent from "../detail/WIshlistComponent";
jaymehta committed
10
import Pagination from "react-js-pagination";
.  
jaymehta committed
11
import { getActivitiesByFilters } from "../../redux/actions/activityAction";
12

jaymehta committed
13
const ListingItems = ({ allActivitiesData, loading, gridClass, totalCount }) => {
Ravindra Kanojiya committed
14
  // const [gridClass, setGridClass] = useState('col-md-3');
jaymehta committed
15
  const { endUser } = useSelector(state => state.endUser);
jaymehta committed
16
  const router = useRouter();
.  
jaymehta committed
17
  const dispatch = useDispatch();
jaymehta committed
18 19 20
  // useEffect(() => {
  //   dispatch(getActivitiesByFilters({ currentPage: router.query.page }));
  // }, [router]);
.  
jaymehta committed
21
  console.log("endUser", endUser);
jaymehta committed
22
  // console.log("router", router);
jaymehta committed
23
  let { page, location = "" } = router.query;
jaymehta committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  page = Number(page);

  let queryParams;

  if (typeof window !== "undefined") {
    queryParams = new URLSearchParams(window.location.search);
  }
  const handlePagination = pageNumber => {
    // This was the old way to do this, by doing this we were getting rid of the other query string parameters.
    // router.push(`?page=${pageNumber}`);

    // We have done this to take care of keeping the search filters in the query params
    // when we end up changing the page number.
    if (queryParams.has("page")) {
      queryParams.set("page", pageNumber);
    } else {
      queryParams.append("page", pageNumber);
    }
jaymehta committed
42

jaymehta committed
43
    // dispatch(getActivitiesByFilters({ currentPage: pageNumber }));
jaymehta committed
44 45
    router.push({
      query: queryParams.toString()
jaymehta committed
46 47
    });
  };
.  
jaymehta committed
48
  console.log("page", page);
jaymehta committed
49 50 51
  // useEffect(() => {
  //   dispatch(getActivitiesByFilters({ currentPage: page }));
  // }, [])
jaymehta committed
52
  
53 54
  return (
    <>
.  
jaymehta committed
55 56 57 58 59 60 61
      {allActivitiesData && !allActivitiesData.data.length == 0 ? (
        <div className="listing-items">
          {!loading ? (
            <div className="row">
              {allActivitiesData &&
                allActivitiesData.data.map(data => {
                  return (
Ravindra Kanojiya committed
62
                    <div className={gridClass}>
.  
jaymehta committed
63 64 65 66 67 68 69 70 71 72 73 74 75
                      <div className="item">
                        <div className="browse-experiences-item">
                          <div className="img-wrapper">
                            <span className="image-container">
                              <Image layout="fill" alt="" className="image img-fluid" src={cleanImage(data.attributes?.image?.data?.attributes)} />
                            </span>
                            <div className="top-rated">Top Rated</div>
                          </div>
                          <div className="info">
                            <div className="top-name">
                              <div className="title">{data?.attributes?.name}</div>
                              <div className="rating-wishlist">
                                <div className="rating">
Ravindra Kanojiya committed
76
                                  <span className="number">{data?.attributes?.rating}</span>
.  
jaymehta committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
                                  <span className="image-container">
                                    <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/star.svg" />
                                  </span>
                                </div>
                                {endUser && <WishlistComponent activityId={data.id} userId={endUser.id} />}
                              </div>
                            </div>
                            <div className="discription">
                              {data.discription} <a href="">Read More</a>
                            </div>
                            <div className="price">
                              ${data?.attributes?.pricePerPerson} <span className="off">{data?.attributes?.off}% OFF</span>
                            </div>
                            <div className="detail">
                              <div className="">For 1 Night</div>
                              <div className="">Includes taxes & Fees</div>
                            </div>
                            <div className="explore-now">
                              <Button
                                onClick={() => {
                                  router.push(`/activities/${data.id}`);
                                }}
                                variant="primary"
                              >
                                Explore Now
                              </Button>
103 104
                            </div>
                          </div>
jaymehta committed
105 106 107
                        </div>
                      </div>
                    </div>
.  
jaymehta committed
108 109 110 111 112 113
                  );
                })}
            </div>
          ) : (
            <Loader />
          )}
114
        </div>
.  
jaymehta committed
115 116 117
      ) : (
        <Empty />
      )}
jaymehta committed
118 119 120 121 122 123 124
      <div className="row">
        <div className="col-12">
          <div className="load-more">
            <Pagination
              activePage={page}
              itemsCountPerPage={12}
              totalItemsCount={totalCount}
jaymehta committed
125
              onChange={async (e) => {
.  
jaymehta committed
126
                console.log("log >",e);
jaymehta committed
127 128
                await handlePagination(e);
              }}
Ravindra Kanojiya committed
129 130 131 132
              nextPageText={"›"}
              prevPageText={"‹"}
              firstPageText={"«"}
              lastPageText={"»"}
jaymehta committed
133 134 135 136 137 138
              itemClass="page-item"
              linkClass="page-link"
            ></Pagination>
          </div>
        </div>
      </div>
139 140 141 142 143
    </>
  );
};

export default ListingItems;