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
232 changes: 80 additions & 152 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import React, { Component } from "react";
import React, { useState, useEffect } from "react";
import { BrowserRouter, 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";

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

if (!prevItems) {
return null;
}

try {
return JSON.parse(prevItems);
} catch (error) {
return null;
}
}

function buildNewCartItem(cartItem) {
if (cartItem.quantity >= cartItem.unitsInStock) {
return cartItem;
}

return {
id: cartItem.id,
title: cartItem.title,
Expand All @@ -38,121 +31,83 @@ function buildNewCartItem(cartItem) {
quantity: cartItem.quantity + 1,
};
}

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,
});
});
return;
function App() {
const [products, setProducts] = useState([]);
const [cartItems, setCartItems] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [hasError, setHasError] = useState(false);
const [loadingError, setLoadingError] = useState(null);

useEffect(() => {
async function loadData() {
const prevItems = loadLocalStorageData();
if (!prevItems) {
setIsLoading(true);
try {
const productResponse = await api.getProducts();
if (productResponse.data) {
setProducts(productResponse.data);
setIsLoading(false);
}
} catch (error) {
setHasError(true);
setIsLoading(false);
setLoadingError("Error");
}
} else {
setProducts(prevItems.products);
}
setCartItems(prevItems.cartItems);
}
loadData();
}, []);

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

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

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

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

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

if (prevCartItem) {
const updatedCartItems = cartItems.map((item) => {
if (item.id !== productId) {
return item;
}

if (item.quantity >= item.unitsInStock) {
return item;
}

return {
...item,
quantity: item.quantity + 1,
};
});

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

const updatedProduct = buildNewCartItem(foundProduct);
this.setState((prevState) => ({
cartItems: [...prevState.cartItems, updatedProduct],
}));
setCartItems([...cartItems, 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 {
...item,
quantity: Number(event.target.value),
};
}

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 @@ -170,16 +125,11 @@ 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 @@ -196,79 +146,57 @@ 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 {
...product,
isFavorite: !product.isFavorite,
};
}

return product;
});

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

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

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

return (
<BrowserRouter>
<Route
path="/"
exact
render={(routeProps) => (
<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}
/>
)}
/>
<Route
path="/new-product"
exact
render={(routeProps) => (
<NewProduct {...routeProps} saveNewProduct={this.saveNewProduct} />
)}
/>
</BrowserRouter>
);
function saveNewProduct(newProduct) {
setProducts((prevProducts) => [...prevProducts, newProduct]);
// newProductFormOpen: !prevState.newProductFormOpen
}
return (
<BrowserRouter>
<Route
path="/"
exact
render={(routeProps) => (
<Home
{...routeProps}
fullWidth
cartItems={cartItems}
products={products}
isLoading={isLoading}
hasError={hasError}
loadingError={loadingError}
handleDownVote={handleDownVote}
handleUpVote={handleUpVote}
handleSetFavorite={handleSetFavorite}
handleAddToCart={handleAddToCart}
handleRemove={handleRemove}
handleChange={handleChange}
/>
)}
/>
<Route
path="/new-product"
exact
render={(routeProps) => (
<NewProduct {...routeProps} saveNewProduct={saveNewProduct} />
)}
/>
</BrowserRouter>
);
}

export default App;
Loading