From 0fc62d125b1a5467d5e3f4a9b1e723f3bb5470b1 Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 16 Feb 2023 18:00:47 -0500 Subject: [PATCH 01/28] added carts --- client/src/views/Cart.jsx | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 client/src/views/Cart.jsx diff --git a/client/src/views/Cart.jsx b/client/src/views/Cart.jsx new file mode 100644 index 000000000..e69de29bb From cca93e5107e24eb8c2ba0a7911cf68d51256e08f Mon Sep 17 00:00:00 2001 From: s-orug Date: Sun, 19 Feb 2023 12:43:01 -0500 Subject: [PATCH 02/28] new items can be added --- client/src/App.js | 2 + client/src/components/NavBar/NavBar.jsx | 14 +- client/src/components/NavBar/index.jsx | 2 + client/src/index.js | 14 +- client/src/views/Aboutus.jsx | 2 + client/src/views/BooksPageGenerator.jsx | 143 ++++++++------- client/src/views/Browse.jsx | 7 + client/src/views/Cart.jsx | 0 client/src/views/Cart/Cart.jsx | 43 +++++ client/src/views/Cart/CartItems.jsx | 28 +++ client/src/views/Cart/CurrentCart.js | 3 + package-lock.json | 226 +++++++++++++++++++++++- package.json | 4 + 13 files changed, 409 insertions(+), 79 deletions(-) delete mode 100644 client/src/views/Cart.jsx create mode 100644 client/src/views/Cart/Cart.jsx create mode 100644 client/src/views/Cart/CartItems.jsx create mode 100644 client/src/views/Cart/CurrentCart.js diff --git a/client/src/App.js b/client/src/App.js index 12fc7874b..d5d83ff79 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -1,6 +1,8 @@ import "./App.css"; import CompleteNavbar from "./components/NavBar"; import { BrowserRouter, Routes } from "react-router-dom"; +import { CartProvider } from "react-use-cart"; +import Main_Cart from "./views/Cart/Cart"; function App() { return ( diff --git a/client/src/components/NavBar/NavBar.jsx b/client/src/components/NavBar/NavBar.jsx index 9560b40fe..ae5639afd 100644 --- a/client/src/components/NavBar/NavBar.jsx +++ b/client/src/components/NavBar/NavBar.jsx @@ -1,5 +1,6 @@ import React from "react"; import { useNavigate } from "react-router-dom"; +import { IoMdCart } from "react-icons/io"; const NavBar = ({ user }) => { const navigate = useNavigate(); @@ -13,8 +14,6 @@ const NavBar = ({ user }) => { } }; - - return (

- - + */}
diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 21465e253..8cf5c299d 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -4,13 +4,36 @@ import { Button } from "@mui/material"; function clear_cart(){ localStorage.setItem("books_cart", JSON.stringify([])); + localStorage.setItem("booksCartNames", JSON.stringify({})); window.location.reload(false) } +function addItem(book) { + var books_cart = JSON.parse(localStorage.getItem("books_cart")); + if (books_cart == null) { + books_cart = []; + } + + var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); + if(booksCartNames == null) { + booksCartNames = []; + } + booksCartNames.push(book.name); + + booksCartNames = booksCartNames.reduce(function (prev, cur) { + prev[cur] = (prev[cur] || 0) + 1; + return prev; + }, {}); + + books_cart.push(book); + localStorage.setItem("books_cart", JSON.stringify(books_cart)); + localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); + console.log(booksCartNames); +} + const Main_Cart = () => { var books_cart = JSON.parse(localStorage.getItem("books_cart")); - console.log(books_cart); - + return (
diff --git a/client/src/views/Login.jsx b/client/src/views/Login.jsx index e2c529b6f..3aea9271b 100644 --- a/client/src/views/Login.jsx +++ b/client/src/views/Login.jsx @@ -45,11 +45,11 @@ const Login = () => {
-
@@ -78,7 +78,7 @@ const Login = () => {
- + */}
    diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 8cf5c299d..c6de21f05 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -1,11 +1,10 @@ import { CartProvider, useCart } from "react-use-cart"; import { Button } from "@mui/material"; - -function clear_cart(){ +function clear_cart() { localStorage.setItem("books_cart", JSON.stringify([])); localStorage.setItem("booksCartNames", JSON.stringify({})); - window.location.reload(false) + window.location.reload(false); } function addItem(book) { @@ -15,7 +14,7 @@ function addItem(book) { } var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); - if(booksCartNames == null) { + if (booksCartNames == null) { booksCartNames = []; } booksCartNames.push(book.name); @@ -33,12 +32,14 @@ function addItem(book) { const Main_Cart = () => { var books_cart = JSON.parse(localStorage.getItem("books_cart")); + var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); + var bookNames = Object.keys(booksCartNames); - + console.log(booksCartNames); return (
    - +
    {books_cart.map((book) => ( @@ -59,6 +60,12 @@ const Main_Cart = () => {
    ))}
    + +
    + {Object.keys(booksCartNames).map((bookName) => ( +
    {bookName}
    + ))} +
); }; From 8b65738a741cdd40e30d8d0de94a11cd734f313f Mon Sep 17 00:00:00 2001 From: s-orug Date: Tue, 21 Feb 2023 19:36:46 -0500 Subject: [PATCH 05/28] cart contains items --- client/src/views/BooksPageGenerator.jsx | 2 +- client/src/views/Cart/Cart.jsx | 58 +++++++++++++++---------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/client/src/views/BooksPageGenerator.jsx b/client/src/views/BooksPageGenerator.jsx index 24a890b1b..dae4be659 100644 --- a/client/src/views/BooksPageGenerator.jsx +++ b/client/src/views/BooksPageGenerator.jsx @@ -33,7 +33,7 @@ const BooksPageGenerator = ({ book }) => { console.log(booksCartNames); } - // Increase Quantity + function add(quantity) { setQuantity(quantity + 1); } diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index c6de21f05..86bf6d0e8 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -1,5 +1,7 @@ import { CartProvider, useCart } from "react-use-cart"; +import { Grid, Chip, Avatar } from "@mui/material"; import { Button } from "@mui/material"; +import books from "../Books"; function clear_cart() { localStorage.setItem("books_cart", JSON.stringify([])); @@ -30,40 +32,50 @@ function addItem(book) { console.log(booksCartNames); } +function getBook(books, bookName) { + for (let i = 0; i < books.length; ++i) { + if (books[i].name == bookName) { + console.log(books[i]); + return books[i]; + } + } +} + const Main_Cart = () => { var books_cart = JSON.parse(localStorage.getItem("books_cart")); var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); var bookNames = Object.keys(booksCartNames); - console.log(booksCartNames); + console.log(books_cart); return (
-
- {books_cart.map((book) => ( - - ))} -
- -
+
{Object.keys(booksCartNames).map((bookName) => ( -
{bookName}
+
+ + + + + {getBook(books, bookName).name} by{" "} + {getBook(books, bookName).author} + + + +
+ +
+
+ +
    + +
))}
From dd27e17298c5308db07fff354fc959d402367110 Mon Sep 17 00:00:00 2001 From: s-orug Date: Tue, 21 Feb 2023 20:12:03 -0500 Subject: [PATCH 06/28] update --- client/src/views/Cart/Cart.jsx | 46 ++++++++++++++++++++-------------- client/src/views/Home.jsx | 2 +- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 86bf6d0e8..2f375dd73 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -52,29 +52,37 @@ const Main_Cart = () => {
-
+
{Object.keys(booksCartNames).map((bookName) => ( + + //
+ //
+ // + //
+ // + //
+ //
+ //
+ // + // + // {getBook(books, bookName).name} by{" "} + // {getBook(books, bookName).author} + // + // + //
+ //
+ + // + //
    + // + //
- - - - {getBook(books, bookName).name} by{" "} - {getBook(books, bookName).author} - - - -
- -
-
-
    -
))}
diff --git a/client/src/views/Home.jsx b/client/src/views/Home.jsx index 7c63006b5..e46449602 100644 --- a/client/src/views/Home.jsx +++ b/client/src/views/Home.jsx @@ -6,7 +6,7 @@ const Home = () => { return (
-
+
Welcome
From d7410845083b15980fea969d783b61a7d0b18fb6 Mon Sep 17 00:00:00 2001 From: s-orug Date: Tue, 21 Feb 2023 23:59:59 -0500 Subject: [PATCH 07/28] cart contains the items with their corresponding data --- client/src/views/Cart/Cart.jsx | 85 ++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 2f375dd73..71e877e19 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -53,38 +53,63 @@ const Main_Cart = () => {
- {Object.keys(booksCartNames).map((bookName) => ( - - //
- //
- // - //
- // - //
- //
- //
- // - // - // {getBook(books, bookName).name} by{" "} - // {getBook(books, bookName).author} - // - // - //
- //
- - // - //
    - // - //
-
- + {/* {Object.keys(booksCartNames).map((bookName) => ( */} + {/*
+
+ +
+ +
+
+
+ + + {getBook(books, bookName).name} by{" "} + {getBook(books, bookName).author} + + +
+
+ +
    + +
*/} +
+ {Object.keys(booksCartNames).map((bookName) => ( +
+
+ +
+
+

+ {getBook(books, bookName).name} by{" "} + {getBook(books, bookName).author} +

+

+ Price: ${getBook(books, bookName).price} +

+

+ Quantity: {booksCartNames[bookName]} +

+
+
+ ))}
- ))} + + {/* ))} */}
); From 88eeac55f036c8e9a04b442552e7df4ac248e5ca Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 00:24:16 -0500 Subject: [PATCH 08/28] price for the cart added --- client/src/views/Cart/Cart.jsx | 125 ++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 33 deletions(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 71e877e19..2f8d57d07 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -1,4 +1,6 @@ import { CartProvider, useCart } from "react-use-cart"; +import { React, useState } from "react"; +import { Remove, Add } from "@mui/icons-material"; import { Grid, Chip, Avatar } from "@mui/material"; import { Button } from "@mui/material"; import books from "../Books"; @@ -35,18 +37,49 @@ function addItem(book) { function getBook(books, bookName) { for (let i = 0; i < books.length; ++i) { if (books[i].name == bookName) { - console.log(books[i]); return books[i]; } } } +function round(value, decimals) { + return Number(Math.round(value+'e'+decimals)+'e-'+decimals); +} + +function calculatePrice(books, booksCartNames) { + let finalPrice = 0; + var bookNames = Object.keys(booksCartNames); + console.log(bookNames.length) + for (let i = 0; i < bookNames.length; ++i) { + finalPrice = finalPrice + booksCartNames[bookNames[i]]*(getBook(books, bookNames[i]).price) ; + // console.log(booksCartNames[bookNames[i]]); + } + // console.log(finalPrice); + + return round(finalPrice,2); +} + + + + const Main_Cart = () => { var books_cart = JSON.parse(localStorage.getItem("books_cart")); var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); var bookNames = Object.keys(booksCartNames); + const bookQuantities = Object.values(booksCartNames) - console.log(books_cart); + const [quantity, setQuantity] = useState(bookQuantities); + function add(quantity) { + setQuantity(quantity + 1); + } + + function subtract(quantity) { + if (quantity > 0) { + setQuantity(quantity - 1); + } + } + + // console.log(books_cart); return (
@@ -54,7 +87,7 @@ const Main_Cart = () => {
{/* {Object.keys(booksCartNames).map((bookName) => ( */} - {/*
+ {/*
@@ -79,37 +112,63 @@ const Main_Cart = () => {
*/} -
- {Object.keys(booksCartNames).map((bookName) => ( -
-
- -
-
-

- {getBook(books, bookName).name} by{" "} - {getBook(books, bookName).author} -

-

- Price: ${getBook(books, bookName).price} -

-

- Quantity: {booksCartNames[bookName]} -

-
+
+ {Object.keys(booksCartNames).map((bookName) => ( +
+
+ +
+
+

+ {getBook(books, bookName).name} by{" "} + {getBook(books, bookName).author} +

+

+ Price: ${getBook(books, bookName).price} +

+

+ Quantity: {booksCartNames[bookName]} +
+ subtract(quantity)}> + + + } + label={

{quantity}

} + clickable + onDelete={() => add(quantity)} + deleteIcon={} + /> + +
+

- ))} -
- - {/* ))} */} +
+ ))} +
+ +
+

+ Total: ${calculatePrice(books, booksCartNames)} +

+

+

+
+
); From 288cd6264978ec6ca12be50eceae00a4270350ee Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 00:56:59 -0500 Subject: [PATCH 09/28] price calculator for the cart page added --- client/src/views/Cart/Cart.jsx | 89 +++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 2f8d57d07..39a930a1f 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -43,42 +43,50 @@ function getBook(books, bookName) { } function round(value, decimals) { - return Number(Math.round(value+'e'+decimals)+'e-'+decimals); + return Number(Math.round(value + "e" + decimals) + "e-" + decimals); } function calculatePrice(books, booksCartNames) { let finalPrice = 0; var bookNames = Object.keys(booksCartNames); - console.log(bookNames.length) for (let i = 0; i < bookNames.length; ++i) { - finalPrice = finalPrice + booksCartNames[bookNames[i]]*(getBook(books, bookNames[i]).price) ; - // console.log(booksCartNames[bookNames[i]]); + finalPrice = + finalPrice + + booksCartNames[bookNames[i]] * getBook(books, bookNames[i]).price; } - // console.log(finalPrice); - return round(finalPrice,2); + return round(finalPrice, 2); } - - - const Main_Cart = () => { - var books_cart = JSON.parse(localStorage.getItem("books_cart")); var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); - var bookNames = Object.keys(booksCartNames); - const bookQuantities = Object.values(booksCartNames) - const [quantity, setQuantity] = useState(bookQuantities); - function add(quantity) { - setQuantity(quantity + 1); + const [quantity, setQuantity] = useState(booksCartNames); + function add(booksCartNames, bookName) { + console.log("adding"); + booksCartNames[bookName] = booksCartNames[bookName] + 1; + setQuantity(booksCartNames); + localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); + // window.location.reload(); } - - function subtract(quantity) { - if (quantity > 0) { - setQuantity(quantity - 1); + + function subtract(booksCartNames, bookName) { + console.log("subing"); + if (quantity[bookName] > 0) { + booksCartNames[bookName] = booksCartNames[bookName] - 1; + setQuantity(booksCartNames); + localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); + // window.location.reload(); } } + function setZero(booksCartNames, bookName) { + booksCartNames[bookName] = 0; + setQuantity(booksCartNames); + localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); + window.location.reload(); + } + // console.log(books_cart); return (
@@ -137,22 +145,29 @@ const Main_Cart = () => {

Quantity: {booksCartNames[bookName]}
- subtract(quantity)}> - - - } - label={

{quantity}

} - clickable - onDelete={() => add(quantity)} - deleteIcon={} - /> - -
+ subtract(booksCartNames, bookName)} + > + + + } + label={ +

{quantity[bookName]}

+ } + clickable + onDelete={() => add(booksCartNames, bookName)} + deleteIcon={} + /> + +

@@ -165,10 +180,8 @@ const Main_Cart = () => {

Total: ${calculatePrice(books, booksCartNames)}

-

-

+

-
); From 81afcee02b08ffb0cac14cd680d9238108219787 Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 17:53:56 -0500 Subject: [PATCH 10/28] checkout button added --- client/package-lock.json | 18 ++++++++++++++++++ client/package.json | 1 + client/src/components/NavBar/index.jsx | 4 ++-- client/src/views/Cart/Cart.jsx | 24 +++++++++++++++++++++--- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 65a52a05e..ccf5c69e0 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -28,6 +28,7 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", + "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", @@ -26350,6 +26351,17 @@ "react-dom": ">=16.6.0" } }, + "node_modules/react-use-cart": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", + "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "react": ">=16" + } + }, "node_modules/reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", @@ -50650,6 +50662,12 @@ "prop-types": "^15.6.2" } }, + "react-use-cart": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", + "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", + "requires": {} + }, "reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", diff --git a/client/package.json b/client/package.json index 74bf01a95..a52bf6285 100644 --- a/client/package.json +++ b/client/package.json @@ -23,6 +23,7 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", + "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", diff --git a/client/src/components/NavBar/index.jsx b/client/src/components/NavBar/index.jsx index 64beb3cad..d4ecd7727 100644 --- a/client/src/components/NavBar/index.jsx +++ b/client/src/components/NavBar/index.jsx @@ -12,7 +12,7 @@ import Signup from "../../views/Signup"; import BooksPageGenerator from "../../views/BooksPageGenerator"; import books from "../../views/Books"; import ValidatedUsers from "../../views/ValidatedUsers"; -import Main_Cart from "../../views/Cart/Cart"; +import MainCart from "../../views/Cart/Cart"; const CompleteNavbar = () => { const user = localStorage.getItem("token"); @@ -33,7 +33,7 @@ const CompleteNavbar = () => { } /> } /> } /> - } /> + } /> {/* Admin */} } /> diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 39a930a1f..8f84b5548 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -4,6 +4,7 @@ import { Remove, Add } from "@mui/icons-material"; import { Grid, Chip, Avatar } from "@mui/material"; import { Button } from "@mui/material"; import books from "../Books"; +import { useNavigate } from "react-router-dom"; function clear_cart() { localStorage.setItem("books_cart", JSON.stringify([])); @@ -58,7 +59,20 @@ function calculatePrice(books, booksCartNames) { return round(finalPrice, 2); } -const Main_Cart = () => { +const MainCart = ({ currentUser }) => { + const navigate = useNavigate(); + + var puchaseBooks = (currentUser) => { + if (currentUser && currentUser.length !== 0) { + // localStorage.removeItem("token"); + localStorage.setItem("books_cart", JSON.stringify([])); + localStorage.setItem("booksCartNames", JSON.stringify({})); + window.location.reload(); + } else { + navigate("/login"); + } + }; + var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); const [quantity, setQuantity] = useState(booksCartNames); @@ -175,16 +189,20 @@ const Main_Cart = () => {

Total: ${calculatePrice(books, booksCartNames)}

+
); }; -export default Main_Cart; +export default MainCart; From 8d6f56923904be790f9102e138495fe3e8283d3c Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 18:29:24 -0500 Subject: [PATCH 11/28] changes to the cart for the backend integration --- client/src/views/BooksPageGenerator.jsx | 17 +++++++++++++++-- client/src/views/Cart/Cart.jsx | 23 ++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/client/src/views/BooksPageGenerator.jsx b/client/src/views/BooksPageGenerator.jsx index dae4be659..d4a982120 100644 --- a/client/src/views/BooksPageGenerator.jsx +++ b/client/src/views/BooksPageGenerator.jsx @@ -5,6 +5,19 @@ import { Grid, Chip, Avatar } from "@mui/material"; import { Remove, Add } from "@mui/icons-material"; import Popup from "reactjs-popup"; + +function getKeys(obj) { + var keys = []; + iterate(obj, function (oVal, oKey) { keys.push(oKey) }); + return keys; +} +function iterate(iterable, callback) { + for (var key in iterable) { + if (key === 'length' || key === 'prototype' || !Object.prototype.hasOwnProperty.call(iterable, key)) continue; + callback(iterable[key], key, iterable); + } +} + const BooksPageGenerator = ({ book }) => { const [quantity, setQuantity] = useState(1); @@ -21,7 +34,7 @@ const BooksPageGenerator = ({ book }) => { // console.log(books_cart); var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); - var bookNames = Object.keys(booksCartNames); + var bookNames = getKeys(booksCartNames); if (bookNames.includes(book.name) == false) { booksCartNames[book.name] = 0; @@ -107,7 +120,7 @@ const BooksPageGenerator = ({ book }) => { onClick={() => addItem(book, quantity)} > Add to cart - + {/* +
{/* {Object.keys(booksCartNames).map((bookName) => ( */} @@ -137,7 +150,7 @@ const MainCart = ({ currentUser }) => {
- {Object.keys(booksCartNames).map((bookName) => ( + {getKeys(booksCartNames).map((bookName) => (
@@ -195,7 +208,7 @@ const MainCart = ({ currentUser }) => { Total: ${calculatePrice(books, booksCartNames)}

- From ac44d20f9e94726269677649ffcddb309dfc3e09 Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 19:09:49 -0500 Subject: [PATCH 12/28] default values in the cart --- client/src/views/BooksPageGenerator.jsx | 2 +- client/src/views/Cart/Cart.jsx | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/client/src/views/BooksPageGenerator.jsx b/client/src/views/BooksPageGenerator.jsx index d4a982120..0a1a27542 100644 --- a/client/src/views/BooksPageGenerator.jsx +++ b/client/src/views/BooksPageGenerator.jsx @@ -36,7 +36,7 @@ const BooksPageGenerator = ({ book }) => { var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); var bookNames = getKeys(booksCartNames); - if (bookNames.includes(book.name) == false) { + if (bookNames.includes(book.name) === false) { booksCartNames[book.name] = 0; // console.log(Object.keys(booksCartNames)); } diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 540b21b04..0c5605920 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -85,7 +85,10 @@ const MainCart = ({ currentUser }) => { } }; - var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); + var booksCartNames = { + "Steve Jobs": 22, + "Go-To-Dinners": 13 +};//JSON.parse(localStorage.getItem("booksCartNames")); const [quantity, setQuantity] = useState(booksCartNames); function add(booksCartNames, bookName) { From a44c2c93a99ad928df7ed645fb6186b5b216bb2e Mon Sep 17 00:00:00 2001 From: s-orug Date: Wed, 22 Feb 2023 22:57:38 -0500 Subject: [PATCH 13/28] cart has added checkout feature for the backend --- client/src/views/Cart/Cart.jsx | 42 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index 0c5605920..ef3686fec 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -49,12 +49,19 @@ function round(value, decimals) { function getKeys(obj) { var keys = []; - iterate(obj, function (oVal, oKey) { keys.push(oKey) }); + iterate(obj, function (oVal, oKey) { + keys.push(oKey); + }); return keys; } function iterate(iterable, callback) { for (var key in iterable) { - if (key === 'length' || key === 'prototype' || !Object.prototype.hasOwnProperty.call(iterable, key)) continue; + if ( + key === "length" || + key === "prototype" || + !Object.prototype.hasOwnProperty.call(iterable, key) + ) + continue; callback(iterable[key], key, iterable); } } @@ -64,8 +71,8 @@ function calculatePrice(books, booksCartNames) { var bookNames = getKeys(booksCartNames); for (let i = 0; i < bookNames.length; ++i) { finalPrice = - finalPrice - + booksCartNames[bookNames[i]] * getBook(books, bookNames[i]).price; + finalPrice + + booksCartNames[bookNames[i]] * getBook(books, bookNames[i]).price; } return round(finalPrice, 2); @@ -73,6 +80,7 @@ function calculatePrice(books, booksCartNames) { const MainCart = ({ currentUser }) => { const navigate = useNavigate(); + var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); var puchaseBooks = (currentUser) => { if (currentUser && currentUser.length !== 0) { @@ -85,10 +93,10 @@ const MainCart = ({ currentUser }) => { } }; - var booksCartNames = { - "Steve Jobs": 22, - "Go-To-Dinners": 13 -};//JSON.parse(localStorage.getItem("booksCartNames")); + // { + // "Steve Jobs": 22, + // "Go-To-Dinners": 13 + // }; const [quantity, setQuantity] = useState(booksCartNames); function add(booksCartNames, bookName) { @@ -96,7 +104,7 @@ const MainCart = ({ currentUser }) => { booksCartNames[bookName] = booksCartNames[bookName] + 1; setQuantity(booksCartNames); localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); - // window.location.reload(); + window.location.reload(); } function subtract(booksCartNames, bookName) { @@ -105,7 +113,7 @@ const MainCart = ({ currentUser }) => { booksCartNames[bookName] = booksCartNames[bookName] - 1; setQuantity(booksCartNames); localStorage.setItem("booksCartNames", JSON.stringify(booksCartNames)); - // window.location.reload(); + window.location.reload(); } } @@ -121,7 +129,6 @@ const MainCart = ({ currentUser }) => {
-
{/* {Object.keys(booksCartNames).map((bookName) => ( */} @@ -205,14 +212,17 @@ const MainCart = ({ currentUser }) => {
-

+

Total: ${calculatePrice(books, booksCartNames)}

-

-
From a6a1f792576e65346845128e852ccb050aea838a Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 00:04:41 -0500 Subject: [PATCH 14/28] checkpoint 1 --- client/src/components/NavBar/NavBar.jsx | 12 ++++--- client/src/components/UserRow.jsx | 1 + client/src/views/Cart/Cart.jsx | 46 +++++++++++++++++++++---- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/client/src/components/NavBar/NavBar.jsx b/client/src/components/NavBar/NavBar.jsx index 546f4fe07..3417802eb 100644 --- a/client/src/components/NavBar/NavBar.jsx +++ b/client/src/components/NavBar/NavBar.jsx @@ -25,19 +25,21 @@ const NavBar = ({ user }) => { -
+
navigate("/cart")} - className="icon" + className="" style={{ - position: "absolute", + position: "", top: "10px", - right: "20px", + right: "70px", }} size="40px" color="white" /> - -
+ {/* {console.log(currentUser)} + {console.log(token)} */} +
{/* {Object.keys(booksCartNames).map((bookName) => ( */} {/*
@@ -201,8 +228,13 @@ const MainCart = ({ currentUser }) => { className="pl-4" onClick={() => setZero(booksCartNames, bookName)} > - {" "} - Clear{" "} + Clear + +
@@ -210,7 +242,9 @@ const MainCart = ({ currentUser }) => {
))}
- +
+ +
From a287bf21c0f78bcf7685ebc3c8365f6072f1dabe Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 15:45:52 -0500 Subject: [PATCH 15/28] uninstall react-use-cart --- client/package-lock.json | 18 ------------------ client/package.json | 1 - 2 files changed, 19 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index ccf5c69e0..65a52a05e 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -28,7 +28,6 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", - "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", @@ -26351,17 +26350,6 @@ "react-dom": ">=16.6.0" } }, - "node_modules/react-use-cart": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", - "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", - "engines": { - "node": ">=10" - }, - "peerDependencies": { - "react": ">=16" - } - }, "node_modules/reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", @@ -50662,12 +50650,6 @@ "prop-types": "^15.6.2" } }, - "react-use-cart": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", - "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", - "requires": {} - }, "reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", diff --git a/client/package.json b/client/package.json index a52bf6285..74bf01a95 100644 --- a/client/package.json +++ b/client/package.json @@ -23,7 +23,6 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", - "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", From 040ea2f3724a63e1266a02481946b33e2366857b Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 15:52:52 -0500 Subject: [PATCH 16/28] update --- client/package-lock.json | 18 ++++++++++++++++++ client/package.json | 1 + 2 files changed, 19 insertions(+) diff --git a/client/package-lock.json b/client/package-lock.json index 65a52a05e..ccf5c69e0 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -28,6 +28,7 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", + "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", @@ -26350,6 +26351,17 @@ "react-dom": ">=16.6.0" } }, + "node_modules/react-use-cart": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", + "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "react": ">=16" + } + }, "node_modules/reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", @@ -50650,6 +50662,12 @@ "prop-types": "^15.6.2" } }, + "react-use-cart": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/react-use-cart/-/react-use-cart-1.13.0.tgz", + "integrity": "sha512-V3jrNOgBXxTn/RWh1HCEdrQk/jx2xCixdkX1c7Vyl4zxEf9jOuniUnjZSTALO7rQ5226onqmvXxKz1eu0Yz6Iw==", + "requires": {} + }, "reactjs-popup": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/reactjs-popup/-/reactjs-popup-2.0.5.tgz", diff --git a/client/package.json b/client/package.json index 74bf01a95..a52bf6285 100644 --- a/client/package.json +++ b/client/package.json @@ -23,6 +23,7 @@ "react-router-dom": "^6.6.1", "react-scripts": "5.0.1", "react-stars": "^2.2.5", + "react-use-cart": "^1.13.0", "reactjs-popup": "^2.0.5", "styled-components": "^5.3.6", "sweetalert2": "^11.7.1", From d97419fdaaabcd75d9170f45e825300a994aaae5 Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 16:00:52 -0500 Subject: [PATCH 17/28] update From e32cf6047a4b256a577144818606d2f0693bda8b Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 16:23:42 -0500 Subject: [PATCH 18/28] update --- backend/models/user.js | 1 + client/src/views/BooksPageGenerator.jsx | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/models/user.js b/backend/models/user.js index 5f9c41a9f..6745aaed6 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -10,6 +10,7 @@ const userSchema = new mongoose.Schema({ password: { type: String, required: true }, role: { type: String, required: true, enum: ["admin", "customer"]}, balance: { type: mongoose.Schema.Types.Decimal128, required: false }, + }); userSchema.methods.generateAuthToken = function () { diff --git a/client/src/views/BooksPageGenerator.jsx b/client/src/views/BooksPageGenerator.jsx index 0a1a27542..f6d1d3a9a 100644 --- a/client/src/views/BooksPageGenerator.jsx +++ b/client/src/views/BooksPageGenerator.jsx @@ -36,7 +36,7 @@ const BooksPageGenerator = ({ book }) => { var booksCartNames = JSON.parse(localStorage.getItem("booksCartNames")); var bookNames = getKeys(booksCartNames); - if (bookNames.includes(book.name) === false) { + if (bookNames.includes(book.name) !== null && !bookNames.includes(book.name)) { booksCartNames[book.name] = 0; // console.log(Object.keys(booksCartNames)); } From fedec6ffdc569dcd4316150cb7362da4c500a0ef Mon Sep 17 00:00:00 2001 From: s-orug Date: Thu, 23 Feb 2023 17:48:15 -0500 Subject: [PATCH 19/28] update --- client/src/views/Browse.jsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/client/src/views/Browse.jsx b/client/src/views/Browse.jsx index 51fb8b0f6..d3a59ddf7 100644 --- a/client/src/views/Browse.jsx +++ b/client/src/views/Browse.jsx @@ -81,13 +81,21 @@ const sort = (books, selection) => { }; const Browse = () => { - const [value, setValue] = useState("1"); - const book_instance = localStorage.getItem("Book"); - console.log("book_instance" + book_instance); + const [value, setValue] = useState(""); + const book_instance = localStorage.getItem("Book"); + function changeOption(option) { + setValue(option); + localStorage.setItem("option", option); + } - console.log() + let prevOption = localStorage.getItem("option"); + // if (prevOption) { + // setValue(prevOption); + // } + // console.log(prevOption); + // console.log(prevOption); return ( @@ -97,6 +105,9 @@ const Browse = () => { Browse
+

@@ -125,7 +136,7 @@ const Browse = () => {
updateCartItemCount(Number(e.target.value), id)} - /> - -
-
-
- ); -} - -export default CartItems \ No newline at end of file From 6fd0f491bc1d888757f1a6453bb6ef4c74a56ed1 Mon Sep 17 00:00:00 2001 From: s-orug Date: Sun, 26 Feb 2023 00:36:32 -0500 Subject: [PATCH 28/28] MongoDB's zero storing problem FIXED --- client/src/views/Cart/Cart.jsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client/src/views/Cart/Cart.jsx b/client/src/views/Cart/Cart.jsx index df525c14b..b72c28191 100644 --- a/client/src/views/Cart/Cart.jsx +++ b/client/src/views/Cart/Cart.jsx @@ -194,7 +194,12 @@ const MainCart = ({ currentUser }) => { if (currentUser && currentUser.length !== 0) { console.log(currentBalance); if (currentBalance - calculatePrice(books, booksCartNames) >= 0) { - data.balance = currentBalance - calculatePrice(books, booksCartNames); + if (currentBalance - calculatePrice(books, booksCartNames) === 0) { + data.balance = "0"; + } else { + data.balance = currentBalance - calculatePrice(books, booksCartNames); + } + console.log(data.balance); localStorage.setItem("books_cart", JSON.stringify([])); localStorage.setItem("booksCartNames", JSON.stringify({})); handleChange();