From 42e00c7ec08fb014853b2a568b3889e84b3c3884 Mon Sep 17 00:00:00 2001 From: Reza_ashrafi Date: Thu, 7 Jul 2022 18:37:30 +0430 Subject: [PATCH] update src/views/Blog/index.js, Suggest.js and src/views/ShoppingCart/index.js --- src/views/Blog/index.js | 96 +++++------ src/views/Blogs/layouts/Suggest.js | 2 +- src/views/ShoppingCart/index.js | 268 +++++++++++++++-------------- 3 files changed, 183 insertions(+), 183 deletions(-) diff --git a/src/views/Blog/index.js b/src/views/Blog/index.js index 791cdb5..b6168fd 100644 --- a/src/views/Blog/index.js +++ b/src/views/Blog/index.js @@ -2,14 +2,13 @@ import React, { useEffect } from "react"; import { connect } from "react-redux"; import { blog, publicApi } from "../../redux/actions"; import { Carousel } from "@trendyol-js/react-carousel"; -import location from "../../utils/location"; +import { useParams } from "react-router-dom"; -import Hashtags from "../Blogs/layouts/Hashtags"; +//components import Header from "../Blogs/layouts/Header"; import Suggest from "../Blogs/layouts/Suggest"; import New from "../Blogs/layouts/New"; import Avatar from "../../components/Avatar"; -import { useParams } from "react-router-dom"; //assets import arrow from "../../assets/icons/dropdown-blue.svg"; @@ -21,64 +20,21 @@ export const Blog = ({ headerOptions, getList, blogs, - activeBlog, }) => { let { id } = useParams(); - // let id = location.getId(); useEffect(() => { + getList(); setHeaderOptions(headerOptions); - getInfo({ id: location.getId() }); - if (blogs.length === 0) { - getList(); - } - }, [id]); - - useEffect(() => { - getInfo({ id: location.getId() }); - }, [activeBlog]); + }, []); useEffect(() => { - // alert(); - window.scrollTo(0, 0); + window.scroll({ top: 0, behavior: "smooth" }); + getInfo({ id }); }, [id]); - const suggestionSlider = React.useCallback(() => { - return ( - e.stopPropagation()} - /> - } - leftArrow={ - e.stopPropagation()} - /> - } - > - {blogs - ?.filter((blog) => Number(blog?.id) !== Number(id)) - .map((blog, index) => ( - - ))} - - ); - }, [blogs, id]); - - return ( -
+ return blogs?.length ? ( +
- + e.stopPropagation()} + /> + } + leftArrow={ + e.stopPropagation()} + /> + } + > + {blogs + ?.slice(0, 10) + ?.filter((blog) => Number(blog?.id) !== Number(id)) + .map((blog, index) => ( + + ))} +
{/* desktop */}
- ); + ) :
+ +
; }; const mapStateToProps = (state) => ({ diff --git a/src/views/Blogs/layouts/Suggest.js b/src/views/Blogs/layouts/Suggest.js index 4032f91..1afdd10 100644 --- a/src/views/Blogs/layouts/Suggest.js +++ b/src/views/Blogs/layouts/Suggest.js @@ -20,7 +20,7 @@ export const Suggest = ({ blog }) => { className="w-full rounded-md" style={{ height: "calc(100vw / 12 * 11 / 3 / 21 * 9)" }} /> - {blog?.title} + {blog?.title} ); }; diff --git a/src/views/ShoppingCart/index.js b/src/views/ShoppingCart/index.js index 2a3fb23..2134d2d 100644 --- a/src/views/ShoppingCart/index.js +++ b/src/views/ShoppingCart/index.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useMemo, useCallback } from "react"; import { userProduct, userFactor, @@ -24,7 +24,6 @@ import { connect } from "react-redux"; function ShoppingCart({ isDark, isMobile, - list, factorInfo, user, getList, @@ -37,11 +36,11 @@ function ShoppingCart({ loading, setDialog, isLogin, + useless, }) { const [phase, setPhase] = useState("products"); const navigation = useNavigate(); useEffect(() => { - console.log("gl1") if (isLogin) getList(); getFactorInfo({ userId: user?.id }); setHeaderOptions(headerOptions); @@ -53,36 +52,38 @@ function ShoppingCart({ } }, [phase]); - const productsLength = userProducts?.filter( - (product) => product.condition < 2 - )?.length; + const productsLength = useMemo(() => { + return userProducts?.filter((product) => product.condition < 2)?.length; + }, [userProducts]); - const next = { - products: () => - productsLength - ? userProducts.filter((object) => object.productType === 2).length > 0 - ? setPhase("deliveryForm") - : setDialog({ - type: "confirm", - text: "به درگاه پرداخت بریم؟", - open: true, - accept: () => payFactor({ userId: user.id }), - }) - : toast.info("ابتدا یک محصول را به سبد خرید خود اضافه کنید."), - deliveryForm: () => - setDialog({ - type: "confirm", - text: "به درگاه پرداخت بریم؟", - open: true, - accept: () => payFactor({ userId: user.id }), - }), - profile: () => { - console.log("Set profile"); - console.log("Go to shaparak"); - }, - }; + const next = useMemo(() => { + return { + products: () => + productsLength + ? userProducts.filter((object) => object.productType === 2)?.length + ? setPhase("deliveryForm") + : setDialog({ + type: "confirm", + text: "به درگاه پرداخت بریم؟", + open: true, + accept: () => payFactor({ userId: user.id }), + }) + : toast.info("ابتدا یک محصول را به سبد خرید خود اضافه کنید."), + deliveryForm: () => + setDialog({ + type: "confirm", + text: "به درگاه پرداخت بریم؟", + open: true, + accept: () => payFactor({ userId: user.id }), + }), + profile: () => { + console.log("Set profile"); + console.log("Go to shaparak"); + }, + }; + }, [userProducts, user]); - const Table = () => { + const Table = React.useCallback(() => { return (
@@ -94,11 +95,11 @@ function ShoppingCart({
    - {list - .sort((a, b) => a?.id - b?.id) - .map( + {userProducts + ?.sort((a, b) => a?.id - b?.id) + ?.map( (product, index) => - product.condition < 2 && ( + product?.condition < 2 && ( ) )} @@ -108,90 +109,97 @@ function ShoppingCart({
); - }; + }, [userProducts]); - const PriceStatus = ({ name, value, type }) => { - return isMobile ? ( -
- - {name} - - - {separate(Math.floor(value))} - - تومان - - -
- ) : ( -
- - {name} - - - {separate(Math.floor(value))} - - تومان - - -
- ); - }; + const PriceStatus = useCallback( + ({ name, value, type }) => { + return ( + +
+ + {name} + + + {separate(Math.floor(value))} + + تومان + + +
+
+ + {name} + + {value ? ( + + {separate(Math.floor(value))} + + تومان + + + ) : null} +
+
+ ); + }, + [useless] + ); - const layouts = { - products: isMobile ? ( - <> - {list - .sort((a, b) => a?.id - b?.id) - .map( - (product, index) => - product.condition < 2 && ( - - ) - )} - - - ) : productsLength ? ( - - ) : ( - navigation("/products")} - /> - // سبد خرید شما خالی است. - ), - deliveryForm: , - address:
, - }; + const layouts = useMemo(() => { + return { + products: isMobile ? ( + <> + {userProducts + .sort((a, b) => a?.id - b?.id) + .map( + (product, index) => + product.condition < 2 && ( + + ) + )} + + + ) : productsLength ? ( +
+ ) : ( + navigation("/products")} + /> + ), + deliveryForm: , + address:
, + }; + }, [isMobile, userProducts, loading]); return isMobile ? (
@@ -221,11 +229,13 @@ function ShoppingCart({ جمع کل سبد خرید -

- {separate(productsLength ? factorInfo?.payPrice : 0) + - " " + - "تومان"} -

+ {factorInfo?.payFactor ? ( +

+ {separate(factorInfo?.payPrice) + + " " + + "تومان"} +

+ ) : null}
{layouts[phase]}
- {console.log(list)} - {list?.filter((product) => product?.condition < 2)?.length + {userProducts?.filter((product) => product?.condition < 2)?.length ? phase !== "address" && (
{/* ({ isDark: state.publicApi.isDark, isMobile: state.publicApi.isMobile, - list: state.userProduct.list, payPrice: state.userProduct.sum, factorInfo: state.userFactor.info, user: state.user.status,