Skip to content

Commit

Permalink
Adds adjustments to useCheckoutPricing hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrudner committed Jan 30, 2020
1 parent 24869db commit 9fb9997
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/use-checkout-pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import cloneDeep from 'lodash/cloneDeep';
*
* @typedef {Object} useCheckoutPricingInterface
* @property {Array} subscriptions
* @property {Array} adjustments
*/

/**
Expand Down Expand Up @@ -43,7 +44,12 @@ export default function useCheckoutPricing(initialInputs = {}, handleError = thr

useEffect(() => {
setLoading(true);
const { subscriptions = [] } = input;
const { subscriptions = [], adjustments = [] } = input;

if (adjustments.length) {
checkoutPricing = addAdjustments(adjustments, checkoutPricing);
};

addSubscriptions(subscriptions, checkoutPricing)
.then(() => {
setPricing(checkoutPricing);
Expand All @@ -52,6 +58,14 @@ export default function useCheckoutPricing(initialInputs = {}, handleError = thr
.finally(() => setLoading(false));
}, [input]);

function addAdjustments (adjustments, checkoutPricing) {
return adjustments
.reduce((checkoutPricing, adjustment) => {
return checkoutPricing.adjustment(adjustment);
}, checkoutPricing)
.catch(handleError);
};

function addSubscriptions(subscriptions, checkoutPricing) {
const { subscriptionPricings } = subscriptions.reduce(
({ checkoutPricing, subscriptionPricings }, { plan, tax, addons = [], quantity }) => {
Expand Down

0 comments on commit 9fb9997

Please sign in to comment.