Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
add checkout hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed Dec 15, 2019
1 parent b85b11a commit 316656d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions assets/js/base/hooks/checkout/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as useCheckoutEvents } from './use-checkout-events';
export { default as useCheckoutNotices } from './use-checkout-notices';
export { default as useCheckoutRedirectUrls } from './use-checkout-redirect-urls';
export { default as useCheckoutData } from './use-checkout-data';
29 changes: 29 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @todo this should be a value object. Provided via wc-settings?
const currencyObject = {
code: 'USD',
precision: 2,
symbol: '$',
symbolPosition: 'left',
decimalSeparator: '.',
priceFormat: '%1$s%2$s',
thousandSeparator: ',',
};

const useCheckoutData = () => {
// @todo this will likely be a global wp.data store state so that things
// like shipping selection, quantity changes, etc that affect totals etc
// will automatically update the payment data. For POC this is hardcoded
const checkoutData = {
// this likely should be a float.
total: 10.123,
currency: currencyObject,
// @todo, should this be a standard format of items in the checkout/cart
// provided to ALL payment methods? Line items includes taxes/shipping
// costs? Coupons?
lineItems: [],
};
const updateCheckoutData = () => {};
return [ checkoutData, updateCheckoutData ];
};

export default useCheckoutData;
41 changes: 41 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* External dependencies
*/
import useCheckoutContext from '@woocommerce/base-context/checkout-context';

export const useCheckoutEvents = () => {
const {
checkoutComplete,
setCheckoutComplete,
checkoutHasError,
setCheckoutHasError,
isCalculating,
setIsCalculating,
} = useCheckoutContext();
const setHasError = () => {
setCheckoutHasError( true );
};
const cancelCheckoutError = () => {
setCheckoutHasError( false );
};
const setComplete = () => {
cancelCheckoutError();
setCheckoutComplete( true );
};
const setCalculating = () => {
setIsCalculating( true );
};
const cancelCalculating = () => {
setIsCalculating( false );
};
return {
setCheckoutComplete: setComplete,
setCheckoutHasError: setHasError,
cancelCheckoutError,
setIsCalculating: setCalculating,
cancelCalculating,
isCalculating,
checkoutComplete,
checkoutHasError,
};
};
24 changes: 24 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-notices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* External dependencies
*/
import useCheckoutContext from '@woocommerce/base-context/checkout-context';

const useCheckoutNotices = () => {
const { notices, updateNotices } = useCheckoutContext();
const addNotice = ( notice ) => {
updateNotices( ( originalNotices ) => [ ...originalNotices, notice ] );
};
const removeNotice = ( notice ) => {
// @todo...figure out how notices are saved - might need unique ids?
// Do we have a special notice creator that takes care of that?
// Use wp notice api?
return notice;
};
return {
notices,
addNotice,
removeNotice,
};
};

export default useCheckoutNotices;
22 changes: 22 additions & 0 deletions assets/js/base/hooks/checkout/use-checkout-redirect-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import useCheckoutContext from '@woocommerce/base-context/checkout-context';

const useCheckoutRedirectUrls = () => {
const {
successRedirectUrl,
setSuccessRedirectUrl,
failureRedirectUrl,
setFailureRedirectUrl,
} = useCheckoutContext();

return {
successRedirectUrl,
setSuccessRedirectUrl,
failureRedirectUrl,
setFailureRedirectUrl,
};
};

export default useCheckoutRedirectUrls;
1 change: 1 addition & 0 deletions assets/js/base/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './use-collection';
export * from './use-collection-header';
export * from './use-collection-data';
export * from './use-previous';
export * from './checkout';

0 comments on commit 316656d

Please sign in to comment.