| 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 |
|