DonationRowActions.tsx
9 months ago
DonationsListTable.tsx
9 months ago
ListTable.module.scss
10 months ago
DonationRowActions.tsx
79 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 | |
| 7 | /** |
| 8 | * @since 4.12.0 Revert delete action to use Donation Actions API and add trash and restore actions. |
| 9 | * @since 4.6.0 Soft delete donations with Donation v3 API. |
| 10 | */ |
| 11 | export const DonationRowActions = ({item, removeRow, setUpdateErrors, parameters, listTableApi}) => { |
| 12 | const showConfirmModal = useContext(ShowConfirmModalContext); |
| 13 | const {mutate} = useSWRConfig(); |
| 14 | |
| 15 | const fetchAndUpdateErrors = async (parameters, endpoint, id, method) => { |
| 16 | const response = await listTableApi.fetchWithArgs(endpoint, {ids: [id]}, method); |
| 17 | setUpdateErrors(response); |
| 18 | await mutate(parameters); |
| 19 | return response; |
| 20 | }; |
| 21 | |
| 22 | const deleteItem = async () => await fetchAndUpdateErrors(parameters, '/delete', item.id, 'DELETE'); |
| 23 | const trashItem = async () => await fetchAndUpdateErrors(parameters, '/trash', item.id, 'DELETE'); |
| 24 | const restoreItem = async () => await fetchAndUpdateErrors(parameters, '/untrash', item.id, 'POST'); |
| 25 | |
| 26 | const confirmDelete = () => <p>{sprintf(__('Really delete donation #%d?', 'give'), item.id)}</p>; |
| 27 | const confirmTrash = () => <p>{sprintf(__('Trash the following donation #%d?', 'give'), item.id)}</p>; |
| 28 | const confirmRestore = () => <p>{sprintf(__('Restore the following donation #%d?', 'give'), item.id)}</p>; |
| 29 | |
| 30 | const confirmDeleteModal = () => { |
| 31 | showConfirmModal(__('Delete', 'give'), confirmDelete, deleteItem, 'danger'); |
| 32 | }; |
| 33 | |
| 34 | const confirmTrashModal = () => { |
| 35 | showConfirmModal(__('Trash', 'give'), confirmTrash, trashItem, 'warning'); |
| 36 | }; |
| 37 | |
| 38 | const confirmRestoreModal = () => { |
| 39 | showConfirmModal(__('Restore', 'give'), confirmRestore, restoreItem, 'normal'); |
| 40 | }; |
| 41 | |
| 42 | return ( |
| 43 | <> |
| 44 | {parameters?.status?.includes('trash') ? ( |
| 45 | <> |
| 46 | <RowAction |
| 47 | onClick={confirmRestoreModal} |
| 48 | actionId={item.id} |
| 49 | displayText={__('Restore', 'give')} |
| 50 | /> |
| 51 | <RowAction |
| 52 | onClick={confirmDeleteModal} |
| 53 | actionId={item.id} |
| 54 | displayText={__('Delete', 'give')} |
| 55 | hiddenText={item.name} |
| 56 | highlight |
| 57 | /> |
| 58 | </> |
| 59 | ) : ( |
| 60 | <> |
| 61 | <RowAction |
| 62 | href={`${window.GiveDonations.adminUrl}edit.php?post_type=give_forms&page=give-payment-history&id=${item.id}`} |
| 63 | displayText={__('Edit', 'give')} |
| 64 | ariaLabel={sprintf(__('Edit donation #%d.', 'give'), item.id)} |
| 65 | /> |
| 66 | <RowAction |
| 67 | onClick={confirmTrashModal} |
| 68 | actionId={item.id} |
| 69 | displayText={__('Trash', 'give')} |
| 70 | ariaLabel={sprintf(__('Move donation #%d to trash.', 'give'), item.id)} |
| 71 | hiddenText={item.name} |
| 72 | highlight |
| 73 | /> |
| 74 | </> |
| 75 | )} |
| 76 | </> |
| 77 | ); |
| 78 | }; |
| 79 |