Advertisement
vandasche

Untitled

Dec 16th, 2020
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. import React, { useState, useEffect } from "react";
  2. import PropTypes from "prop-types";
  3. import dynamic from "next/dynamic";
  4. import Router from "next/router";
  5. import { connect } from "react-redux";
  6.  
  7. import Navbar from "../../components/Navbar";
  8. // import Dropdown from '../../components/Dropdown'
  9. import Footer from "../../components/Footer";
  10. import Card from "../../components/Card";
  11. import Loader from "../../components/Loader";
  12. // import Carousel from '../../components/Carousel'
  13. const Carousel = dynamic(() => import("../../components/Carousel"), {
  14. ssr: false,
  15. });
  16. const Dropdown = dynamic(() => import("../../components/Dropdown"), {
  17. ssr: false,
  18. });
  19.  
  20. import ReactPaginate from "react-paginate";
  21. import styles from "../../assets/scoped/products.module.sass";
  22. import { isLaptopSize, isMobileSize } from "../../utils/helper";
  23. import { CarouselImg } from "../../store/actions/HomePageApi";
  24. import { getListProduct } from "../../store/actions/ListProductApi";
  25. import { getDropDownProduct } from "../../store/actions/CategoryProductSelectApi";
  26.  
  27. const Product = ({
  28. CarouselHome,
  29. CarouselImg,
  30. getListProduct,
  31. ListProduct,
  32. TotalProduct,
  33. CategoryProduct,
  34. getDropDownProduct,
  35. isLoading,
  36. }) => {
  37. console.log(CarouselHome, "carousel");
  38. const [perPage, setPerPage] = useState(9); //per page 9
  39. const [currentPage, setCurrentPage] = useState(0);
  40. const [pageCount, setPageCount] = useState(0);
  41. const [slice, setSlice] = useState([]);
  42. // const [offset, setOffset] = useState(0)
  43.  
  44. // state for product
  45. const [page, setPage] = useState(0);
  46. const [categories, setCategories] = useState(0);
  47. const [statusApi, setStatusApi] = useState("all");
  48. const [sort, setSort] = useState("");
  49. const [active, setActive] = useState({});
  50.  
  51. useEffect(() => {
  52. CarouselImg();
  53. getListProduct(
  54. Router.router.query.page,
  55. Router.router.query.category,
  56. statusApi,
  57. sort
  58. );
  59. getDropDownProduct();
  60. setActive(
  61. CategoryProduct.filter(
  62. (res) => String(res.valueId) === Router.router.query.category
  63. )[0]
  64. );
  65. }, []);
  66.  
  67. useEffect(() => {
  68. setSlice(ListProduct);
  69. setPageCount(Math.ceil(TotalProduct / perPage));
  70. setPage(0);
  71. setCurrentPage(page);
  72. }, [ListProduct, TotalProduct, perPage, setSlice, setPageCount]);
  73.  
  74. const category = async (id) => {
  75. Router.push({
  76. pathname: "/product",
  77. query: { page: page, category: id },
  78. });
  79. await setPage(0);
  80. await setCategories(id);
  81. await getListProduct(page, id, statusApi, sort);
  82. };
  83.  
  84. const status = async (status) => {
  85. Router.push({
  86. pathname: "/product",
  87. query: {
  88. page: page,
  89. category: categories,
  90. status: status,
  91. },
  92. });
  93. await setStatusApi(status);
  94. await getListProduct(page, categories, status, sort);
  95. };
  96.  
  97. const sorted = async (valueId) => {
  98. Router.push({
  99. pathname: "/product",
  100. query: {
  101. page: 1,
  102. category: categories,
  103. status: statusApi,
  104. sort:
  105. valueId === "-final_price"
  106. ? "dsc"
  107. : valueId === "final_price"
  108. ? "asc"
  109. : "",
  110. },
  111. });
  112. await setSort(valueId);
  113. await getListProduct(page, categories, statusApi, valueId);
  114. };
  115.  
  116. const data = async (payload) => {
  117. Router.push({
  118. pathname: "/product",
  119. query: { page: payload },
  120. });
  121. await setPage(payload);
  122. await getListProduct(payload, categories, statusApi, sort);
  123. await window.scrollTo(0, 700);
  124. };
  125.  
  126. const handleClick = ({ selected }) => {
  127. setCurrentPage(selected);
  128. // setOffset(selected * perPage)
  129. // const newOffside = selected * perPage
  130. data(selected);
  131. };
  132.  
  133. const categoryDropdown =
  134. CategoryProduct &&
  135. [
  136. ...CategoryProduct,
  137. { id: 1, defaultName: "All Category", value: 0, valueId: 0 },
  138. ].sort((a, b) => a.valueId - b.valueId);
  139.  
  140. return (
  141. <div>
  142. <Navbar active="shop" />
  143. <div className={styles.containercarousel}>
  144. <Carousel
  145. datas={CarouselHome}
  146. type="product"
  147. width={isLaptopSize ? "1600px" : "100%"}
  148. height={isLaptopSize ? "835px" : "100%"}
  149. />
  150. </div>
  151. <section className={styles.contentsection}>
  152. <div className={styles.content}>
  153. <div className={styles.wrapperdropdown}>
  154. <div className={styles.categorysize}>
  155. <div className={styles.category}>
  156. <Dropdown
  157. border="1px solid #c4c4c4"
  158. handleChange={category}
  159. warding={categoryDropdown}
  160. active={active}
  161. />
  162. </div>
  163. <div className={styles.size}>
  164. <Dropdown
  165. border="1px solid #c4c4c4"
  166. handleChange={status}
  167. warding={[
  168. {
  169. id: 1,
  170. defaultName: "All Status",
  171. value: "",
  172. valueId: "all",
  173. },
  174. {
  175. id: 2,
  176. defaultName: "New",
  177. value: "New",
  178. valueId: "new",
  179. },
  180. {
  181. id: 3,
  182. defaultName: "Sale",
  183. value: "Sale",
  184. valueId: "sale",
  185. },
  186. {
  187. id: 4,
  188. defaultName: "Out of Stock",
  189. value: "Out of Stock",
  190. valueId: "out_of_stock",
  191. },
  192. {
  193. id: 5,
  194. defaultName: "None",
  195. value: "None",
  196. valueId: "none",
  197. },
  198. ]}
  199. setPage={setPage}
  200. />
  201. </div>
  202. </div>
  203. <div className={styles.wrappersort}>
  204. <h6 className="t-black left-align spacing-normal reg">
  205. {isMobileSize ? "" : "Sort by :"}
  206. </h6>
  207. <div className={styles.sort}>
  208. <Dropdown
  209. border="1px solid #c4c4c4"
  210. handleChange={sorted}
  211. setPage={setPage}
  212. warding={[
  213. {
  214. id: 1,
  215. defaultName: "Most Related",
  216. value: "",
  217. valueId: "",
  218. },
  219. {
  220. id: 2,
  221. defaultName: "Highest Price",
  222. value: "Highest Price",
  223. valueId: "-final_price",
  224. },
  225. {
  226. id: 3,
  227. defaultName: "Lowest Price",
  228. value: "Lowest Price",
  229. valueId: "final_price",
  230. },
  231. ]}
  232. />
  233. </div>
  234. </div>
  235. </div>
  236. </div>
  237. </section>
  238.  
  239. <section className={styles.cardsection}>
  240. {isLoading ? (
  241. <div className={[styles.wrapperloader, "row"].join(" ")}>
  242. {[1, 2, 3].map((res) => (
  243. <div className={styles.card} key={res}>
  244. <Loader />
  245. </div>
  246. ))}
  247. </div>
  248. ) : (
  249. <div className={[styles.wrappercard, "row"].join(" ")}>
  250. <Card data={slice && slice} />
  251. <div className={[styles.wrapperpagination, "col-sm-12"].join(" ")}>
  252. <ReactPaginate
  253. previousLabel={""}
  254. previousClassName={styles.previous}
  255. nextClassName={styles.next}
  256. nextLabel={""}
  257. breakLabel={"..."}
  258. breakClassName={"break-me"}
  259. pageCount={pageCount}
  260. forcePage={currentPage}
  261. marginPagesDisplayed={2}
  262. pageRangeDisplayed={5}
  263. onPageChange={handleClick}
  264. containerClassName={styles.pagination}
  265. subContainerClassName={[styles.pagination, "pages"].join(" ")}
  266. activeClassName={styles.active}
  267. />
  268. </div>
  269. </div>
  270. )}
  271. </section>
  272. <Footer />
  273. </div>
  274. );
  275. };
  276.  
  277. Product.propTypes = {};
  278. //untuk ngambil data dari redux
  279. const mapStateToProps = ({
  280. CarouselHome,
  281. ListProduct,
  282. TotalProduct,
  283. CategoryProduct,
  284. }) => ({
  285. CarouselHome: CarouselHome.img,
  286. TotalProduct,
  287. ListProduct: ListProduct.product,
  288. CategoryProduct: CategoryProduct.category,
  289. isLoading: ListProduct.loading,
  290. });
  291.  
  292. //triger api dari action
  293. const mapDispatchToProps = (dispatch) => ({
  294. CarouselImg: () => dispatch(CarouselImg()),
  295. getListProduct: (page, id, status, price) =>
  296. dispatch(getListProduct(page, id, status, price)),
  297. getDropDownProduct: () => dispatch(getDropDownProduct()),
  298. });
  299.  
  300. // using React.memo to achieve PureComponents
  301. export default React.memo(
  302. connect(mapStateToProps, mapDispatchToProps)(Product)
  303. );
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement