DonationRowActions.tsx
3 years ago
DonationsListTable.tsx
3 years ago
ListTable.module.scss
3 years ago
DonationRowActions.tsx
48 lines
| 1 | import {useContext} from 'react'; |
| 2 | import {ShowConfirmModalContext} from '@givewp/components/ListTable/ListTablePage'; |
| 3 | import {__, sprintf} from '@wordpress/i18n'; |
| 4 | import RowAction from '@givewp/components/ListTable/RowAction'; |
| 5 | import {useSWRConfig} from 'swr'; |
| 6 | import ListTableApi from '@givewp/components/ListTable/api'; |
| 7 | |
| 8 | const API = new ListTableApi(window.GiveDonations); |
| 9 | |
| 10 | export const DonationRowActions = ({item, removeRow, setUpdateErrors, parameters}) => { |
| 11 | const showConfirmModal = useContext(ShowConfirmModalContext); |
| 12 | const {mutate} = useSWRConfig(); |
| 13 | |
| 14 | const fetchAndUpdateErrors = async (parameters, endpoint, id, method) => { |
| 15 | const response = await API.fetchWithArgs(endpoint, {ids: [id]}, method); |
| 16 | setUpdateErrors(response); |
| 17 | await mutate(parameters); |
| 18 | return response; |
| 19 | }; |
| 20 | |
| 21 | const deleteItem = async (selected) => await fetchAndUpdateErrors(parameters, '/delete', item.id, 'DELETE'); |
| 22 | |
| 23 | const confirmDelete = (selected) => <p>{sprintf(__('Really delete donation #%d?', 'give'), item.id)}</p>; |
| 24 | |
| 25 | const confirmModal = (event) => { |
| 26 | showConfirmModal(__('Delete', 'give'), confirmDelete, deleteItem, 'danger'); |
| 27 | }; |
| 28 | |
| 29 | return ( |
| 30 | <> |
| 31 | <RowAction |
| 32 | href={ |
| 33 | window.GiveDonations.adminUrl + |
| 34 | `edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&id=${item.id}` |
| 35 | } |
| 36 | displayText={__('Edit', 'give')} |
| 37 | /> |
| 38 | <RowAction |
| 39 | onClick={confirmModal} |
| 40 | actionId={item.id} |
| 41 | displayText={__('Delete', 'give')} |
| 42 | hiddenText={item.name} |
| 43 | highlight |
| 44 | /> |
| 45 | </> |
| 46 | ); |
| 47 | }; |
| 48 |