This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic product list and page (#7)
- Loading branch information
1 parent
db32134
commit b1beace
Showing
7 changed files
with
188 additions
and
231 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,76 @@ | ||
import { useEffect, useState } from 'react' | ||
import { Product, ProductcatalogClient } from '../../api/productcatalog' | ||
import { useParams } from 'react-router-dom' | ||
import { formatMoney } from '../../utils/money.utils' | ||
|
||
export const ProductPage = () => { | ||
const { productId } = useParams() | ||
const [product, setProduct] = useState<Product | undefined>() | ||
|
||
useEffect(() => { | ||
const productsClient = new ProductcatalogClient('http://localhost:8892/ingress') | ||
productsClient.list({}).then((response) => { | ||
setProduct(response.products.find((product) => product.id.toLowerCase() === productId?.toLowerCase())) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<div className='bg-white'> | ||
<div className='pb-16 pt-6 sm:pb-24'> | ||
<div className='mx-auto mt-8 max-w-2xl px-4 sm:px-6 lg:max-w-7xl lg:px-8'> | ||
<div className='lg:grid lg:auto-rows-min lg:grid-cols-12 lg:gap-x-8'> | ||
<div className='lg:col-span-5 lg:col-start-8'> | ||
<div className='flex justify-between'> | ||
<h1 className='text-xl font-medium text-gray-900'>{product?.name}</h1> | ||
{product?.priceUSD && ( | ||
<p className='text-xl font-medium text-gray-900'>{formatMoney(product.priceUSD)}</p> | ||
)} | ||
</div> | ||
<div className='mt-4'> | ||
{product?.categories.map((category) => ( | ||
<span | ||
key={category} | ||
className='inline-flex items-center rounded-md bg-gray-100 px-2 py-1 text-xs font-medium text-gray-600 mr-2' | ||
> | ||
{category} | ||
</span> | ||
))} | ||
</div> | ||
|
||
<div className='mt-10'> | ||
<h2 className='text-sm font-medium text-gray-900'>Description</h2> | ||
|
||
<div | ||
className='prose prose-sm mt-4 text-gray-500' | ||
dangerouslySetInnerHTML={{ __html: product?.description ?? '' }} | ||
/> | ||
</div> | ||
|
||
<div className='mt-8 lg:col-span-5'> | ||
<form> | ||
<button | ||
type='submit' | ||
className='mt-8 flex w-full items-center justify-center rounded-md border border-transparent bg-indigo-600 px-8 py-3 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2' | ||
> | ||
Add to cart | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
<div className='mt-8 lg:col-span-7 lg:col-start-1 lg:row-span-3 lg:row-start-1 lg:mt-0'> | ||
<div className='grid grid-cols-1 lg:grid-cols-2 lg:grid-rows-3 lg:gap-8'> | ||
<img | ||
key={product?.id} | ||
src={product?.picture} | ||
alt={product?.name} | ||
className='lg:col-span-2 lg:row-span-2 rounded-lg' | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Money } from '../api/productcatalog' | ||
|
||
export const formatMoney = (money: Money): string => { | ||
const fractionalAmount = money.nanos / 1e9 | ||
const totalAmount = money.units + fractionalAmount | ||
return `$${totalAmount.toFixed(2)}` | ||
} |
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 |
---|---|---|
|
@@ -9,5 +9,5 @@ export default { | |
}, | ||
}, | ||
}, | ||
plugins: [], | ||
plugins: [require('@tailwindcss/aspect-ratio'),], | ||
} |