-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add dynamic shopping cart which loads info from LocalStorage
- Loading branch information
1 parent
82dbb23
commit 7d856e8
Showing
6 changed files
with
129 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 16 additions & 41 deletions
57
src/app/[locale]/(default)/storage/shopping-cart/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,26 @@ | ||
import { ShoppingCartTable } from '@/components/storage/ShoppingCartTable'; | ||
import ShoppingCartTableSkeleton from '@/components/storage/ShoppingCartTableSkeleton'; | ||
import { useTranslations } from 'next-intl'; | ||
|
||
import { | ||
Table, | ||
TableBody, | ||
TableCaption, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from '@/components/ui/Table'; | ||
import { Suspense } from 'react'; | ||
|
||
export default function StorageShoppingCartPage() { | ||
const t = useTranslations('storage.shoppingCart'); | ||
|
||
const tableMessages = { | ||
tableDescription: t('tableDescription'), | ||
productId: t('productId'), | ||
productName: t('productName'), | ||
location: t('location'), | ||
unitsAvailable: t('unitsAvailable'), | ||
cartEmpty: t('cartEmpty'), | ||
}; | ||
|
||
return ( | ||
<> | ||
<h1 className='my-4 md:text-center'>{t('title')}</h1> | ||
<Table className='my-4'> | ||
<TableCaption>{t('tableDescription')}</TableCaption> | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead className='w-[150px]'>{t('productId')}</TableHead> | ||
<TableHead>{t('productName')}</TableHead> | ||
<TableHead>{t('location')}</TableHead> | ||
<TableHead className='text-right'>{t('unitsAvailable')}</TableHead> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
<TableRow> | ||
<TableCell className='font-medium'>01</TableCell> | ||
<TableCell>Laptop</TableCell> | ||
<TableCell>Storage Room A</TableCell> | ||
<TableCell className='text-right'>15</TableCell> | ||
</TableRow> | ||
<TableRow> | ||
<TableCell className='font-medium'>01</TableCell> | ||
<TableCell>Laptop</TableCell> | ||
<TableCell>Storage Room A</TableCell> | ||
<TableCell className='text-right'>15</TableCell> | ||
</TableRow> | ||
<TableRow> | ||
<TableCell className='font-medium'>01</TableCell> | ||
<TableCell>Laptop</TableCell> | ||
<TableCell>Storage Room A</TableCell> | ||
<TableCell className='text-right'>15</TableCell> | ||
</TableRow> | ||
</TableBody> | ||
</Table> | ||
<Suspense fallback={<ShoppingCartTableSkeleton />}> | ||
<ShoppingCartTable messages={tableMessages} /> | ||
</Suspense> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use client'; | ||
|
||
import { | ||
Table, | ||
TableBody, | ||
TableCaption, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from '@/components/ui/Table'; | ||
import { useLocalStorage } from 'usehooks-ts'; | ||
|
||
// TODO: Must be replaced by the type provided from database ORM. | ||
import type { StorageItem } from '@/components/storage/AddToCartButton'; | ||
|
||
type ShoppingCartTableProps = { | ||
messages: { | ||
tableDescription: string; | ||
productId: string; | ||
productName: string; | ||
location: string; | ||
unitsAvailable: string; | ||
cartEmpty: string; | ||
}; | ||
}; | ||
|
||
async function ShoppingCartTable({ messages }: ShoppingCartTableProps) { | ||
const [cart] = useLocalStorage<StorageItem[]>('shopping-cart', []); | ||
|
||
if (cart.length <= 0) { | ||
return <h3 className='text-center'>{messages.cartEmpty}</h3>; | ||
} | ||
|
||
return ( | ||
<Table className='my-4'> | ||
<TableCaption>{messages.tableDescription}</TableCaption> | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead className='w-[150px]'>{messages.productId}</TableHead> | ||
<TableHead>{messages.productName}</TableHead> | ||
<TableHead>{messages.location}</TableHead> | ||
<TableHead className='text-right'> | ||
{messages.unitsAvailable} | ||
</TableHead> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
{cart.map((item) => ( | ||
<TableRow key={item.name}> | ||
<TableCell className='font-medium'>{item.id}</TableCell> | ||
<TableCell>{item.name}</TableCell> | ||
<TableCell>{item.location}</TableCell> | ||
<TableCell className='text-right'>{item.quantity}</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
); | ||
} | ||
|
||
export { ShoppingCartTable }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { SkeletonCard } from '@/components/storage/SkeletonCard'; | ||
import { Skeleton } from '@/components/ui/Skeleton'; | ||
|
||
export default function ShoppingCartTableSkeleton() { | ||
return ( | ||
<> | ||
<div className='mt-4 grid grid-cols-10 gap-2'> | ||
<Skeleton className='col-span-1 h-5' /> | ||
<Skeleton className='col-span-3 h-5' /> | ||
<Skeleton className='col-span-5 h-5' /> | ||
<Skeleton className='col-span-1 h-5' /> | ||
</div> | ||
{Array.from({ length: 4 }).map((_, index) => { | ||
return ( | ||
// biome-ignore lint/suspicious/noArrayIndexKey: Skeleton items don't have a unique id to use. | ||
<div key={index} className='my-4 grid grid-cols-10 gap-2'> | ||
<Skeleton className='col-span-1 h-10' /> | ||
<Skeleton className='col-span-3 h-10' /> | ||
<Skeleton className='col-span-5 h-10' /> | ||
<Skeleton className='col-span-1 h-10' /> | ||
</div> | ||
); | ||
})} | ||
<Skeleton className='mx-auto mt-2 h-5 w-[200px]' /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters