|
| 1 | +import React from "react"; |
| 2 | +import { Route, Routes, BrowserRouter } from "react-router-dom"; |
| 3 | +//Componnets |
| 4 | +import HeaderNav from "./components/HeaderNav.tsx"; |
| 5 | +import FooterNav from "./components/FooterNav.tsx"; |
| 6 | +import MenuRoot from "./components/MenuRoot"; |
| 7 | +import RootSection from "./components/RootSection"; |
| 8 | +import ContactRoot from "./components/ContactRoot"; |
| 9 | +import SushiRoot from "./components/SushiRoot"; |
| 10 | +import SaleRoot from "./components/SaleRoot"; |
| 11 | +import PastaRoot from "./components/PastaRoot"; |
| 12 | +import DrinksRoot from "./components/DrinksRoot"; |
| 13 | +import AboutUs from "./components/AboutUs"; |
| 14 | +import Blog from "./components/Blog"; |
| 15 | +import Cart from "./components/Cart"; |
| 16 | +import PasswordRecovery from "./components/PasswordRecovery.js"; |
| 17 | +import Register from "./components/Register"; |
| 18 | +//Data |
| 19 | +import { allProducts } from "./data/AllProducts"; |
| 20 | +export default class App extends React.Component { |
| 21 | + constructor() { |
| 22 | + super(); |
| 23 | + this.state = { |
| 24 | + cartItems: [], |
| 25 | + }; |
| 26 | + } |
| 27 | + CheckRepeatableProducts = ( |
| 28 | + cartItems, |
| 29 | + targetProduct, |
| 30 | + userSelectedAttributes |
| 31 | + ) => { |
| 32 | + let item; |
| 33 | + let productsById = cartItems.filter((item) => item.id === targetProduct.id); |
| 34 | + productsById.forEach((targetProduct) => { |
| 35 | + if (this.MatchingAttributes(userSelectedAttributes, targetProduct)) { |
| 36 | + item = targetProduct; |
| 37 | + } |
| 38 | + }); |
| 39 | + return item; |
| 40 | + }; |
| 41 | + updateCartQuantity( |
| 42 | + actionToPerfrom, |
| 43 | + productAlreadyInCart, |
| 44 | + userSelectedAttributes |
| 45 | + ) { |
| 46 | + const repeatableProduct = this.CheckRepeatableProducts( |
| 47 | + this.state.cartItems, |
| 48 | + productAlreadyInCart, |
| 49 | + userSelectedAttributes |
| 50 | + ); |
| 51 | + //Find the target product to update by index |
| 52 | + const indexOfRepeatableProduct = |
| 53 | + this.state.cartItems.indexOf(repeatableProduct); |
| 54 | + const currentProductList = [...this.state.cartItems]; |
| 55 | + //Check type of action |
| 56 | + if (actionToPerfrom === "addProduct") { |
| 57 | + currentProductList[indexOfRepeatableProduct].quantity += 1; |
| 58 | + } else { |
| 59 | + currentProductList[indexOfRepeatableProduct].quantity -= 1; |
| 60 | + } |
| 61 | + |
| 62 | + return currentProductList; |
| 63 | + } |
| 64 | + handleAddProduct = (targetProduct, userSelectedAttributes) => { |
| 65 | + let updatedProductList; |
| 66 | + const productAlreadyInCart = this.CheckRepeatableProducts( |
| 67 | + this.state.cartItems, |
| 68 | + targetProduct, |
| 69 | + userSelectedAttributes |
| 70 | + ); |
| 71 | + |
| 72 | + if (productAlreadyInCart) { |
| 73 | + updatedProductList = this.updateCartQuantity( |
| 74 | + "addProduct", |
| 75 | + productAlreadyInCart, |
| 76 | + userSelectedAttributes |
| 77 | + ); |
| 78 | + } else { |
| 79 | + let modifiedProduct = JSON.parse(JSON.stringify(targetProduct)); |
| 80 | + let clone; |
| 81 | + |
| 82 | + for (let i = 0; i < targetProduct?.attributes?.length; i++) { |
| 83 | + for (let j = 0; j < targetProduct?.attributes[i]?.items?.length; j++) { |
| 84 | + if ( |
| 85 | + targetProduct.attributes[i].items[j].value === |
| 86 | + userSelectedAttributes[i].value |
| 87 | + ) { |
| 88 | + clone = { |
| 89 | + ...targetProduct.attributes[i].items[j], |
| 90 | + }; |
| 91 | + clone.isSelected = true; |
| 92 | + |
| 93 | + modifiedProduct.attributes[i].items[j].isSelected = true; |
| 94 | + |
| 95 | + modifiedProduct.attributes[i].items[j] = { |
| 96 | + ...clone, |
| 97 | + }; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + updatedProductList = [ |
| 103 | + ...this.state.cartItems, |
| 104 | + { |
| 105 | + ...modifiedProduct, |
| 106 | + userSelectedAttributes, |
| 107 | + quantity: 1, |
| 108 | + }, |
| 109 | + ]; |
| 110 | + } |
| 111 | + this.setState({ cartItems: updatedProductList }); |
| 112 | + //save to local storage |
| 113 | + localStorage.setItem("cartItems", JSON.stringify(updatedProductList)); |
| 114 | + //Update cart amount |
| 115 | + if (updatedProductList.length <= 1) { |
| 116 | + updatedProductList.map((item) => { |
| 117 | + //save to local storage |
| 118 | + localStorage.setItem("productsQuantity", JSON.stringify(item.quantity)); |
| 119 | + return this.setState({ productsQuantity: item.quantity }); |
| 120 | + }); |
| 121 | + } else { |
| 122 | + let productListArray = updatedProductList.map((item) => item.quantity); |
| 123 | + let sum = productListArray.reduce((a, b) => a + b, 0); |
| 124 | + this.setState({ productsQuantity: sum }); |
| 125 | + //save to local storage |
| 126 | + localStorage.setItem("productsQuantity", JSON.stringify(sum)); |
| 127 | + } |
| 128 | + }; |
| 129 | + // Remove Product From Cart |
| 130 | + handleRemoveProduct = (targetProduct, userSelectedAttributes) => { |
| 131 | + let updatedProductList; |
| 132 | + let repeatableProduct = this.CheckRepeatableProducts( |
| 133 | + this.state.cartItems, |
| 134 | + targetProduct, |
| 135 | + userSelectedAttributes |
| 136 | + ); |
| 137 | + if (repeatableProduct.quantity > 1) { |
| 138 | + updatedProductList = this.updateCartQuantity( |
| 139 | + "removeProduct", |
| 140 | + repeatableProduct, |
| 141 | + userSelectedAttributes |
| 142 | + ); |
| 143 | + } else { |
| 144 | + const products = [...this.state.cartItems]; |
| 145 | + const indexOfProduct = products.indexOf(repeatableProduct); |
| 146 | + products.splice(indexOfProduct, 1); |
| 147 | + updatedProductList = products; |
| 148 | + } |
| 149 | + this.setState({ cartItems: updatedProductList }); |
| 150 | + //save to local storage |
| 151 | + localStorage.setItem("cartItems", JSON.stringify(updatedProductList)); |
| 152 | + |
| 153 | + //Update cart amount |
| 154 | + if (updatedProductList.length <= 1) { |
| 155 | + updatedProductList.map((item) => { |
| 156 | + //save to local storage |
| 157 | + localStorage.setItem("productsQuantity", JSON.stringify(item.quantity)); |
| 158 | + |
| 159 | + return this.setState({ productsQuantity: item.quantity }); |
| 160 | + }); |
| 161 | + } else { |
| 162 | + let productListArray = updatedProductList.map((item) => item.quantity); |
| 163 | + let sum = productListArray.reduce((a, b) => a + b); |
| 164 | + this.setState({ productsQuantity: sum }); |
| 165 | + //save to local storage |
| 166 | + localStorage.setItem("productsQuantity", JSON.stringify(sum)); |
| 167 | + } |
| 168 | + if (updatedProductList.length === 0) { |
| 169 | + this.setState({ productsQuantity: 0 }); |
| 170 | + //save to local storage |
| 171 | + localStorage.setItem("productsQuantity", JSON.stringify(0)); |
| 172 | + } |
| 173 | + }; |
| 174 | + componentDidMount() { |
| 175 | + console.log(allProducts) |
| 176 | + } |
| 177 | + render() { |
| 178 | + console.log(this.state.cartItems); |
| 179 | + return ( |
| 180 | + <BrowserRouter> |
| 181 | + <HeaderNav /> |
| 182 | + <RootSection /> |
| 183 | + <Routes> |
| 184 | + <Route path="/pizza-time-with-react" element={<RootSection />} /> |
| 185 | + <Route path="/pizza" element={<MenuRoot />} /> |
| 186 | + <Route path="/contact" element={<ContactRoot />} /> |
| 187 | + <Route path="/cart" element={<Cart />} /> |
| 188 | + <Route path="/blog" element={<Blog />} /> |
| 189 | + <Route path="/about" element={<AboutUs />} /> |
| 190 | + <Route path="/sushi" element={<SushiRoot />} /> |
| 191 | + <Route path="/sale" element={<SaleRoot />} /> |
| 192 | + <Route path="/pasta" element={<PastaRoot />} /> |
| 193 | + <Route path="/drinks" element={<DrinksRoot />} /> |
| 194 | + <Route path="/register" element={<Register />} /> |
| 195 | + <Route path="/password-recovery" element={<PasswordRecovery />} /> |
| 196 | + </Routes> |
| 197 | + <FooterNav /> |
| 198 | + </BrowserRouter> |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
0 commit comments