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 / Donations / resources / hooks / useResendReceipt.tsx

useResendReceipt.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/Donations/resources/hooks/useResendReceipt.tsx

58 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useState } from 'react';
2 import { __ } from '@wordpress/i18n';
3 import { useDispatch } from '@wordpress/data';
4
5 /**
6 * @since 4.6.0
7 */
8 export default function useResendReceipt() {
9 const [loading, setLoading] = useState<boolean>(false);
10 const [message, setMessage] = useState<string | null>(__('Resend Receipt', 'give'));
11 const [hasResolved, setHasResolved] = useState<boolean>(false);
12
13 const urlParams = new URLSearchParams(window.location.search);
14 const donationId = urlParams.get('id');
15 const dispatch = useDispatch('givewp/admin-details-page-notifications');
16
17 const handleResendReceipt = async () => {
18 if (!donationId) {
19 setMessage(__('Donation ID not found.', 'give'));
20 setHasResolved(true);
21 return;
22 }
23
24 setLoading(true);
25 setMessage(__('Resending', 'give'));
26 setHasResolved(false);
27
28 try {
29 const response = await fetch('/wp-json/give-api/v2/admin/donations/resendEmailReceipt', {
30 method: 'POST',
31 headers: {
32 'Content-Type': 'application/json',
33 'X-WP-Nonce': (window as any).wpApiSettings?.nonce || '',
34 },
35 body: JSON.stringify({ ids: donationId }),
36 });
37
38 if (!response.ok) {
39 throw new Error(await response.text());
40 }
41 } catch (error: any) {
42 dispatch.addSnackbarNotice({
43 id: 'resend-receipt-error',
44 content: __('Failed to resend receipt', 'give'),
45 });
46 } finally {
47 setLoading(false);
48 setHasResolved(true);
49 dispatch.addSnackbarNotice({
50 id: 'resend-receipt',
51 content: __('Receipt has been resent successfully', 'give'),
52 });
53 }
54 };
55
56 return { loading, message, hasResolved, handleResendReceipt };
57 }
58