/**
* External Dependencies
*/
import { useState } from 'react';
import cx from 'classnames';
/**
* WordPress Dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { createInterpolateElement } from '@wordpress/element';
/**
* Internal Dependencies
*/
import { RefundIcon, TrashIcon } from '@givewp/components/AdminDetailsPage/Icons';
import AdminDetailsPage from '@givewp/components/AdminDetailsPage';
import ConfirmationDialog from '@givewp/components/AdminDetailsPage/ConfirmationDialog';
import { getDonationOptionsWindowData, useDonationEntityRecord } from '@givewp/donations/utils';
import styles from './DonationDetailsPage.module.scss';
import tabDefinitions from './Tabs/definitions';
import useDonationRefund from '@givewp/donations/hooks/useDonationRefund';
import { useDonationAmounts } from '@givewp/donations/hooks';
import { useDispatch } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
const { donationStatuses } = getDonationOptionsWindowData();
/**
* @since 4.6.0
*/
const StatusBadge = ({ status, isTest }: { status: string, isTest: boolean }) => {
const statusMap = donationStatuses;
if (!statusMap[status]) {
return null;
}
return (
<>
{statusMap[status]}
{isTest && (
{__('Test Donation', 'give')}
)}
>
);
};
/**
* @since 4.6.0
*/
export default function DonationDetailsPage() {
const { adminUrl, donationsAdminUrl} = getDonationOptionsWindowData();
const [showConfirmationDialog, setShowConfirmationDialog] = useState(null);
const { record: donation } = useDonationEntityRecord();
const {formatter} = useDonationAmounts(donation);
const {canRefund, refund, isRefunding, isRefunded} = useDonationRefund(donation);
const { deleteEntityRecord } = useDispatch( coreDataStore );
const ContextMenuItems = ({ className }: { className: string }) => {
return (
<>
{canRefund && (
setShowConfirmationDialog('refund')}
>
{__('Refund', 'give')}
)}
setShowConfirmationDialog('delete')}
>
{__('Trash donation', 'give')}
>
);
};
/**
* @since 4.6.0
*/
const handleRefund = async () => {
try {
await refund();
} catch (error) {
setShowConfirmationDialog(null);
}
};
/**
* @since 4.6.0
*/
const handleDelete = async () => {
try {
await deleteEntityRecord('givewp', 'donation', donation?.id, {force: false})
window.location.href = donationsAdminUrl;
} catch (error) {
setShowConfirmationDialog(null);
}
};
return (
}
ContextMenuItems={ContextMenuItems}
>
setShowConfirmationDialog(null)}
handleConfirm={handleRefund}
isConfirming={isRefunding}
>
{
createInterpolateElement(
sprintf(
__('Refund %s to %s', 'give'),
formatter.format(donation?.amount?.value),
donation?.firstName
),
{
strong:
}
)
}
setShowConfirmationDialog(null)}
handleConfirm={handleDelete}
>
{__('Are you sure you want to move this donation to the trash? You can restore it later if needed.', 'give')}
);
}