woocommerce/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/cart/full-cart/index.js

233 lines
6.1 KiB
JavaScript
Raw Normal View History

Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
// @ts-nocheck
Cart block: line-items front end initial work (https://github.com/woocommerce/woocommerce-blocks/pull/1333) * render block on front end, add `Shopping cart` heading (baby steps) * fake data for editing full cart + show line count in header * add note about core/html using `is-active` class for toggle state * reinstate work-in-progress full cart component (lost in rebase) * reinstate full cart from master * component for full cart title & item count + margin tweaks: - add margin between main cart & sidebar - add margin after cart block * add cart items sample data + factor sample product image to module * use sample cart data for item count * basic table of cart line items (no styling) * prettification * show images for cart line items + initial table styling * cart quantity selector component (work in progress) * use state for cart product quantity, allow incr/decr from UI (WIP) * replace WIP custom quantity control with number edit (temporary) * correctly format cart line item total price * align cart item columns with headings + indent image on desktop * tweak css for cart line item padding on mobile so it's more explicit * show cart line item full price if discounted * add placeholder for cart remove item link * switch cart table to flex layout (was table)… This will allow us to move things around for mobile/responsive layout. * only show cart items table header on desktop * more cart items styling - row borders, appropriate padding + + move image width to variable + fix class name plurality for row (item not items) * use standard $gap instead of 1em for padding/margins * responsive (mobile) layout for cart line items: - shift line $ total to bottom right - stack quantity selector in product info column * remove extraneous cart table padding on mobile * comment about unused styles for quantity selector component * add follow up issue for todo * remove inappropriate href * render srcset & sizes for cart line item product image * remove todo comment * switch back to table markup for cart items (in progress): - table is more semantic, associates headers with columns * cart line items column widths - product column is larger (60%) * reinstate table row borders * bottom-align line item price on mobile * cart contents heading should be H2 + prettify * remove unused QuantitySelector code/styles, rename main class in line with BEM * defaults for QuantitySelector props * variable/property name tidies - match conventions/API * fix bug: line total price is only bottom-align on small screen * move QuantitySelector to root of components, intended to be generally useful * use lineItem directly for cart, specify shape in PropTypes * rename cart components to align with "line item" rather than "product" * rejig class names to better align with new component names & BEM style * show cart item image correct size: - use single column for product image and info, with flex container - specify image width (rem instead of px) * fix safari issue - cart product images displaying vertically stretched * shift product name left margin from image, to account for no-image case * experiment: bump bundlewatch size limit for cart temporarily: - our fake data inline image is heavy - when we switch to real API we will no longer need it * fix issue introduced when moving margin from image to details div: - product details needs margin on left (not right) * fix react props issues: - explicitly destructure image props for srcSet (vs srcset) - use API key field for line item key instead of id, fix duplicate test id - CartLineItemsTable takes an array of lineItems (incorrect PropTypes) * remove redundant divs + use conventional `null` (when no full price) * override editor styles to ensure cart product image is correct size * move cart items editor style override to editor.css * add an explicit readable heading for cart heading to match visual layout
2020-01-09 22:50:14 +00:00
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { __ } from '@wordpress/i18n';
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
import {
SubtotalsItem,
TotalsFeesItem,
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
TotalsCouponCodeInput,
TotalsDiscountItem,
TotalsFooterItem,
TotalsShippingItem,
TotalsTaxesItem,
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
} from '@woocommerce/base-components/totals';
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
import ShippingRatesControl from '@woocommerce/base-components/shipping-rates-control';
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
import {
COUPONS_ENABLED,
SHIPPING_ENABLED,
DISPLAY_CART_PRICES_INCLUDING_TAX,
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
} from '@woocommerce/block-settings';
import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils';
import { Card, CardBody } from 'wordpress-components';
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
import { decodeEntities } from '@wordpress/html-entities';
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
import { useStoreCartCoupons, useShippingRates } from '@woocommerce/base-hooks';
import classnames from 'classnames';
Cart block: line-items front end initial work (https://github.com/woocommerce/woocommerce-blocks/pull/1333) * render block on front end, add `Shopping cart` heading (baby steps) * fake data for editing full cart + show line count in header * add note about core/html using `is-active` class for toggle state * reinstate work-in-progress full cart component (lost in rebase) * reinstate full cart from master * component for full cart title & item count + margin tweaks: - add margin between main cart & sidebar - add margin after cart block * add cart items sample data + factor sample product image to module * use sample cart data for item count * basic table of cart line items (no styling) * prettification * show images for cart line items + initial table styling * cart quantity selector component (work in progress) * use state for cart product quantity, allow incr/decr from UI (WIP) * replace WIP custom quantity control with number edit (temporary) * correctly format cart line item total price * align cart item columns with headings + indent image on desktop * tweak css for cart line item padding on mobile so it's more explicit * show cart line item full price if discounted * add placeholder for cart remove item link * switch cart table to flex layout (was table)… This will allow us to move things around for mobile/responsive layout. * only show cart items table header on desktop * more cart items styling - row borders, appropriate padding + + move image width to variable + fix class name plurality for row (item not items) * use standard $gap instead of 1em for padding/margins * responsive (mobile) layout for cart line items: - shift line $ total to bottom right - stack quantity selector in product info column * remove extraneous cart table padding on mobile * comment about unused styles for quantity selector component * add follow up issue for todo * remove inappropriate href * render srcset & sizes for cart line item product image * remove todo comment * switch back to table markup for cart items (in progress): - table is more semantic, associates headers with columns * cart line items column widths - product column is larger (60%) * reinstate table row borders * bottom-align line item price on mobile * cart contents heading should be H2 + prettify * remove unused QuantitySelector code/styles, rename main class in line with BEM * defaults for QuantitySelector props * variable/property name tidies - match conventions/API * fix bug: line total price is only bottom-align on small screen * move QuantitySelector to root of components, intended to be generally useful * use lineItem directly for cart, specify shape in PropTypes * rename cart components to align with "line item" rather than "product" * rejig class names to better align with new component names & BEM style * show cart item image correct size: - use single column for product image and info, with flex container - specify image width (rem instead of px) * fix safari issue - cart product images displaying vertically stretched * shift product name left margin from image, to account for no-image case * experiment: bump bundlewatch size limit for cart temporarily: - our fake data inline image is heavy - when we switch to real API we will no longer need it * fix issue introduced when moving margin from image to details div: - product details needs margin on left (not right) * fix react props issues: - explicitly destructure image props for srcSet (vs srcset) - use API key field for line item key instead of id, fix duplicate test id - CartLineItemsTable takes an array of lineItems (incorrect PropTypes) * remove redundant divs + use conventional `null` (when no full price) * override editor styles to ensure cart product image is correct size * move cart items editor style override to editor.css * add an explicit readable heading for cart heading to match visual layout
2020-01-09 22:50:14 +00:00
/**
* Internal dependencies
*/
import CheckoutButton from './checkout-button';
Cart block: line-items front end initial work (https://github.com/woocommerce/woocommerce-blocks/pull/1333) * render block on front end, add `Shopping cart` heading (baby steps) * fake data for editing full cart + show line count in header * add note about core/html using `is-active` class for toggle state * reinstate work-in-progress full cart component (lost in rebase) * reinstate full cart from master * component for full cart title & item count + margin tweaks: - add margin between main cart & sidebar - add margin after cart block * add cart items sample data + factor sample product image to module * use sample cart data for item count * basic table of cart line items (no styling) * prettification * show images for cart line items + initial table styling * cart quantity selector component (work in progress) * use state for cart product quantity, allow incr/decr from UI (WIP) * replace WIP custom quantity control with number edit (temporary) * correctly format cart line item total price * align cart item columns with headings + indent image on desktop * tweak css for cart line item padding on mobile so it's more explicit * show cart line item full price if discounted * add placeholder for cart remove item link * switch cart table to flex layout (was table)… This will allow us to move things around for mobile/responsive layout. * only show cart items table header on desktop * more cart items styling - row borders, appropriate padding + + move image width to variable + fix class name plurality for row (item not items) * use standard $gap instead of 1em for padding/margins * responsive (mobile) layout for cart line items: - shift line $ total to bottom right - stack quantity selector in product info column * remove extraneous cart table padding on mobile * comment about unused styles for quantity selector component * add follow up issue for todo * remove inappropriate href * render srcset & sizes for cart line item product image * remove todo comment * switch back to table markup for cart items (in progress): - table is more semantic, associates headers with columns * cart line items column widths - product column is larger (60%) * reinstate table row borders * bottom-align line item price on mobile * cart contents heading should be H2 + prettify * remove unused QuantitySelector code/styles, rename main class in line with BEM * defaults for QuantitySelector props * variable/property name tidies - match conventions/API * fix bug: line total price is only bottom-align on small screen * move QuantitySelector to root of components, intended to be generally useful * use lineItem directly for cart, specify shape in PropTypes * rename cart components to align with "line item" rather than "product" * rejig class names to better align with new component names & BEM style * show cart item image correct size: - use single column for product image and info, with flex container - specify image width (rem instead of px) * fix safari issue - cart product images displaying vertically stretched * shift product name left margin from image, to account for no-image case * experiment: bump bundlewatch size limit for cart temporarily: - our fake data inline image is heavy - when we switch to real API we will no longer need it * fix issue introduced when moving margin from image to details div: - product details needs margin on left (not right) * fix react props issues: - explicitly destructure image props for srcSet (vs srcset) - use API key field for line item key instead of id, fix duplicate test id - CartLineItemsTable takes an array of lineItems (incorrect PropTypes) * remove redundant divs + use conventional `null` (when no full price) * override editor styles to ensure cart product image is correct size * move cart items editor style override to editor.css * add an explicit readable heading for cart heading to match visual layout
2020-01-09 22:50:14 +00:00
import CartLineItemsTitle from './cart-line-items-title';
import CartLineItemsTable from './cart-line-items-table';
import './style.scss';
Cart block: line-items front end initial work (https://github.com/woocommerce/woocommerce-blocks/pull/1333) * render block on front end, add `Shopping cart` heading (baby steps) * fake data for editing full cart + show line count in header * add note about core/html using `is-active` class for toggle state * reinstate work-in-progress full cart component (lost in rebase) * reinstate full cart from master * component for full cart title & item count + margin tweaks: - add margin between main cart & sidebar - add margin after cart block * add cart items sample data + factor sample product image to module * use sample cart data for item count * basic table of cart line items (no styling) * prettification * show images for cart line items + initial table styling * cart quantity selector component (work in progress) * use state for cart product quantity, allow incr/decr from UI (WIP) * replace WIP custom quantity control with number edit (temporary) * correctly format cart line item total price * align cart item columns with headings + indent image on desktop * tweak css for cart line item padding on mobile so it's more explicit * show cart line item full price if discounted * add placeholder for cart remove item link * switch cart table to flex layout (was table)… This will allow us to move things around for mobile/responsive layout. * only show cart items table header on desktop * more cart items styling - row borders, appropriate padding + + move image width to variable + fix class name plurality for row (item not items) * use standard $gap instead of 1em for padding/margins * responsive (mobile) layout for cart line items: - shift line $ total to bottom right - stack quantity selector in product info column * remove extraneous cart table padding on mobile * comment about unused styles for quantity selector component * add follow up issue for todo * remove inappropriate href * render srcset & sizes for cart line item product image * remove todo comment * switch back to table markup for cart items (in progress): - table is more semantic, associates headers with columns * cart line items column widths - product column is larger (60%) * reinstate table row borders * bottom-align line item price on mobile * cart contents heading should be H2 + prettify * remove unused QuantitySelector code/styles, rename main class in line with BEM * defaults for QuantitySelector props * variable/property name tidies - match conventions/API * fix bug: line total price is only bottom-align on small screen * move QuantitySelector to root of components, intended to be generally useful * use lineItem directly for cart, specify shape in PropTypes * rename cart components to align with "line item" rather than "product" * rejig class names to better align with new component names & BEM style * show cart item image correct size: - use single column for product image and info, with flex container - specify image width (rem instead of px) * fix safari issue - cart product images displaying vertically stretched * shift product name left margin from image, to account for no-image case * experiment: bump bundlewatch size limit for cart temporarily: - our fake data inline image is heavy - when we switch to real API we will no longer need it * fix issue introduced when moving margin from image to details div: - product details needs margin on left (not right) * fix react props issues: - explicitly destructure image props for srcSet (vs srcset) - use API key field for line item key instead of id, fix duplicate test id - CartLineItemsTable takes an array of lineItems (incorrect PropTypes) * remove redundant divs + use conventional `null` (when no full price) * override editor styles to ensure cart product image is correct size * move cart items editor style override to editor.css * add an explicit readable heading for cart heading to match visual layout
2020-01-09 22:50:14 +00:00
import './editor.scss';
const renderShippingRatesControlOption = ( option ) => ( {
label: decodeEntities( option.name ),
value: option.rate_id,
description: (
<>
{ option.price && (
<FormattedMonetaryAmount
currency={ getCurrencyFromPriceResponse( option ) }
value={ option.price }
/>
) }
{ option.price && option.delivery_time ? ' — ' : null }
{ decodeEntities( option.delivery_time ) }
</>
),
} );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
const ShippingCalculatorOptions = ( {
shippingRates,
shippingRatesLoading,
shippingAddress,
} ) => {
return (
<fieldset className="wc-block-cart__shipping-options-fieldset">
<legend className="screen-reader-text">
{ __(
'Choose the shipping method.',
'woo-gutenberg-products-block'
) }
</legend>
<ShippingRatesControl
className="wc-block-cart__shipping-options"
address={
shippingAddress
? {
city: shippingAddress.city,
state: shippingAddress.state,
postcode: shippingAddress.postcode,
country: shippingAddress.country,
}
: null
}
collapsibleWhenMultiple={ true }
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
noResultsMessage={ __(
'No shipping options were found.',
'woo-gutenberg-products-block'
) }
renderOption={ renderShippingRatesControlOption }
shippingRates={ shippingRates }
shippingRatesLoading={ shippingRatesLoading }
/>
</fieldset>
);
};
/**
* Component that renders the Cart block when user has something in cart aka "full".
*/
const Cart = ( {
cartItems = [],
cartTotals = {},
cartCoupons = [],
isShippingCalculatorEnabled,
isShippingCostHidden,
shippingRates,
isLoading = false,
} ) => {
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
const { updateShippingAddress, shippingRatesLoading } = useShippingRates();
const shippingAddress = shippingRates[ 0 ]?.destination;
const {
applyCoupon,
removeCoupon,
isApplyingCoupon,
isRemovingCoupon,
} = useStoreCartCoupons();
const showShippingCosts = Boolean(
SHIPPING_ENABLED &&
isShippingCalculatorEnabled &&
( ! isShippingCostHidden || shippingAddress?.country )
);
const totalsCurrency = getCurrencyFromPriceResponse( cartTotals );
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
const cartClassName = classnames( 'wc-block-cart', {
'wc-block-cart--is-loading': isLoading,
} );
return (
<div className={ cartClassName }>
<div className="wc-block-cart__main">
<CartLineItemsTitle itemCount={ cartItems.length } />
<CartLineItemsTable
lineItems={ cartItems }
isLoading={ isLoading }
/>
</div>
<div className="wc-block-cart__sidebar">
<Card isElevated={ true }>
<CardBody>
<h2 className="wc-block-cart__totals-title">
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
{ __(
'Cart totals',
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
'woo-gutenberg-products-block'
) }
</h2>
<SubtotalsItem
currency={ totalsCurrency }
values={ cartTotals }
/>
<TotalsFeesItem
currency={ totalsCurrency }
values={ cartTotals }
/>
<TotalsDiscountItem
cartCoupons={ cartCoupons }
currency={ totalsCurrency }
isRemovingCoupon={ isRemovingCoupon }
removeCoupon={ removeCoupon }
values={ cartTotals }
/>
{ isShippingCalculatorEnabled && (
<TotalsShippingItem
currency={ totalsCurrency }
shippingAddress={ shippingAddress }
updateShippingAddress={ updateShippingAddress }
values={ cartTotals }
/>
) }
{ showShippingCosts && (
<fieldset className="wc-block-cart__shipping-options-fieldset">
<legend className="screen-reader-text">
{ __(
'Choose the shipping method.',
'woo-gutenberg-products-block'
) }
</legend>
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
<ShippingCalculatorOptions
shippingRates={ shippingRates }
shippingRatesLoading={
shippingRatesLoading
}
shippingAddress={ shippingAddress }
/>
</fieldset>
) }
{ ! DISPLAY_CART_PRICES_INCLUDING_TAX && (
<TotalsTaxesItem
currency={ totalsCurrency }
values={ cartTotals }
/>
) }
{ COUPONS_ENABLED && (
<TotalsCouponCodeInput
onSubmit={ applyCoupon }
isLoading={ isApplyingCoupon }
/>
) }
<TotalsFooterItem
currency={ totalsCurrency }
values={ cartTotals }
Add Cart totals to Cart block (https://github.com/woocommerce/woocommerce-blocks/pull/1411) * Add Cart totals to Cart block * Accessibility improvements * Load vendors styles separately * Use same shipping placeholders for cart and checkout * Refactor how we import @wordpress/components styles so only panel styles are imported * Remove style-loader from vendors styles build process * Add htmlFor attribute to TotalsCouponCodeInput * Update totalItems shape to match API * Fix wrong total items shape using numbers instead of strings * Rename wc-blocks classes to wc-block * Remove unnecessary parseInt() * Add radix to parseInt() * Rename totalRows to totalRowsConfig * Move placeholder content out of the component * Use Card component for cart's sidebar (https://github.com/woocommerce/woocommerce-blocks/pull/1423) * Use Card component for cart's sidebar * Split RadioControl component * No need to use Label in RadioControlOption * Remove no longer valid @todo comment * Use 'checked' prop instead of 'selected' in RadioControlOption * Rename wc-blocks classes to wc-block * Rename wc-blocks classes to wc-block (II) * Make sure radio control ids are unique using withComponentId * Load PanelBody and PanelRow from last version of @wordpress/components * Create vendors-frontend.js file * Load wordpress-component instead of @wordpress/components from <Button> component * Only load 'withRestApiHydration' HOC * Make vendors-frontend a dependency of cart-frontend script * Revert "Only load 'withRestApiHydration' HOC" This reverts commit 9f9b9759a98047b26e7d8f04189ffe78c1d5bb06. * Fix fieldset background
2020-01-10 14:37:27 +00:00
/>
<CheckoutButton />
</CardBody>
</Card>
</div>
</div>
);
};
Cart.propTypes = {
cartItems: PropTypes.array,
cartTotals: PropTypes.shape( {
total_items: PropTypes.string,
total_items_tax: PropTypes.string,
total_fees: PropTypes.string,
total_fees_tax: PropTypes.string,
total_discount: PropTypes.string,
total_discount_tax: PropTypes.string,
total_shipping: PropTypes.string,
total_shipping_tax: PropTypes.string,
total_tax: PropTypes.string,
total_price: PropTypes.string,
} ),
isShippingCalculatorEnabled: PropTypes.bool,
isShippingCostHidden: PropTypes.bool,
isLoading: PropTypes.bool,
/**
* List of shipping rates to display. If defined, shipping rates will not be fetched from the API (used for the block preview).
*/
shippingRates: PropTypes.array,
};
export default Cart;