Skip to content

Commit

Permalink
feat: Add clear cart button
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroWave022 committed Sep 13, 2024
1 parent cd78df7 commit 27180b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"unitsAvailable": "Units available",
"tableDescription": "A list of your shopping cart items.",
"backToStorage": "Back to storage",
"cartEmpty": "Your shopping cart is empty."
"cartEmpty": "Your shopping cart is empty.",
"clearCart": "Empty shopping cart"
}
}
}
3 changes: 2 additions & 1 deletion messages/no.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"unitsAvailable": "Stk tilgjengelig",
"tableDescription": "En liste over handlekurven din.",
"backToStorage": "Tilbake til lageret",
"cartEmpty": "Handlekurven din er tom."
"cartEmpty": "Handlekurven din er tom.",
"clearCart": "Tøm handlekurven"
}
}
}
16 changes: 10 additions & 6 deletions src/app/[locale]/(default)/storage/shopping-cart/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ShoppingCartClearButton } from '@/components/storage/ShoppingCartClearButton';
import { ShoppingCartTable } from '@/components/storage/ShoppingCartTable';
import { Button } from '@/components/ui/Button';
import { Link } from '@/lib/navigation';
Expand Down Expand Up @@ -26,12 +27,15 @@ export default function StorageShoppingCartPage({
<>
<h1 className='my-4 md:text-center'>{t('title')}</h1>
<ShoppingCartTable messages={tableMessages} />
<Link href='/storage'>
<Button className='mx-auto flex gap-2'>
<ArrowLeft />
{t('backToStorage')}
</Button>
</Link>
<div className='flex justify-center gap-2'>
<Link href='/storage'>
<Button className='flex gap-2'>
<ArrowLeft />
{t('backToStorage')}
</Button>
</Link>
<ShoppingCartClearButton caption={t('clearCart')} />
</div>
</>
);
}
27 changes: 27 additions & 0 deletions src/components/storage/ShoppingCartClearButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client';
import { Button } from '@/components/ui/Button';
import { X } from 'lucide-react';
import { useLocalStorage } from 'usehooks-ts';

function ShoppingCartClearButton({ caption }: { caption: string }) {
const [cart, setCart] = useLocalStorage<number[]>('shopping-cart', []);

function clearCart() {
setCart([]);
}

if (cart.length <= 0) return;

return (
<Button
className='flex gap-2'
variant='destructive'
onClick={() => clearCart()}
>
<X />
{caption}
</Button>
);
}

export { ShoppingCartClearButton };

0 comments on commit 27180b1

Please sign in to comment.