diff --git a/package.json b/package.json index 91dcbdd..fd868b4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "@testing-library/user-event": "^7.1.2", "react": "^17.0.1", "react-dom": "^17.0.1", - "react-scripts": "^4.0.2" + "react-redux": "^8.0.5", + "react-scripts": "^4.0.2", + "redux": "^4.2.0" }, "scripts": { "start": "react-scripts start", diff --git a/src/components/Cart.js b/src/components/Cart.js index c51b442..ea6334d 100644 --- a/src/components/Cart.js +++ b/src/components/Cart.js @@ -1,5 +1,6 @@ -import React from 'react'; -import CartItem from './CartItem'; +import React from "react"; +import CartItem from "./CartItem"; +import { connect } from "react-redux"; class Cart extends React.Component { constructor(props) { @@ -14,46 +15,65 @@ class Cart extends React.Component { open = () => { this.setState({ isOpen: true }); }; + totalItemsInCart = () => { + return this.props.cart.reduce((acc, cur) => { + acc += cur.quantity; + return acc; + }, 0); + }; + subTotal = () => { + return this.props.cart.reduce((acc, cur) => { + acc += cur.price * cur.quantity; + return acc; + }, 0); + }; + alertSubTotal = () => { + alert(`Checkout - Subtotal: $ ${this.subTotal().toFixed(2)}`); + }; render() { const { isOpen } = this.state; if (!isOpen) { - return ; + return ( + + ); } return ( -