Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist cart on refresh #58

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
10 changes: 7 additions & 3 deletions src/Components/Cart.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dialog, Transition } from "@headlessui/react";
import { XIcon } from "@heroicons/react/outline";
import { XIcon, ShoppingCartIcon } from "@heroicons/react/outline";
import React, { Fragment } from "react";

export default function Cart({ open, setOpen, cart, updateCart }) {
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function Cart({ open, setOpen, cart, updateCart }) {
>
<div className="pointer-events-auto w-screen max-w-md">
<div className="flex h-full flex-col overflow-y-scroll bg-white shadow-xl">
<div className="flex-1 overflow-y-auto py-6 px-4 sm:px-6">
<div className="flex flex-col flex-1 overflow-y-auto py-6 px-4 sm:px-6">
<div className="flex items-start justify-between">
<Dialog.Title className="text-lg font-medium text-gray-900"> Shopping cart </Dialog.Title>
<div className="ml-3 flex h-7 items-center">
Expand All @@ -51,7 +51,11 @@ export default function Cart({ open, setOpen, cart, updateCart }) {
</button>
</div>
</div>

{cart.length == 0 &&
<div className="flex flex-col justify-center items-center flex-1">
<ShoppingCartIcon className="h-12 w-12" />
<span className="pt-2">Your Cart is Empty.</span>
</div>}
<div className="mt-8">
<div className="flow-root">
<ul role="list" className="-my-6 divide-y divide-gray-200">
Expand Down
2 changes: 1 addition & 1 deletion src/Components/ProductTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function ProductTable({ cart, updateCart }) {
setProducts(body);
};
fetchProducts();
});
}, []);

return (
<div className="bg-white">
Expand Down
23 changes: 21 additions & 2 deletions src/Pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import Cart from "../Components/Cart";
import NavBar from "../Components/NavBar";
import ProductTable from "../Components/ProductTable";

function Home() {
const [open, setOpen] = useState(false);
const [cart, updateCart] = useState([]);
const [cart, setCartState] = useState(() => {
const localCart = JSON.parse(localStorage.getItem('cart'))
return localCart ? localCart : []
});

// Trying to have one location to update the local storage when the cart state changes, but only when items are added or removed.
// It doesn't make sense to overwrite the localstorage every time the page is loaded with the logic I have with state, above.
// But this implementation isn't much better, since I'm reading from disk twice on first load, and then again every time the cart is updated.
// The alternative that I'm aware of is to put it in the onClick actions for adding and removing from the cart, which probably makes more sense at this point in time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're on the right track - linking the actions that update the cart to localStorage is what I would recommend, but with a slight twist.

instead of spreading the code to multiple components (as you stated), you can simply wrap the updateCart function in a new function. This new function can update the cart state, and also pass the new cart state straight into localStorage. Here is an example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh I think I see what you did there. That makes sense, I didn't think of handling state within another function. Thank you!

// That way, we're only reading/writing to disk as actually necessary, but it does spread the code to do the same thing across multiple components.
function updateCart(newCart) {
localStorage.setItem('cart', JSON.stringify(newCart))
setCartState(newCart)
}

useEffect(() => {
if (cart != JSON.parse(localStorage.getItem('cart'))){
localStorage.setItem('cart', JSON.stringify(cart))
}
}, [cart])

return (
<main>
Expand Down