diff --git a/src/axios.js b/src/axios.js index c4d4317..51df470 100644 --- a/src/axios.js +++ b/src/axios.js @@ -1,5 +1,5 @@ import axios from "axios"; -const baseUrl = "http://127.0.0.1:8000/"; +const baseUrl = "http://45.15.25.48:8000/"; const axiosInstance = axios.create({ baseURL: baseUrl, diff --git a/src/pages/AccountBox/index.js b/src/pages/AccountBox/index.js index 5bf63fd..b34ac93 100644 --- a/src/pages/AccountBox/index.js +++ b/src/pages/AccountBox/index.js @@ -1,10 +1,11 @@ -import React, { useEffect, useState } from "react"; +import React, { useContext, useEffect, useState } from "react"; import { Login } from "./Login"; import { motion } from "framer-motion"; import { AccountContext } from "./accountContext"; import { Signup } from "./Signup"; -import { Link } from "react-router-dom"; +import { Link, useNavigate } from "react-router-dom"; import classes from "./AccountBox.module.scss"; +import UserContext from "../../store/UserContext"; function getBackdropVariants() { return { @@ -30,6 +31,15 @@ const expandingTransition = { }; export default function AccountBox(props) { + let { auth, type } = useContext(UserContext); + const navigate = useNavigate(); + + useEffect(() => { + if (auth) { + navigate("/"); + } + }, [auth, navigate]); + let backdropVariants = getBackdropVariants(); const [isExpanded, setExpanded] = useState(false); const [active, setActive] = useState("signin"); diff --git a/src/pages/Product/index.js b/src/pages/Product/index.js index cb4a2b9..56fe9df 100644 --- a/src/pages/Product/index.js +++ b/src/pages/Product/index.js @@ -51,7 +51,7 @@ function Product() { count: 5, color: "#868e96", activeColor: "#6667ab", - value: 0, + value: product.score, a11y: true, isHalf: true, emptyIcon: , @@ -59,6 +59,16 @@ function Product() { filledIcon: , onChange: (newValue) => { console.log(`Example 2: new value is ${newValue}`); + console.log([product.id, newValue]); + axiosInstance + .post(`/accounts/show_better_clothes/`, { + data: [product.id, newValue], + }) + .then((res) => { + if (res.status === 200) { + setProduct((prev) => ({ ...prev, is_favorite: false })); + } + }); }, }; @@ -104,68 +114,52 @@ function Product() { // }, [productId]); const favoriteHandler = () => { - console.log(product) - if(product.is_favorite) { + console.log(product); + if (product.is_favorite) { axiosInstance - .post(`accounts/delete_from_favorite/`, { - data: product.id, - }) - .then((res) => { - if (res.status === 200) { - setProduct(prev => ({...prev, is_favorite: false})) - } - }); - } - else{ + .post(`accounts/delete_from_favorite/`, { + data: product.id, + }) + .then((res) => { + if (res.status === 200) { + setProduct((prev) => ({ ...prev, is_favorite: false })); + } + }); + } else { axiosInstance - .post(`accounts/add_to_favorite/`, { - data: product.id, - }) - .then((res) => { - if (res.status === 200) { - setProduct(prev => ({...prev, is_favorite: true})) - } - }); + .post(`accounts/add_to_favorite/`, { + data: product.id, + }) + .then((res) => { + if (res.status === 200) { + setProduct((prev) => ({ ...prev, is_favorite: true })); + } + }); } - }; const addProductHandler = () => { - if(product.is_in_cart) { + if (product.is_in_cart) { axiosInstance - .post(`accounts/delete_from_cart/`, { - data: product.id, - }) - .then((res) => { - if (res.status === 200) { - setProduct(prev => ({...prev, is_in_cart: false})) - } - }); - } - else { + .post(`accounts/delete_from_cart/`, { + data: product.id, + }) + .then((res) => { + if (res.status === 200) { + setProduct((prev) => ({ ...prev, is_in_cart: false })); + } + }); + } else { axiosInstance - .post(`accounts/add_to_cart/`, { - data: product.id, - }) - .then((res) => { - if (res.status === 200) { - setProduct(prev => ({...prev, is_in_cart: true})) - } - }); + .post(`accounts/add_to_cart/`, { + data: product.id, + }) + .then((res) => { + if (res.status === 200) { + setProduct((prev) => ({ ...prev, is_in_cart: true })); + } + }); } - - - console.log("add to shop"); - axiosInstance - .post(`accounts/add_to_cart/`, { - data: product.id, - }) - .then((res) => { - if (res.status === 200) { - console.log(res); - console.log(res.data); - } - }); }; return ( @@ -187,15 +181,22 @@ function Product() { src={product.upload} alt={product.product_name} > - {!product.is_favorite && - {heart} - } - {product.is_favorite && {heartSolid}} - - + {!product.is_favorite && ( + + {heart} + + )} + {product.is_favorite && ( + + {heartSolid} + + )}
@@ -383,7 +384,11 @@ function Product() { isDisable={product.inventory === 0} onClickHandler={addProductHandler} > - {product.inventory === 0 ? "ناموجود" : !product.is_in_cart ? "افزودن به سبد" : " حذف از سبد"} + {product.inventory === 0 + ? "ناموجود" + : !product.is_in_cart + ? "افزودن به سبد" + : " حذف از سبد"}
diff --git a/src/pages/ProductsList/index.js b/src/pages/ProductsList/index.js index 5682f55..0ba39f6 100644 --- a/src/pages/ProductsList/index.js +++ b/src/pages/ProductsList/index.js @@ -99,7 +99,7 @@ function ProductsList() { price={99000} priceOff={100000} img={"./images/clothes/11bg.png"} - /> */}{" "} + />{" "} */}
diff --git a/src/pages/SellerPanel/index.js b/src/pages/SellerPanel/index.js index 547f914..b3aab28 100644 --- a/src/pages/SellerPanel/index.js +++ b/src/pages/SellerPanel/index.js @@ -10,24 +10,14 @@ import { useContext, useEffect, useLayoutEffect } from "react"; import UserContext from "../../store/UserContext"; function SellerPanel() { - const { user } = useContext(UserContext); + const { auth, type } = useContext(UserContext); const navigate = useNavigate(); useEffect(() => { - console.log(!user.auth || (!user.auth && user.type !== "user")); - if (!user.auth || (!user.auth && user.type !== "seller")) { - console.log("sellerrrrr2"); - // navigate("/404"); + if (!auth || (!auth && type !== "seller")) { + navigate("/404"); } - }, []); - - useLayoutEffect(() => { - console.log(!user.auth || (!user.auth && user.type !== "user")); - if (!user.auth || (!user.auth && user.type !== "seller")) { - console.log("sellerrrrr2"); - // navigate("/404"); - } - }, [user.auth, user.type, navigate]); + }, [auth, type, navigate]); return ( <> diff --git a/src/pages/ShoppingList/ShoppingItem/index.js b/src/pages/ShoppingList/ShoppingItem/index.js index 65522a4..e6079c9 100644 --- a/src/pages/ShoppingList/ShoppingItem/index.js +++ b/src/pages/ShoppingList/ShoppingItem/index.js @@ -9,6 +9,7 @@ import { faShield } from "@fortawesome/free-solid-svg-icons"; import { faRulerVertical } from "@fortawesome/free-solid-svg-icons"; import { faTrashCan } from "@fortawesome/free-solid-svg-icons"; import { faBrush } from "@fortawesome/free-solid-svg-icons"; +import axiosInstance from "../../../axios"; const money = ; const check = ; const shield = ; @@ -16,10 +17,22 @@ const size = ; const trash = ; const color = ; - -function ShoppingItem({ name, price, img, id }) { +function ShoppingItem({ name, price, img, id, setProducts }) { function deleteProductHandler() { - + console.log("delete"); + axiosInstance + .post(`accounts/delete_from_cart/`, { + data: id, + }) + .then((res) => { + if (res.status === 200) { + axiosInstance.get(`/accounts/show_cart/`).then((res) => { + if (res.status === 200) { + setProducts(res.data); + } + }); + } + }); } return (
@@ -57,7 +70,6 @@ function ShoppingItem({ name, price, img, id }) {
- {shield} @@ -81,7 +93,10 @@ function ShoppingItem({ name, price, img, id }) {
- diff --git a/src/pages/ShoppingList/index.js b/src/pages/ShoppingList/index.js index 965550b..aeb1037 100644 --- a/src/pages/ShoppingList/index.js +++ b/src/pages/ShoppingList/index.js @@ -9,6 +9,7 @@ import { useInView } from "react-intersection-observer"; import { useEffect, useState } from "react"; import axiosInstance from "../../axios"; import { PuffLoader } from "react-spinners"; +import { useNavigate } from "react-router-dom"; const override = ` display: inline-block; @@ -18,12 +19,13 @@ const override = ` function ShoppingList() { let [loading, setLoading] = useState(true); let [list, setList] = useState([]); + const navigate = useNavigate(); useEffect(() => { axiosInstance.get(`/accounts/show_cart/`).then((res) => { if (res.status === 200) { - setLoading(false); setList(res.data); + setLoading(false); } }); }, []); @@ -31,6 +33,18 @@ function ShoppingList() { const { ref, inView, entry } = useInView({ threshold: 1, }); + + function confirmShoppingHandler() { + axiosInstance.get(`/accounts/checkout/`).then((res) => { + if (res.status === 200) { + // setList(res.data); + // setLoading(false); + navigate("/user-panel/orders", { replace: true }); + } + }); + console.log("/accounts/checkout/"); + } + return ( <> @@ -53,21 +67,22 @@ function ShoppingList() { سبد خرید - {digitsEnToFa(1)} + {digitsEnToFa(list.products.length)}
- {list.map((element) => ( + {console.log(list.total_price)} + {list.products.map((element) => ( + id={element.id} + name={element.product_name} + price={element.product_price} + img={element.upload} + setProducts={setList} + /> ))} -
- {digitsEnToFa(addCommas(2000000))} تومان + {digitsEnToFa(addCommas(list.total_price))} تومان
@@ -101,7 +116,8 @@ function ShoppingList() { - {digitsEnToFa(addCommas(2000000))} تومان + {digitsEnToFa(addCommas(list.total_price_with_discount))}{" "} + تومان
@@ -111,6 +127,7 @@ function ShoppingList() { diff --git a/src/pages/UserPanel/AdditionalQuestion/index.js b/src/pages/UserPanel/AdditionalQuestion/index.js index 547a90e..b9ea771 100644 --- a/src/pages/UserPanel/AdditionalQuestion/index.js +++ b/src/pages/UserPanel/AdditionalQuestion/index.js @@ -49,12 +49,11 @@ function AdditionalQuestion() { const { user } = useContext(UserContext); let [loading, setLoading] = useState(false); - // let [questions, setQuestions] = useState([]); + let [questions, setQuestions] = useState([]); const [answers, setAnswers] = useState({}); const navigate = useNavigate(); const setAnswerHandler = useCallback((id, option) => { - // console.log(answers); setAnswers((answers) => ({ ...answers, [id]: option })); }, []); console.log(answers); @@ -81,8 +80,8 @@ function AdditionalQuestion() { axiosInstance.get(`/questions/get_pics/`).then((res) => { if (res.status === 200) { setLoading(false); - console.log(res) - // setQuestions(res.data); + console.log(res); + setQuestions(res.data); } }); }, []); diff --git a/src/pages/UserPanel/Favorites/index.js b/src/pages/UserPanel/Favorites/index.js index beb8d7a..cbade71 100644 --- a/src/pages/UserPanel/Favorites/index.js +++ b/src/pages/UserPanel/Favorites/index.js @@ -15,6 +15,7 @@ const override = ` function Favorites() { let [loading, setLoading] = useState(true); let [favorites, setFavorites] = useState([]); + console.log(favorites); useEffect(() => { axiosInstance.get(`/accounts/show_favorite/`).then((res) => { @@ -27,7 +28,20 @@ function Favorites() { function favoriteHandler(e) { console.log(e.target); - console.log(e.target.id); + console.log(e.target.id + ""); + + axiosInstance + .post(`accounts/delete_from_favorite/`, { + data: e.target.id, + }) + .then((res) => { + if (res.status === 200) { + console.log(favorites); + favorites.map((el) => console.log(Number(el.id))); + setFavorites((prev) => prev.filter((el) => el.id != e.target.id)); + console.log(favorites); + } + }); } return ( diff --git a/src/pages/UserPanel/Orders/index.js b/src/pages/UserPanel/Orders/index.js index cfc58c1..0f20327 100644 --- a/src/pages/UserPanel/Orders/index.js +++ b/src/pages/UserPanel/Orders/index.js @@ -14,7 +14,7 @@ function Orders() { let [orders, setOrders] = useState([]); useEffect(() => { - axiosInstance.get(``).then((res) => { + axiosInstance.get(`/accounts/user_orders`).then((res) => { if (res.status === 200) { setLoading(false); setOrders(res.data);