This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit b8332dd.
- Loading branch information
Showing
31 changed files
with
1,912 additions
and
0 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
12 changes: 12 additions & 0 deletions
12
assets/js/atomic/blocks/product-elements/add-to-cart/attributes.ts
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,12 @@ | ||
export const blockAttributes = { | ||
showFormElements: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
productId: { | ||
type: 'number', | ||
default: 0, | ||
}, | ||
}; | ||
|
||
export default blockAttributes; |
87 changes: 87 additions & 0 deletions
87
assets/js/atomic/blocks/product-elements/add-to-cart/block.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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { | ||
AddToCartFormContextProvider, | ||
useAddToCartFormContext, | ||
} from '@woocommerce/base-context'; | ||
import { useProductDataContext } from '@woocommerce/shared-context'; | ||
import { isEmpty } from '@woocommerce/types'; | ||
import { withProductDataContext } from '@woocommerce/shared-hocs'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { AddToCartButton } from './shared'; | ||
import { | ||
SimpleProductForm, | ||
VariableProductForm, | ||
ExternalProductForm, | ||
GroupedProductForm, | ||
} from './product-types'; | ||
|
||
interface Props { | ||
/** | ||
* CSS Class name for the component. | ||
*/ | ||
className?: string; | ||
/** | ||
* Whether or not to show form elements. | ||
*/ | ||
showFormElements?: boolean; | ||
} | ||
|
||
/** | ||
* Renders the add to cart form using useAddToCartFormContext. | ||
*/ | ||
const AddToCartForm = () => { | ||
const { showFormElements, productType } = useAddToCartFormContext(); | ||
|
||
if ( showFormElements ) { | ||
if ( productType === 'variable' ) { | ||
return <VariableProductForm />; | ||
} | ||
if ( productType === 'grouped' ) { | ||
return <GroupedProductForm />; | ||
} | ||
if ( productType === 'external' ) { | ||
return <ExternalProductForm />; | ||
} | ||
if ( productType === 'simple' || productType === 'variation' ) { | ||
return <SimpleProductForm />; | ||
} | ||
return null; | ||
} | ||
|
||
return <AddToCartButton />; | ||
}; | ||
|
||
/** | ||
* Product Add to Form Block Component. | ||
*/ | ||
const Block = ( { className, showFormElements }: Props ) => { | ||
const { product } = useProductDataContext(); | ||
const componentClass = classnames( | ||
className, | ||
'wc-block-components-product-add-to-cart', | ||
{ | ||
'wc-block-components-product-add-to-cart--placeholder': | ||
isEmpty( product ), | ||
} | ||
); | ||
|
||
return ( | ||
<AddToCartFormContextProvider | ||
product={ product } | ||
showFormElements={ showFormElements } | ||
> | ||
<div className={ componentClass }> | ||
<AddToCartForm /> | ||
</div> | ||
</AddToCartFormContextProvider> | ||
); | ||
}; | ||
|
||
export default withProductDataContext( Block ); |
15 changes: 15 additions & 0 deletions
15
assets/js/atomic/blocks/product-elements/add-to-cart/constants.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { cart } from '@woocommerce/icons'; | ||
import { Icon } from '@wordpress/icons'; | ||
|
||
export const BLOCK_TITLE = __( 'Add to Cart', 'woo-gutenberg-products-block' ); | ||
export const BLOCK_ICON = ( | ||
<Icon icon={ cart } className="wc-block-editor-components-block-icon" /> | ||
); | ||
export const BLOCK_DESCRIPTION = __( | ||
'Displays an add to cart button. Optionally displays other add to cart form elements.', | ||
'woo-gutenberg-products-block' | ||
); |
94 changes: 94 additions & 0 deletions
94
assets/js/atomic/blocks/product-elements/add-to-cart/edit.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import EditProductLink from '@woocommerce/editor-components/edit-product-link'; | ||
import { useProductDataContext } from '@woocommerce/shared-context'; | ||
import classnames from 'classnames'; | ||
import { | ||
Disabled, | ||
PanelBody, | ||
ToggleControl, | ||
Notice, | ||
} from '@wordpress/components'; | ||
import { InspectorControls } from '@wordpress/block-editor'; | ||
import { productSupportsAddToCartForm } from '@woocommerce/base-utils'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import Block from './block'; | ||
import withProductSelector from '../shared/with-product-selector'; | ||
import { BLOCK_TITLE, BLOCK_ICON } from './constants'; | ||
|
||
interface EditProps { | ||
attributes: { | ||
className: string; | ||
showFormElements: boolean; | ||
}; | ||
setAttributes: ( attributes: { showFormElements: boolean } ) => void; | ||
} | ||
|
||
const Edit = ( { attributes, setAttributes }: EditProps ) => { | ||
const { product } = useProductDataContext(); | ||
const { className, showFormElements } = attributes; | ||
|
||
return ( | ||
<div | ||
className={ classnames( | ||
className, | ||
'wc-block-components-product-add-to-cart' | ||
) } | ||
> | ||
<EditProductLink productId={ product.id } /> | ||
<InspectorControls> | ||
<PanelBody | ||
title={ __( 'Layout', 'woo-gutenberg-products-block' ) } | ||
> | ||
{ productSupportsAddToCartForm( product ) ? ( | ||
<ToggleControl | ||
label={ __( | ||
'Display form elements', | ||
'woo-gutenberg-products-block' | ||
) } | ||
help={ __( | ||
'Depending on product type, allow customers to select a quantity, variations etc.', | ||
'woo-gutenberg-products-block' | ||
) } | ||
checked={ showFormElements } | ||
onChange={ () => | ||
setAttributes( { | ||
showFormElements: ! showFormElements, | ||
} ) | ||
} | ||
/> | ||
) : ( | ||
<Notice | ||
className="wc-block-components-product-add-to-cart-notice" | ||
isDismissible={ false } | ||
status="info" | ||
> | ||
{ __( | ||
'This product does not support the block based add to cart form. A link to the product page will be shown instead.', | ||
'woo-gutenberg-products-block' | ||
) } | ||
</Notice> | ||
) } | ||
</PanelBody> | ||
</InspectorControls> | ||
<Disabled> | ||
<Block { ...attributes } /> | ||
</Disabled> | ||
</div> | ||
); | ||
}; | ||
|
||
export default withProductSelector( { | ||
icon: BLOCK_ICON, | ||
label: BLOCK_TITLE, | ||
description: __( | ||
'Choose a product to display its add to cart form.', | ||
'woo-gutenberg-products-block' | ||
), | ||
} )( Edit ); |
12 changes: 12 additions & 0 deletions
12
assets/js/atomic/blocks/product-elements/add-to-cart/frontend.ts
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,12 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { withFilteredAttributes } from '@woocommerce/shared-hocs'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Block from './block'; | ||
import attributes from './attributes'; | ||
|
||
export default withFilteredAttributes( attributes )( Block ); |
29 changes: 29 additions & 0 deletions
29
assets/js/atomic/blocks/product-elements/add-to-cart/index.ts
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,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerExperimentalBlockType } from '@woocommerce/block-settings'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import sharedConfig from '../shared/config'; | ||
import edit from './edit'; | ||
import attributes from './attributes'; | ||
import { | ||
BLOCK_TITLE as title, | ||
BLOCK_ICON as icon, | ||
BLOCK_DESCRIPTION as description, | ||
} from './constants'; | ||
|
||
const blockConfig = { | ||
title, | ||
description, | ||
icon: { src: icon }, | ||
edit, | ||
attributes, | ||
}; | ||
|
||
registerExperimentalBlockType( 'woocommerce/product-add-to-cart', { | ||
...sharedConfig, | ||
...blockConfig, | ||
} ); |
13 changes: 13 additions & 0 deletions
13
assets/js/atomic/blocks/product-elements/add-to-cart/product-types/external.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import AddToCartButton from '../shared/add-to-cart-button'; | ||
|
||
/** | ||
* External Product Add To Cart Form | ||
*/ | ||
const External = () => { | ||
return <AddToCartButton />; | ||
}; | ||
|
||
export default External; |
8 changes: 8 additions & 0 deletions
8
assets/js/atomic/blocks/product-elements/add-to-cart/product-types/grouped.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Grouped Product Add To Cart Form | ||
*/ | ||
const Grouped = () => ( | ||
<p>This is a placeholder for the grouped products form element.</p> | ||
); | ||
|
||
export default Grouped; |
4 changes: 4 additions & 0 deletions
4
assets/js/atomic/blocks/product-elements/add-to-cart/product-types/index.ts
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,4 @@ | ||
export { default as SimpleProductForm } from './simple'; | ||
export { default as VariableProductForm } from './variable/index'; | ||
export { default as ExternalProductForm } from './external'; | ||
export { default as GroupedProductForm } from './grouped'; |
57 changes: 57 additions & 0 deletions
57
assets/js/atomic/blocks/product-elements/add-to-cart/product-types/simple.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useAddToCartFormContext } from '@woocommerce/base-context'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { AddToCartButton, QuantityInput, ProductUnavailable } from '../shared'; | ||
|
||
/** | ||
* Simple Product Add To Cart Form | ||
*/ | ||
const Simple = () => { | ||
// @todo Add types for `useAddToCartFormContext` | ||
const { | ||
product, | ||
quantity, | ||
minQuantity, | ||
maxQuantity, | ||
multipleOf, | ||
dispatchActions, | ||
isDisabled, | ||
} = useAddToCartFormContext(); | ||
|
||
if ( product.id && ! product.is_purchasable ) { | ||
return <ProductUnavailable />; | ||
} | ||
|
||
if ( product.id && ! product.is_in_stock ) { | ||
return ( | ||
<ProductUnavailable | ||
reason={ __( | ||
'This product is currently out of stock and cannot be purchased.', | ||
'woo-gutenberg-products-block' | ||
) } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<QuantityInput | ||
value={ quantity } | ||
min={ minQuantity } | ||
max={ maxQuantity } | ||
step={ multipleOf } | ||
disabled={ isDisabled } | ||
onChange={ dispatchActions.setQuantity } | ||
/> | ||
<AddToCartButton /> | ||
</> | ||
); | ||
}; | ||
|
||
export default Simple; |
Oops, something went wrong.