| 1 |
import {__} from '@wordpress/i18n'; |
| 2 |
import {useSWRConfig} from 'swr'; |
| 3 |
import RowAction from '@givewp/components/ListTable/RowAction'; |
| 4 |
import ListTableApi from '@givewp/components/ListTable/api'; |
| 5 |
import {useContext} from 'react'; |
| 6 |
import {ShowConfirmModalContext} from '@givewp/components/ListTable/ListTablePage'; |
| 7 |
import {Interweave} from 'interweave'; |
| 8 |
import './style.scss'; |
| 9 |
|
| 10 |
const donorsApi = new ListTableApi(window.GiveDonors); |
| 11 |
|
| 12 |
export function DonorsRowActions({item, setUpdateErrors, parameters}) { |
| 13 |
const showConfirmModal = useContext(ShowConfirmModalContext); |
| 14 |
const {mutate} = useSWRConfig(); |
| 15 |
|
| 16 |
const fetchAndUpdateErrors = async (parameters, endpoint, id, method) => { |
| 17 |
const deleteDonations = document.querySelector('#giveDonorsTableDeleteDonations') as HTMLInputElement; |
| 18 |
const response = await donorsApi.fetchWithArgs( |
| 19 |
endpoint, |
| 20 |
{ids: [id], deleteDonationsAndRecords: deleteDonations.checked}, |
| 21 |
method |
| 22 |
); |
| 23 |
setUpdateErrors(response); |
| 24 |
await mutate(parameters); |
| 25 |
return response; |
| 26 |
}; |
| 27 |
|
| 28 |
const deleteDonor = async (selected) => await fetchAndUpdateErrors(parameters, '/delete', item.id, 'DELETE'); |
| 29 |
|
| 30 |
const confirmDeleteDonor = (selected) => ( |
| 31 |
<div> |
| 32 |
<p>{__('Really delete the follow donor?', 'give')}</p> |
| 33 |
<Interweave attributes={{className: 'donorBulkModalContent'}} content={item?.donorInformation} /> |
| 34 |
<br></br> |
| 35 |
<input id="giveDonorsTableDeleteDonations" type="checkbox" defaultChecked={true} /> |
| 36 |
<label htmlFor="giveDonorsTableDeleteDonations"> |
| 37 |
{__('Delete all associated donations and records', 'give')} |
| 38 |
</label> |
| 39 |
</div> |
| 40 |
); |
| 41 |
|
| 42 |
const confirmModal = (event) => { |
| 43 |
showConfirmModal(__('Delete', 'give'), confirmDeleteDonor, deleteDonor, 'danger'); |
| 44 |
}; |
| 45 |
|
| 46 |
return ( |
| 47 |
<> |
| 48 |
<RowAction |
| 49 |
href={ |
| 50 |
window.GiveDonors.adminUrl + |
| 51 |
`edit.php?post_type=give_forms&page=give-donors&view=overview&id=${item.id}` |
| 52 |
} |
| 53 |
displayText={__('Edit', 'give')} |
| 54 |
/> |
| 55 |
<RowAction |
| 56 |
onClick={confirmModal} |
| 57 |
actionId={item.id} |
| 58 |
displayText={__('Delete', 'give')} |
| 59 |
hiddenText={item.name} |
| 60 |
highlight |
| 61 |
/> |
| 62 |
</> |
| 63 |
); |
| 64 |
} |
| 65 |
|