From 9fb999733592a3c35fbd79f4aea09fd268d087c5 Mon Sep 17 00:00:00 2001 From: Dave Brudner Date: Fri, 17 Jan 2020 12:39:04 -0600 Subject: [PATCH] Adds adjustments to useCheckoutPricing hook --- lib/use-checkout-pricing.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/use-checkout-pricing.js b/lib/use-checkout-pricing.js index ea3238a..4faddb0 100644 --- a/lib/use-checkout-pricing.js +++ b/lib/use-checkout-pricing.js @@ -12,6 +12,7 @@ import cloneDeep from 'lodash/cloneDeep'; * * @typedef {Object} useCheckoutPricingInterface * @property {Array} subscriptions + * @property {Array} adjustments */ /** @@ -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); @@ -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 }) => {