PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonationForms / resources / app / hooks / useDonationSummary.ts

useDonationSummary.ts in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/DonationForms/resources/app/hooks/useDonationSummary.ts

47 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 DonationSummaryLineItem,
3 useDonationSummaryContext,
4 useDonationSummaryDispatch
5 } from '@givewp/forms/app/store/donation-summary';
6 import {
7 addAmountToTotal,
8 addItem,
9 removeAmountFromTotal,
10 removeItem
11 } from '@givewp/forms/app/store/donation-summary/reducer';
12 import {useCallback} from '@wordpress/element';
13
14 /**
15 * The donation summary hook is used to interact with the donation summary context which wraps around our donation form.
16 * It provides methods to add and remove items from the summary, as well as to add and remove amounts from the total.
17 * It also provides the current items and totals from the context, making it easier to access form values specific to donations.
18 *
19 * @since 4.0.0 added getTotalSum
20 * @since 3.0.0
21 */
22 export default function useDonationSummary() {
23 const { items, totals } = useDonationSummaryContext();
24 const dispatch = useDonationSummaryDispatch();
25
26 return {
27 items,
28 totals,
29 addItem: useCallback((item: DonationSummaryLineItem) => dispatch(addItem(item)), [dispatch]),
30 removeItem: useCallback((itemId: string) => dispatch(removeItem(itemId)), [dispatch]),
31 addToTotal: useCallback(
32 (itemId: string, amount: number) => dispatch(addAmountToTotal(itemId, amount)),
33 [dispatch]
34 ),
35 removeFromTotal: useCallback((itemId: string) => dispatch(removeAmountFromTotal(itemId)), [dispatch]),
36 getTotalSum: useCallback((amount: number) =>
37 Number(
38 Object.values({
39 ...totals,
40 amount
41 }).reduce((total: number, amount: number) => {
42 return total + amount;
43 }, 0)
44 ), [totals])
45 };
46 }
47