SubscriptionsRowActions.tsx
45 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 subscriptionsApi = new ListTableApi(window.GiveSubscriptions); |
| 9 | |
| 10 | export function SubscriptionsRowActions({item, setUpdateErrors, parameters}) { |
| 11 | const showConfirmModal = useContext(ShowConfirmModalContext); |
| 12 | const {mutate} = useSWRConfig(); |
| 13 | |
| 14 | const fetchAndUpdateErrors = async (parameters, endpoint, id, method) => { |
| 15 | const response = await subscriptionsApi.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={`edit.php?post_type=give_forms&page=give-subscriptions&id=${item.id}`} |
| 33 | displayText={__('Edit', 'give')} |
| 34 | /> |
| 35 | <RowAction |
| 36 | onClick={confirmModal} |
| 37 | actionId={item.id} |
| 38 | displayText={__('Delete', 'give')} |
| 39 | hiddenText={item.name} |
| 40 | highlight |
| 41 | /> |
| 42 | </> |
| 43 | ); |
| 44 | } |
| 45 |