-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: update donation summary hook with helpful form state values #7600
Draft
jonwaldstein
wants to merge
5
commits into
develop
Choose a base branch
from
feature/donation-summary-hook-store
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−28
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f42422a
feature: add donation summary state values to hook for unified access
51bd5e6
feature: add initial subscription values
13837af
refactor: update variable names
04c7ca5
feature: add subscription installments
8fd681c
Merge branch 'develop' into feature/donation-summary-hook-store
jonwaldstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,118 @@ | ||
import { | ||
DonationSummaryLineItem, | ||
DonationSummaryLineItem, DonationTotals, | ||
useDonationSummaryContext, | ||
useDonationSummaryDispatch, | ||
useDonationSummaryDispatch | ||
} from '@givewp/forms/app/store/donation-summary'; | ||
import { | ||
addAmountToTotal, | ||
addItem, | ||
removeAmountFromTotal, | ||
removeItem, | ||
removeItem | ||
} from '@givewp/forms/app/store/donation-summary/reducer'; | ||
import {useCallback} from '@wordpress/element'; | ||
import type { | ||
subscriptionPeriod | ||
} from '@givewp/forms/registrars/templates/groups/DonationAmount/subscriptionPeriod'; | ||
|
||
/** | ||
* Zero decimal currencies are currencies that do not have a minor unit. | ||
* For example, the Japanese Yen (JPY) does not have a minor unit. | ||
* @unreleased | ||
* | ||
* @see https://stripe.com/docs/currencies#zero-decimal | ||
*/ | ||
const zeroDecimalCurrencies = [ | ||
'BIF', | ||
'CLP', | ||
'DJF', | ||
'GNF', | ||
'JPY', | ||
'KMF', | ||
'KRW', | ||
'MGA', | ||
'PYG', | ||
'RWF', | ||
'UGX', | ||
'VND', | ||
'VUV', | ||
'XAF', | ||
'XOF', | ||
'XPF', | ||
]; | ||
|
||
/** | ||
* Takes in an amount value in dollar units and returns the calculated cents (minor) amount | ||
* | ||
* @unreleased | ||
*/ | ||
const dollarsToCents = (amount: string, currency: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I suggest |
||
if (zeroDecimalCurrencies.includes(currency)) { | ||
return Math.round(parseFloat(amount)); | ||
} | ||
|
||
return Math.round(parseFloat(amount) * 100); | ||
}; | ||
|
||
/** | ||
* Donation total calculation | ||
* | ||
* @unreleased | ||
*/ | ||
const getDonationTotal = (totals: DonationTotals, amount: number) => | ||
Number( | ||
Object.values({ | ||
...totals, | ||
amount, | ||
}).reduce((total: number, amount: number) => { | ||
return total + amount; | ||
}, 0) | ||
); | ||
|
||
/** | ||
* Subscription total calculation | ||
* TODO: figure out which totals will be included in subscriptions | ||
* | ||
* @unreleased | ||
*/ | ||
const getSubscriptionTotal = (totals: DonationTotals, amount: number) => { | ||
let total = 0; | ||
|
||
// Subscriptions currently only support donation amount (TODO: and potentially feeRecovery values) | ||
const allowedKeys = ['feeRecovery']; | ||
|
||
for (const [key, value] of Object.entries(totals)) { | ||
if (allowedKeys.includes(key)) { | ||
total += value; | ||
} | ||
} | ||
|
||
return Number(total + amount); | ||
} | ||
/** | ||
* The donation summary hook is used to interact with the donation summary context which wraps around our donation form. | ||
* It provides methods to add and remove items from the summary, as well as to add and remove amounts from the total. | ||
* It also provides the current items and totals from the context, making it easier to access form values specific to donations. | ||
* | ||
* Although the initial intent for this hook was to be used in the DonationSummary component for visual reasons, it is also recommended to be used in others | ||
* areas like gateways to get the total donation amount and currency. | ||
* | ||
* @unreleased added state object | ||
* @since 3.0.0 | ||
*/ | ||
export default function useDonationSummary() { | ||
const {items, totals} = useDonationSummaryContext(); | ||
const { items, totals } = useDonationSummaryContext(); | ||
const dispatch = useDonationSummaryDispatch(); | ||
const { useWatch } = window.givewp.form.hooks; | ||
|
||
const amount = useWatch({ name: 'amount' }) as string; | ||
const currency = useWatch({ name: 'currency' }) as string; | ||
const subscriptionPeriod = useWatch({name: 'subscriptionPeriod'}) as subscriptionPeriod | undefined; | ||
const subscriptionFrequency = useWatch({name: 'subscriptionFrequency'}) as number | undefined; | ||
const subscriptionInstallments = useWatch({name: 'subscriptionInstallments'}); | ||
const donationType = useWatch({name: 'donationType'}) as "single" | "subscription" | undefined; | ||
|
||
const donationAmountTotal = getDonationTotal(totals, Number(amount)); | ||
const subscriptionAmount = getSubscriptionTotal(totals, Number(amount)) | ||
|
||
return { | ||
items, | ||
|
@@ -28,5 +124,19 @@ export default function useDonationSummary() { | |
[dispatch] | ||
), | ||
removeFromTotal: useCallback((itemId: string) => dispatch(removeAmountFromTotal(itemId)), [dispatch]), | ||
state: { | ||
currency, | ||
donationAmount: Number(amount), | ||
donationAmountMinor: dollarsToCents(amount, currency), | ||
donationAmountTotal, | ||
donationAmountTotalMinor: dollarsToCents(donationAmountTotal.toString(), currency), | ||
subscriptionAmount, | ||
subscriptionAmountMinor: dollarsToCents(subscriptionAmount.toString(), currency), | ||
donationIsOneTime: donationType === 'single', | ||
donationIsRecurring: donationType === 'subscription', | ||
subscriptionPeriod, | ||
subscriptionFrequency, | ||
subscriptionInstallments, | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonwaldstein I recommend using the following array to also identify 3-decimal currencies:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could then adjust your math: