|
1 | 1 | import React from "react";
|
2 | 2 | import { Route, Routes, BrowserRouter } from "react-router-dom";
|
3 | 3 | //Componnets
|
4 |
| -import HeaderNav from "./components/HeaderNav.tsx"; |
5 |
| -import FooterNav from "./components/FooterNav.tsx"; |
| 4 | +import HeaderNav from "./components/HeaderNav.js"; |
| 5 | +import FooterNav from "./components/FooterNav.js"; |
6 | 6 | import MenuRoot from "./components/MenuRoot";
|
7 | 7 | import RootSection from "./components/RootSection";
|
8 | 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"; |
| 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 | 13 | import AboutUs from "./components/AboutUs";
|
14 | 14 | import Blog from "./components/Blog";
|
15 | 15 | import Cart from "./components/Cart";
|
16 | 16 | import PasswordRecovery from "./components/PasswordRecovery.js";
|
17 | 17 | import Register from "./components/Register";
|
18 | 18 | //Data
|
19 |
| -import { allProducts } from "./data/AllProducts"; |
| 19 | +import { allProductsData } from "./data/AllProductsData.js"; |
| 20 | +import { AllCategories } from "./data/AllCategories"; |
20 | 21 | export default class App extends React.Component {
|
21 | 22 | constructor() {
|
22 | 23 | super();
|
23 | 24 | this.state = {
|
| 25 | + allCategories: [], |
| 26 | + activeCategory: "Menu", |
24 | 27 | cartItems: [],
|
| 28 | + allProducts: [], |
25 | 29 | };
|
| 30 | + this.getProductsByCategory = this.getProductsByCategory.bind(this); |
| 31 | + this.changeCategory = this.changeCategory.bind(this); |
26 | 32 | }
|
| 33 | + |
| 34 | + // GET DATA |
| 35 | + /*******************************************************/ |
| 36 | + allCategoriesData = new Promise((resolve, reject) => { |
| 37 | + if (true) { |
| 38 | + resolve(AllCategories); |
| 39 | + return; |
| 40 | + } |
| 41 | + reject("error, check the code!"); |
| 42 | + }); |
| 43 | + allProductsData = new Promise((resolve, reject) => { |
| 44 | + if (true) { |
| 45 | + resolve(allProductsData); |
| 46 | + return; |
| 47 | + } |
| 48 | + reject("error, check the code!"); |
| 49 | + }); |
| 50 | + |
| 51 | + getCategories = async () => { |
| 52 | + try { |
| 53 | + const result = await this.allCategoriesData; |
| 54 | + this.setState({ allCategories: result }); |
| 55 | + } catch (error) { |
| 56 | + console.log(error); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + getAllProducts = async () => { |
| 61 | + try { |
| 62 | + const result = await this.allProductsData; |
| 63 | + this.setState({ allProducts: result }); |
| 64 | + } catch (error) { |
| 65 | + console.log(error); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // HANDLE CHANGE |
| 70 | + /*******************************************************/ |
| 71 | + changeCategory = async (newCategory) => { |
| 72 | + this.setState({ activeCategory: newCategory }); |
| 73 | + this.getProductsByCategory(newCategory); |
| 74 | + }; |
| 75 | + |
| 76 | + getProductsByCategory = async (category) => { |
| 77 | + let separateCategoriesByname = []; |
| 78 | + //Separate arrays by category names |
| 79 | + const separateCategories = allProductsData.reduce(function ( |
| 80 | + singleCategory, |
| 81 | + singleItem |
| 82 | + ) { |
| 83 | + separateCategoriesByname = Object.keys(singleCategory); |
| 84 | + |
| 85 | + if (!singleCategory[singleItem.Category]) |
| 86 | + singleCategory[singleItem.Category] = singleItem; |
| 87 | + else |
| 88 | + singleCategory[singleItem.Category] = Array.isArray( |
| 89 | + singleCategory[singleItem.Category] |
| 90 | + ) |
| 91 | + ? singleCategory[singleItem.Category].concat(singleItem) |
| 92 | + : [singleCategory[singleItem.Category]].concat(singleItem); |
| 93 | + return singleCategory; |
| 94 | + }, |
| 95 | + {}); |
| 96 | + |
| 97 | + const result = Object.keys(separateCategories).map( |
| 98 | + (e) => separateCategories[e] |
| 99 | + ); |
| 100 | + |
| 101 | + let singleCategoryArray = []; |
| 102 | + result.map((categories) => { |
| 103 | + singleCategoryArray = categories; |
| 104 | + }); |
| 105 | + //Change products by category |
| 106 | + separateCategoriesByname.forEach((cate) => { |
| 107 | + if (cate === category) { |
| 108 | + this.setState({ allProducts: separateCategories[category] }); |
| 109 | + } |
| 110 | + if (category === "Menu") { |
| 111 | + this.setState({ allProducts: allProductsData }); |
| 112 | + } |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
27 | 116 | CheckRepeatableProducts = (
|
28 | 117 | cartItems,
|
29 | 118 | targetProduct,
|
@@ -172,25 +261,37 @@ export default class App extends React.Component {
|
172 | 261 | }
|
173 | 262 | };
|
174 | 263 | componentDidMount() {
|
175 |
| - console.log(allProducts) |
| 264 | + this.getCategories(); |
| 265 | + this.getAllProducts(); |
| 266 | + this.getProductsByCategory(this.state.activeCategory); |
176 | 267 | }
|
177 | 268 | render() {
|
178 |
| - console.log(this.state.cartItems); |
| 269 | + // console.log(this.state.cartItems); |
179 | 270 | return (
|
180 | 271 | <BrowserRouter>
|
181 | 272 | <HeaderNav />
|
182 |
| - <RootSection /> |
183 | 273 | <Routes>
|
184 | 274 | <Route path="/pizza-time-with-react" element={<RootSection />} />
|
185 |
| - <Route path="/pizza" element={<MenuRoot />} /> |
| 275 | + <Route |
| 276 | + path="/menu" |
| 277 | + element={ |
| 278 | + <MenuRoot |
| 279 | + allProducts={this.state.allProducts} |
| 280 | + allCategories={this.state.allCategories} |
| 281 | + activeCategory={this.state.activeCategory} |
| 282 | + changeCategory={this.changeCategory} |
| 283 | + /> |
| 284 | + } |
| 285 | + /> |
| 286 | + {/* <Route path="/pizza" element={<MenuRoot />} /> */} |
186 | 287 | <Route path="/contact" element={<ContactRoot />} />
|
187 | 288 | <Route path="/cart" element={<Cart />} />
|
188 | 289 | <Route path="/blog" element={<Blog />} />
|
189 | 290 | <Route path="/about" element={<AboutUs />} />
|
190 |
| - <Route path="/sushi" element={<SushiRoot />} /> |
| 291 | + {/* <Route path="/sushi" element={<SushiRoot />} /> |
191 | 292 | <Route path="/sale" element={<SaleRoot />} />
|
192 | 293 | <Route path="/pasta" element={<PastaRoot />} />
|
193 |
| - <Route path="/drinks" element={<DrinksRoot />} /> |
| 294 | + <Route path="/drinks" element={<DrinksRoot />} /> */} |
194 | 295 | <Route path="/register" element={<Register />} />
|
195 | 296 | <Route path="/password-recovery" element={<PasswordRecovery />} />
|
196 | 297 | </Routes>
|
|
0 commit comments