Skip to content
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
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 114 additions & 4 deletions src/DonationForms/resources/app/hooks/useDonationSummary.ts
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',
];
Comment on lines +24 to +41
Copy link
Contributor

@JasonTheAdams JasonTheAdams Nov 1, 2024

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:

const nonTwoDecimalCurrencies = {
    'BIF': 0,
    'CLP': 0,
    'DJF': 0,
    'GNF': 0,
    'ISK': 0,
    'JPY': 0,
    'KMF': 0,
    'KRW': 0,
    'PYG': 0,
    'RWF': 0,
    'UGX': 0,
    'VND': 0,
    'VUV': 0,
    'XAF': 0,
    'XOF': 0,
    'XPF': 0,
    'BHD': 3,
    'IQD': 3,
    'JOD': 3,
    'KWD': 3,
    'LYD': 3,
    'OMR': 3,
    'TND': 3
};

Copy link
Contributor

@JasonTheAdams JasonTheAdams Nov 1, 2024

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:

decimals = nonTwoDecimalCurrencies[currency] ?? 2;
return Math.round(parseFloat(amount) * (10 ** decimals)); 


/**
* Takes in an amount value in dollar units and returns the calculated cents (minor) amount
*
* @unreleased
*/
const dollarsToCents = (amount: string, currency: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I suggest amountToMinorUnit — as "dollars" and "cents" are distinct terms for certain currencies. 🤓

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,
Expand All @@ -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,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,29 @@ import {__} from '@wordpress/i18n';
import {isSubscriptionPeriod, SubscriptionPeriod} from '../groups/DonationAmount/subscriptionPeriod';
import {createInterpolateElement} from '@wordpress/element';

/**
* @since 3.0.0
*/
const getDonationTotal = (totals: any, amount: any) =>
Number(
Object.values({
...totals,
amount: Number(amount),
}).reduce((total: number, amount: number) => {
return total + amount;
}, 0)
);

/**
* @since 3.0.0
*/
export default function DonationSummary() {
const DonationSummaryItemsTemplate = window.givewp.form.templates.layouts.donationSummaryItems;
const {useWatch, useCurrencyFormatter, useDonationSummary} = window.givewp.form.hooks;
const {items, totals} = useDonationSummary();
const currency = useWatch({name: 'currency'});
const { useCurrencyFormatter, useDonationSummary } = window.givewp.form.hooks;
const { items } = useDonationSummary();
const {state: {
currency,
donationAmount,
donationAmountTotal,
subscriptionPeriod: period,
subscriptionFrequency: frequency,
}} = window.givewp.form.hooks.useDonationSummary();
const formatter = useCurrencyFormatter(currency);

const amount = useWatch({name: 'amount'});
const period = useWatch({name: 'subscriptionPeriod'});
const frequency = useWatch({name: 'subscriptionFrequency'});

const givingFrequency = useMemo(() => {
if (isSubscriptionPeriod(period)) {
const subscriptionPeriod = new SubscriptionPeriod(period);

if (frequency > 1) {
return createInterpolateElement(__('Every <period />', 'give'), {
period: <span>{`${frequency} ${subscriptionPeriod.label().plural()}`}</span>,
period: <span>{`${frequency} ${subscriptionPeriod.label().plural()}`}</span>
});
}

Expand All @@ -49,18 +38,18 @@ export default function DonationSummary() {
const amountItem = {
id: 'amount',
label: __('Payment Amount', 'give'),
value: formatter.format(Number(amount)),
value: formatter.format(donationAmount)
};

const frequencyItem = {
id: 'frequency',
label: __('Giving Frequency', 'give'),
value: givingFrequency,
value: givingFrequency
};

const donationSummaryItems = [amountItem, frequencyItem, ...Object.values(items)];

const donationTotal = formatter.format(getDonationTotal(totals, amount));
const donationTotal = formatter.format(donationAmountTotal);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const LineItem = ({id, label, value, description, className}: LineItem) => {
);
};

/**
* TODO: account for when the total donation amount is different than subscription amount
*/
export default function DonationSummaryItems({items, total}) {
return (
<ul className="givewp-elements-donationSummary__list">
Expand Down
Loading