| 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 () => await fetchAndUpdateErrors(parameters, '/delete', item.id, 'DELETE'); |
| 22 |
const trashItem = async () => await fetchAndUpdateErrors(parameters, '/trash', item.id, 'DELETE'); |
| 23 |
const restoreItem = async () => await fetchAndUpdateErrors(parameters, '/untrash', item.id, 'POST'); |
| 24 |
|
| 25 |
const confirmDelete = () => <p>{sprintf(__('Really delete subscription #%d?', 'give'), item.id)}</p>; |
| 26 |
const confirmTrash = () => <p>{sprintf(__('Trash the following subscription #%d?', 'give'), item.id)}</p>; |
| 27 |
const confirmRestore = () => <p>{sprintf(__('Restore the following subscription #%d?', 'give'), item.id)}</p>; |
| 28 |
|
| 29 |
const confirmDeleteModal = () => { |
| 30 |
showConfirmModal(__('Delete', 'give'), confirmDelete, deleteItem, 'danger'); |
| 31 |
}; |
| 32 |
|
| 33 |
const confirmTrashModal = () => { |
| 34 |
showConfirmModal(__('Trash', 'give'), confirmTrash, trashItem, 'warning'); |
| 35 |
}; |
| 36 |
|
| 37 |
const confirmRestoreModal = () => { |
| 38 |
showConfirmModal(__('Restore', 'give'), confirmRestore, restoreItem, 'normal'); |
| 39 |
}; |
| 40 |
|
| 41 |
return ( |
| 42 |
<> |
| 43 |
{parameters?.status?.includes('trashed') ? ( |
| 44 |
<> |
| 45 |
<RowAction |
| 46 |
onClick={confirmRestoreModal} |
| 47 |
actionId={item.id} |
| 48 |
displayText={__('Restore', 'give')} |
| 49 |
/> |
| 50 |
<RowAction |
| 51 |
onClick={confirmDeleteModal} |
| 52 |
actionId={item.id} |
| 53 |
displayText={__('Delete', 'give')} |
| 54 |
hiddenText={item.name} |
| 55 |
highlight |
| 56 |
/> |
| 57 |
</> |
| 58 |
) : ( |
| 59 |
<> |
| 60 |
<RowAction |
| 61 |
href={`edit.php?post_type=give_forms&page=give-subscriptions&id=${item.id}`} |
| 62 |
displayText={__('Edit', 'give')} |
| 63 |
/> |
| 64 |
<RowAction |
| 65 |
onClick={confirmTrashModal} |
| 66 |
actionId={item.id} |
| 67 |
displayText={__('Trash', 'give')} |
| 68 |
hiddenText={item.name} |
| 69 |
highlight |
| 70 |
/> |
| 71 |
</> |
| 72 |
)} |
| 73 |
</> |
| 74 |
); |
| 75 |
} |
| 76 |
|