Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve bug of product disappear from cart and basket state sync issue in firebase document #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions src/components/basket/Basket.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
/* eslint-disable max-len */
import { BasketItem, BasketToggle } from '@/components/basket';
import { Boundary, Modal } from '@/components/common';
import { CHECKOUT_STEP_1 } from '@/constants/routes';
import firebase from 'firebase/firebase';
import { calculateTotal, displayMoney } from '@/helpers/utils';
import { useDidMount, useModal } from '@/hooks';
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useLocation } from 'react-router-dom';
import { clearBasket } from '@/redux/actions/basketActions';
import { BasketItem, BasketToggle } from "@/components/basket";
import { Boundary, Modal } from "@/components/common";
import { CHECKOUT_STEP_1 } from "@/constants/routes";
import firebase from "@/services/firebase";
import { calculateTotal, displayMoney } from "@/helpers/utils";
import { useDidMount, useModal } from "@/hooks";
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useHistory, useLocation } from "react-router-dom";
import { clearBasket } from "@/redux/actions/basketActions";

const Basket = () => {
const { isOpenModal, onOpenModal, onCloseModal } = useModal();
const { basket, user } = useSelector((state) => ({
basket: state.basket,
user: state.auth
user: state.auth,
}));
const history = useHistory();
const { pathname } = useLocation();
const dispatch = useDispatch();
const didMount = useDidMount();

useEffect(() => {
if (didMount && firebase.auth.currentUser && basket.length !== 0) {
firebase.saveBasketItems(basket, firebase.auth.currentUser.uid)
if (didMount && firebase.auth.currentUser) {
firebase
.saveBasketItems(
basket.length === 0 ? [] : basket,
firebase.auth.currentUser.uid
)
.then(() => {
console.log('Item saved to basket');
console.log("Item saved to basket");
})
.catch((e) => {
console.log(e);
});
}
}, [basket.length]);
}, [basket]);

const onCheckOut = () => {
if ((basket.length !== 0 && user)) {
document.body.classList.remove('is-basket-open');
if (basket.length !== 0 && user) {
document.body.classList.remove("is-basket-open");
history.push(CHECKOUT_STEP_1);
} else {
onOpenModal();
Expand All @@ -44,7 +48,7 @@ const Basket = () => {

const onSignInClick = () => {
onCloseModal();
document.body.classList.remove('basket-open');
document.body.classList.remove("basket-open");
history.push(CHECKOUT_STEP_1);
};

Expand All @@ -54,12 +58,9 @@ const Basket = () => {
}
};

return user && user.role === 'ADMIN' ? null : (
return user && user.role === "ADMIN" ? null : (
<Boundary>
<Modal
isOpen={isOpenModal}
onRequestClose={onCloseModal}
>
<Modal isOpen={isOpenModal} onRequestClose={onCloseModal}>
<p className="text-center">You must sign in to continue checking out</p>
<br />
<div className="d-flex-center">
Expand All @@ -86,9 +87,7 @@ const Basket = () => {
<h3 className="basket-header-title">
My Basket &nbsp;
<span>
(
{` ${basket.length} ${basket.length > 1 ? 'items' : 'item'}`}
)
({` ${basket.length} ${basket.length > 1 ? "items" : "item"}`})
</span>
</h3>
<BasketToggle>
Expand Down Expand Up @@ -130,12 +129,16 @@ const Basket = () => {
<div className="basket-total">
<p className="basket-total-title">Subtotal Amout:</p>
<h2 className="basket-total-amount">
{displayMoney(calculateTotal(basket.map((product) => product.price * product.quantity)))}
{displayMoney(
calculateTotal(
basket.map((product) => product.price * product.quantity)
)
)}
</h2>
</div>
<button
className="basket-checkout-button button"
disabled={basket.length === 0 || pathname === '/checkout'}
disabled={basket.length === 0 || pathname === "/checkout"}
onClick={onCheckOut}
type="button"
>
Expand Down