-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add stories for skus components
- Loading branch information
Showing
17 changed files
with
400 additions
and
25 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
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,74 @@ | ||
import { Meta, Source, Canvas } from '@storybook/addon-docs'; | ||
import * as Stories from './Skus.stories.tsx' | ||
|
||
|
||
<Meta of={Stories} /> | ||
|
||
# Skus | ||
|
||
This guide will explain how to render a list of skus with prices, availability, and add to cart buttons. | ||
|
||
As first thing, it uses both `<OrderStorage>` and `<OrderContainer>` to manage the state of the cart (remember that for us, the cart is a draft order). | ||
In this way the `<AddToCartButton>` knows to which order it should add the sku. | ||
|
||
<Source language='jsx' dark code={` | ||
<OrderStorage persistKey='my-localStorage-key'> | ||
<OrderContainer> | ||
// ...rest of the app | ||
</OrderContainer> | ||
</OrderStorage> | ||
`} /> | ||
|
||
|
||
The presentational part of the skus is handled with `<SkusContainer>` and `<Skus>` components. | ||
<Source language='jsx' dark code={` | ||
<OrderStorage> | ||
<OrderContainer> | ||
<SkusContainer skus=[...skusToShow]> | ||
<Skus> | ||
<div> | ||
// template used to render each sku | ||
</div> | ||
</Skus> | ||
</SkusContainer> | ||
</OrderContainer> | ||
</OrderStorage> | ||
`} /> | ||
|
||
|
||
Inside the `<Skus>` component we load all other components that are needed to render the skus, | ||
including the `<AvailabilityContainer>`, the `<PricesContainer>` and the `<AddToCartButton>`. | ||
|
||
<span title='Notice' type='info'> | ||
Please note that we don't need to loop though our `skus` array. The content will be rendered for each sku passed to our `SkusContainer`. | ||
</span> | ||
|
||
<Source language='jsx' dark code={` | ||
<Skus> | ||
<SkuField attribute="image_url"> | ||
<SkuField attribute="name"> | ||
<PricesContainer> // <-- gets current sku_code from <SkusContainer> | ||
<Price/> | ||
</PricesContainer> | ||
<AvailabilityContainer> // <-- gets current sku_code from <SkusContainer> | ||
<AvailabilityTemplate/> | ||
</AvailabilityContainer> | ||
<AddToCardButton /> // <-- gets current sku_code from <SkusContainer> and will add new line_items to the <OrderContainer> | ||
</Skus> | ||
`} /> | ||
|
||
|
||
To complete the example, we added a final block to the page that rapresent the items added to the cart (using `<LineItemsContainer>`). | ||
In this way you will be able to see a full interaction between nested contexts (OrderContainer and SkuContainer). | ||
|
||
|
||
### Final example | ||
|
||
Here you can see the final example that applies the block explained above, ready to be used in your application. | ||
|
||
<Canvas of={Stories.Default} /> |
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,98 @@ | ||
import type { Meta, StoryFn } from '@storybook/react' | ||
import CommerceLayer from '../../_internals/CommerceLayer' | ||
import { Skus } from '#components/skus/Skus' | ||
import { SkusContainer } from '#components/skus/SkusContainer' | ||
import { SkuField } from '#components/skus/SkuField' | ||
import { AvailabilityContainer } from '#components/skus/AvailabilityContainer' | ||
import AvailabilityTemplate from '#components/skus/AvailabilityTemplate' | ||
import Price from '#components/prices/Price' | ||
import PricesContainer from '#components/prices/PricesContainer' | ||
import OrderContainer from '#components/orders/OrderContainer' | ||
import AddToCartButton from '#components/orders/AddToCartButton' | ||
import OrderStorage from '#components/orders/OrderStorage' | ||
import LineItemsContainer from '#components/line_items/LineItemsContainer' | ||
import Errors from '#components/errors/Errors' | ||
import LineItemName from '#components/line_items/LineItemName' | ||
import LineItem from '#components/line_items/LineItem' | ||
import LineItemQuantity from '#components/line_items/LineItemQuantity' | ||
import LineItemRemoveLink from '#components/line_items/LineItemRemoveLink' | ||
import LineItemsEmpty from '#components/line_items/LineItemsEmpty' | ||
|
||
const setup: Meta = { | ||
title: 'Examples/Skus' | ||
} | ||
|
||
export default setup | ||
|
||
export const Default: StoryFn = (args) => { | ||
return ( | ||
<CommerceLayer | ||
accessToken='my-access-token' | ||
endpoint='https://demo-store.commercelayer.io' | ||
> | ||
<OrderStorage persistKey='cl-examples-skus-orderId'> | ||
<OrderContainer> | ||
<SkusContainer | ||
skus={['CROPTOPWFFFFFF000000XSXX', 'POLOMXXX000000FFFFFFLXXX']} | ||
> | ||
<Skus> | ||
<div className='flex gap-4 mb-8 pb-8 border-b items-start'> | ||
<SkuField attribute='image_url' tagElement='img' width={100} /> | ||
<div> | ||
<SkuField | ||
attribute='name' | ||
tagElement='h1' | ||
className='block font-bold' | ||
/> | ||
<SkuField | ||
attribute='description' | ||
tagElement='p' | ||
className='mb-2' | ||
/> | ||
|
||
<PricesContainer> | ||
<Price compareClassName='line-through ml-2' /> | ||
</PricesContainer> | ||
|
||
<div className='my-2'> | ||
<AddToCartButton className='px-3 py-2 bg-black text-white rounded disabled:opacity-50' /> | ||
</div> | ||
|
||
<AvailabilityContainer> | ||
<AvailabilityTemplate | ||
timeFormat='days' | ||
className='text-sm' | ||
showShippingMethodName | ||
/> | ||
</AvailabilityContainer> | ||
</div> | ||
</div> | ||
</Skus> | ||
</SkusContainer> | ||
|
||
{/* cart */} | ||
<div | ||
id='cart-recap' | ||
className='p-8 border rounded bg-gray-100 max-w-md' | ||
> | ||
<LineItemsContainer> | ||
<LineItemsEmpty text='Cart is empty' /> | ||
<LineItem type='skus'> | ||
<div className='flex gap-4 items-center mb-2'> | ||
<div className='flex' /> | ||
<LineItemName /> | ||
<LineItemQuantity> | ||
{({ quantity }) => <span>(×{quantity})</span>} | ||
</LineItemQuantity> | ||
|
||
<LineItemRemoveLink className='ml-auto px-2 text-sm rounded bg-red-500 text-white' /> | ||
</div> | ||
</LineItem> | ||
<Errors resource='orders' /> | ||
</LineItemsContainer> | ||
</div> | ||
</OrderContainer> | ||
</OrderStorage> | ||
</CommerceLayer> | ||
) | ||
} |
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
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,28 @@ | ||
import type { Meta, StoryFn } from '@storybook/react' | ||
import CommerceLayer from '../_internals/CommerceLayer' | ||
import { Skus } from '#components/skus/Skus' | ||
import { SkusContainer } from '#components/skus/SkusContainer' | ||
import { Code } from 'stories/_internals/Code' | ||
|
||
const setup: Meta<typeof Skus> = { | ||
title: 'Components/Skus/Skus', | ||
component: Skus | ||
} | ||
|
||
export default setup | ||
|
||
export const Default: StoryFn<typeof Skus> = () => { | ||
return ( | ||
<CommerceLayer accessToken='my-access-token'> | ||
<SkusContainer | ||
skus={['POLOMXXX000000FFFFFFLXXX', 'CROPTOPWFFFFFF000000XSXX']} | ||
> | ||
<Skus> | ||
<div> | ||
I am <Code>Skus</Code> child | ||
</div> | ||
</Skus> | ||
</SkusContainer> | ||
</CommerceLayer> | ||
) | ||
} |
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,40 @@ | ||
import type { Meta, StoryFn } from '@storybook/react' | ||
import CommerceLayer from '../_internals/CommerceLayer' | ||
import { SkusContainer } from '#components/skus/SkusContainer' | ||
import { Code } from '../_internals/Code' | ||
|
||
const setup: Meta<typeof SkusContainer> = { | ||
title: 'Components/Skus/SkusContainer', | ||
component: SkusContainer | ||
} | ||
|
||
export default setup | ||
|
||
const Template: StoryFn<typeof SkusContainer> = (args) => { | ||
return ( | ||
<CommerceLayer | ||
accessToken='my-access-token' | ||
endpoint='https://demo-store.commercelayer.io' | ||
> | ||
<SkusContainer {...args}> | ||
<div> | ||
I am the skus container, responsible to fetch alls <Code>skus</Code>{' '} | ||
details and make them available to the children components. | ||
</div> | ||
</SkusContainer> | ||
</CommerceLayer> | ||
) | ||
} | ||
|
||
export const Default = Template.bind({}) | ||
Default.args = { | ||
skus: ['POLOMXXX000000FFFFFFLXXX', 'CROPTOPWFFFFFF000000XSXX'], | ||
queryParams: { | ||
pageSize: 25, | ||
pageNumber: 1, | ||
fields: ['name', 'description', 'image_url', 'reference'], | ||
sort: { | ||
name: 'asc' | ||
} | ||
} | ||
} |
Oops, something went wrong.