Skip to content
Open
Show file tree
Hide file tree
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
40,428 changes: 40,175 additions & 253 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
"prop-types": "^15.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-icons": "^4.3.1",
"react-loader-spinner": "^4.0.0",
"react-loaders": "^3.0.1",
"react-phone-number-input": "^3.1.34",
"react-redux": "^7.2.3",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-select": "^5.1.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"sass": "^1.32.11",
"uuid": "^8.3.2",
"web-vitals": "^1.1.1",
"yup": "^0.32.9"
"yup": "^0.32.11"
},
"devDependencies": {
"@babel/core": "^7.13.16",
Expand Down
220 changes: 88 additions & 132 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import React, { Component } from "react";
import { BrowserRouter, Route } from "react-router-dom";
import React, { useState, useEffect } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";

import Home from "./pages/Home";
import NewProduct from "./pages/NewProduct";

import * as api from "./api";

const LOCAL_STORAGE_KEY = "react-sc-state";
import FirstStep from "./pages/Checkout/FirstStep";
import SecondStep from "./pages/Checkout/SecondStep";
import ThirdStep from "./pages/Checkout/ThirdStep";
import LastStep from "./pages/Checkout/LastStep";

function loadLocalStorageData() {
const prevItems = localStorage.getItem(LOCAL_STORAGE_KEY);

if (!prevItems) {
return null;
}
import * as api from "./api";

try {
return JSON.parse(prevItems);
} catch (error) {
return null;
}
}
import useLocalStorage from "./hooks/localStorage";
import loadLocalStorageItems from "./utils/loadLocalStorage";
import StateProvider from "./context/contextState";

function buildNewCartItem(cartItem) {
if (cartItem.quantity >= cartItem.unitsInStock) {
Expand All @@ -39,62 +32,43 @@ function buildNewCartItem(cartItem) {
};
}

class App extends Component {
constructor(props) {
super(props);

this.state = {
products: [],
cartItems: [],
isLoading: false,
hasError: false,
loadingError: null,
};

this.handleAddToCart = this.handleAddToCart.bind(this);
this.handleRemove = this.handleRemove.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleDownVote = this.handleDownVote.bind(this);
this.handleUpVote = this.handleUpVote.bind(this);
this.handleSetFavorite = this.handleSetFavorite.bind(this);
this.saveNewProduct = this.saveNewProduct.bind(this);
}

componentDidMount() {
const prevItems = loadLocalStorageData();

if (!prevItems) {
this.setState({
isLoading: true,
});

api.getProducts().then((data) => {
this.setState({
products: data,
isLoading: false,
const PRODUCTS_LOCAL_STORAGE_KEY = "react-sc-state-products";
const CART_ITEMS_LOCAL_STORAGE_KEY = "react-sc-state-cart-items";

function App() {
const [products, setProducts] = useState(() =>
loadLocalStorageItems(PRODUCTS_LOCAL_STORAGE_KEY, []),
);
const [cartItems, setCartItems] = useState(() =>
loadLocalStorageItems(CART_ITEMS_LOCAL_STORAGE_KEY, []),
);

useLocalStorage(products, PRODUCTS_LOCAL_STORAGE_KEY);
useLocalStorage(cartItems, CART_ITEMS_LOCAL_STORAGE_KEY);

const [isLoading, setIsLoading] = useState(false);
const [hasError, setHasError] = useState(false);
const [loadingError, setLoadingError] = useState(null);

useEffect(() => {
if (products.length !== 0) {
setIsLoading(true);

api
.getProducts()
.then((data) => {
setProducts(data);
setIsLoading(false);
})
.catch((error) => {
setIsLoading(false);
setHasError(true);
setLoadingError(error.message);
});
});
return;
}
}, []);

this.setState({
cartItems: prevItems.cartItems,
products: prevItems.products,
});
}

componentDidUpdate() {
const { cartItems, products } = this.state;

localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify({ cartItems, products }),
);
}

handleAddToCart(productId) {
const { cartItems, products } = this.state;

function handleAddToCart(productId) {
const prevCartItem = cartItems.find((item) => item.id === productId);
const foundProduct = products.find((product) => product.id === productId);

Expand All @@ -114,19 +88,15 @@ class App extends Component {
};
});

this.setState({ cartItems: updatedCartItems });
setCartItems(updatedCartItems);
return;
}

const updatedProduct = buildNewCartItem(foundProduct);
this.setState((prevState) => ({
cartItems: [...prevState.cartItems, updatedProduct],
}));
setCartItems((prevState) => [...prevState, updatedProduct]);
}

handleChange(event, productId) {
const { cartItems } = this.state;

function handleChange(event, productId) {
const updatedCartItems = cartItems.map((item) => {
if (item.id === productId && item.quantity <= item.unitsInStock) {
return {
Expand All @@ -138,21 +108,16 @@ class App extends Component {
return item;
});

this.setState({ cartItems: updatedCartItems });
setCartItems(updatedCartItems);
}

handleRemove(productId) {
const { cartItems } = this.state;
function handleRemove(productId) {
const updatedCartItems = cartItems.filter((item) => item.id !== productId);

this.setState({
cartItems: updatedCartItems,
});
setCartItems(updatedCartItems);
}

handleDownVote(productId) {
const { products } = this.state;

function handleDownVote(productId) {
const updatedProducts = products.map((product) => {
if (
product.id === productId &&
Expand All @@ -174,12 +139,10 @@ class App extends Component {
return product;
});

this.setState({ products: updatedProducts });
setProducts(updatedProducts);
}

handleUpVote(productId) {
const { products } = this.state;

function handleUpVote(productId) {
const updatedProducts = products.map((product) => {
if (
product.id === productId &&
Expand All @@ -200,12 +163,10 @@ class App extends Component {
return product;
});

this.setState({ products: updatedProducts });
setProducts(updatedProducts);
}

handleSetFavorite(productId) {
const { products } = this.state;

function handleSetFavorite(productId) {
const updatedProducts = products.map((product) => {
if (product.id === productId) {
return {
Expand All @@ -217,58 +178,53 @@ class App extends Component {
return product;
});

this.setState({ products: updatedProducts });
setProducts(updatedProducts);
}

saveNewProduct(newProduct) {
this.setState((prevState) => ({
products: [newProduct, ...prevState.products],
newProductFormOpen: !prevState.newProductFormOpen,
}));
function saveNewProduct(newProduct) {
setProducts((prevState) => [newProduct, ...prevState]);
}

render() {
const {
cartItems,
products,
isLoading,
hasError,
loadingError,
} = this.state;

return (
return (
// StateProvider is a wrapper for the context. They allow me share states between components without using callbacks which are more complicated and problematic.
<StateProvider>
<BrowserRouter>
<Route
path="/"
exact
render={(routeProps) => (
<Switch>
<Route path="/checkout/FirstStep" exact>
<FirstStep />
</Route>
<Route path="/checkout/SecondStep" exact>
<SecondStep />
</Route>
<Route path="/checkout/ThirdStep" exact>
<ThirdStep />
</Route>
<Route path="/checkout/LastStep" exact>
<LastStep />
</Route>
<Route path="/new-product">
<NewProduct saveNewProduct={saveNewProduct} />
</Route>
<Route path="/" exact>
<Home
{...routeProps}
fullWidth
cartItems={cartItems}
products={products}
isLoading={isLoading}
hasError={hasError}
loadingError={loadingError}
handleDownVote={this.handleDownVote}
handleUpVote={this.handleUpVote}
handleSetFavorite={this.handleSetFavorite}
handleAddToCart={this.handleAddToCart}
handleRemove={this.handleRemove}
handleChange={this.handleChange}
handleDownVote={handleDownVote}
handleUpVote={handleUpVote}
handleSetFavorite={handleSetFavorite}
handleAddToCart={handleAddToCart}
handleRemove={handleRemove}
handleChange={handleChange}
/>
)}
/>
<Route
path="/new-product"
exact
render={(routeProps) => (
<NewProduct {...routeProps} saveNewProduct={this.saveNewProduct} />
)}
/>
</Route>
</Switch>
</BrowserRouter>
);
}
</StateProvider>
);
}

export default App;
24 changes: 21 additions & 3 deletions src/components/Cart/Cart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from "react";
import React, { useContext } from "react";
import { Link } from "react-router-dom";
import { ContextState } from "../../context/contextState";
import { ACTIONS } from "../../context/reducerState";

import ShoppingCartItem from "../ShoppingCartItem";
import Button from "../Button";

function getCartTotal(cart) {
return cart.reduce((accum, item) => {
Expand All @@ -10,6 +12,9 @@ function getCartTotal(cart) {
}

function Cart({ cartItems, handleRemove, handleChange, ...props }) {
const value = useContext(ContextState);
const { dispatch } = value;

return (
<aside {...props}>
<div className="row flex-column">
Expand Down Expand Up @@ -49,7 +54,20 @@ function Cart({ cartItems, handleRemove, handleChange, ...props }) {
<hr />
</div>
<div className="col">
<Button>Checkout</Button>
{cartItems.length > 0 && (
<Link
className="btn btn-primary"
to="/checkout/FirstStep"
onClick={() =>
dispatch({
type: ACTIONS.ADD_PRODUCTS,
payload: cartItems,
})
}
>
Checkout
</Link>
)}
</div>
</div>
</div>
Expand Down
Loading