Skip to content

Commit

Permalink
fix!: Only save ids in LocalStorage instead of item objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroWave022 committed Sep 13, 2024
1 parent 7dacf19 commit 5cc0683
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/[locale]/(default)/storage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { createSearchParamsCache, parseAsInteger } from 'nuqs/parsers';

import { PaginationCarousel } from '@/components/layout/PaginationCarousel';
import { ItemCard } from '@/components/storage/ItemCard';
import { Button } from '@/components/ui/Button';
import { Combobox } from '@/components/ui/Combobox';
import { SearchBar } from '@/components/ui/SearchBar';
Expand All @@ -14,7 +15,6 @@ import {
SelectTrigger,
SelectValue,
} from '@/components/ui/Select';
import { ItemCard } from '@/components/storage/ItemCard';
import {
Tooltip,
TooltipContent,
Expand Down
8 changes: 4 additions & 4 deletions src/components/storage/AddToCartButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function AddToCartButton({
addToCart: string;
removeFromCart: string;
}) {
const [cart, setCart, removeCart] = useLocalStorage<StorageItem[]>(
const [cart, setCart, removeCart] = useLocalStorage<number[]>(
'shopping-cart',
[],
);
Expand All @@ -36,15 +36,15 @@ function AddToCartButton({

// On cart/item/page/etc change, check if we must update the isInCart state.
useEffect(() => {
setIsInCart(cart.some((i) => i.id === item.id));
setIsInCart(cart.some((i) => i === item.id));
}, [cart, item.id]);

const updateState = (addToCart: boolean) => {
let newCart = cart;
if (addToCart) {
newCart.push(item);
newCart.push(item.id);
} else {
newCart = newCart.filter((i) => i.id !== item.id);
newCart = newCart.filter((i) => i !== item.id);
}
setCart(newCart);
setIsInCart(addToCart);
Expand Down
9 changes: 7 additions & 2 deletions src/components/storage/ShoppingCartTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { useLocalStorage } from 'usehooks-ts';
// TODO: Must be replaced by the type provided from database ORM.
import type { StorageItem } from '@/components/storage/AddToCartButton';

// TODO: Must be replaced by requesting the data from a database.
import { items } from '@/mock-data/items';

type ShoppingCartTableProps = {
messages: {
tableDescription: string;
Expand All @@ -26,7 +29,9 @@ type ShoppingCartTableProps = {
};

async function ShoppingCartTable({ messages }: ShoppingCartTableProps) {
const [cart] = useLocalStorage<StorageItem[]>('shopping-cart', []);
const [cart] = useLocalStorage<number[]>('shopping-cart', []);

const itemsInCart = items.filter((item) => cart.includes(item.id));

if (cart.length <= 0) {
return <h3 className='text-center'>{messages.cartEmpty}</h3>;
Expand All @@ -46,7 +51,7 @@ async function ShoppingCartTable({ messages }: ShoppingCartTableProps) {
</TableRow>
</TableHeader>
<TableBody>
{cart.map((item) => (
{itemsInCart.map((item) => (
<TableRow key={item.name}>
<TableCell className='font-medium'>{item.id}</TableCell>
<TableCell>{item.name}</TableCell>
Expand Down

0 comments on commit 5cc0683

Please sign in to comment.