Skip to content

Instantly share code, notes, and snippets.

@ynotdraw
Created August 5, 2019 13:04
Show Gist options
  • Save ynotdraw/95fe08e0585b43cfda014e2b50969242 to your computer and use it in GitHub Desktop.
Save ynotdraw/95fe08e0585b43cfda014e2b50969242 to your computer and use it in GitHub Desktop.
import React, { useState, useContext, useEffect } from "react";
import { getCoffeeData } from "../api/getData";
import Layout from "../components/Layout";
import Loading from "../components/Loading";
import ItemGroup from "../components/ItemGroup";
import Cart from "../components/Cart";
import { GlobalContext } from "../components/GlobalStateProvider";
// this should be a reducer
function useCoffeeData() {
const [data, setData] = useState();
const [loading, setLoading] = useState(true);
const [error, setError] = useState();
useEffect(() => {
const getData = async () => {
try {
const newData = await getCoffeeData();
setData(newData);
setLoading(false);
} catch (ex) {
setData(undefined);
setLoading(false);
setError(ex);
}
};
getData();
}, []);
return { data, isLoading: loading, error };
}
export default function() {
const { data, isLoading, error } = useCoffeeData();
const [showCart, setShowCart] = useState(false);
const { dispatch } = useContext(GlobalContext);
return (
<Layout onToggleCart={() => setShowCart(!showCart)} showCart={showCart}>
{isLoading && <Loading />}
{showCart && <Cart />}
{!showCart && data && (
<ItemGroup
data={data}
addToCart={item => dispatch({ type: "ADD_ITEM", payload: item })}
/>
)}
{error && JSON.stringify(error)}
</Layout>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment