Onboarding
2 years ago
DonationFormsListTable.tsx
2 years ago
DonationFormsRowActions.tsx
2 years ago
DonationFormsRowActions.tsx
94 lines
| 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 {OnboardingContext} from './Onboarding'; |
| 9 | |
| 10 | const donationFormsApi = new ListTableApi(window.GiveDonationForms); |
| 11 | |
| 12 | export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdateErrors, parameters}) { |
| 13 | const {mutate} = useSWRConfig(); |
| 14 | const showConfirmModal = useContext(ShowConfirmModalContext); |
| 15 | const [OnboardingState, setOnboardingState] = useContext(OnboardingContext); |
| 16 | const trashEnabled = Boolean(data?.trash); |
| 17 | const deleteEndpoint = trashEnabled && !item.status.includes('trash') ? '/trash' : '/delete'; |
| 18 | |
| 19 | const fetchAndUpdateErrors = async (parameters, endpoint, id, method) => { |
| 20 | const response = await donationFormsApi.fetchWithArgs(endpoint, {ids: [id]}, method); |
| 21 | setUpdateErrors(response); |
| 22 | await mutate(parameters); |
| 23 | return response; |
| 24 | }; |
| 25 | |
| 26 | const deleteForm = async (selected) => await fetchAndUpdateErrors(parameters, deleteEndpoint, item.id, 'DELETE'); |
| 27 | |
| 28 | const confirmDeleteForm = (selected) => ( |
| 29 | <p> |
| 30 | {__('Really delete the following form?', 'give')} |
| 31 | <br /> |
| 32 | <Interweave content={item?.title} /> |
| 33 | </p> |
| 34 | ); |
| 35 | |
| 36 | const confirmTrashForm = (selected) => ( |
| 37 | <p> |
| 38 | {__('Really trash the following form?', 'give')} |
| 39 | <br /> |
| 40 | <Interweave content={item?.title} /> |
| 41 | </p> |
| 42 | ); |
| 43 | |
| 44 | const confirmModal = (event) => { |
| 45 | showConfirmModal(__('Delete', 'give'), confirmDeleteForm, deleteForm, 'danger'); |
| 46 | }; |
| 47 | |
| 48 | const confirmTrashModal = (event) => { |
| 49 | showConfirmModal(__('Trash', 'give'), confirmTrashForm, deleteForm, 'danger'); |
| 50 | }; |
| 51 | |
| 52 | return ( |
| 53 | <> |
| 54 | {parameters.status === 'trash' ? ( |
| 55 | <> |
| 56 | <RowAction |
| 57 | onClick={removeRow( |
| 58 | async () => await fetchAndUpdateErrors(parameters, '/restore', item.id, 'POST') |
| 59 | )} |
| 60 | actionId={item.id} |
| 61 | displayText={__('Restore', 'give')} |
| 62 | hiddenText={item?.name} |
| 63 | /> |
| 64 | <RowAction |
| 65 | onClick={confirmModal} |
| 66 | actionId={item.id} |
| 67 | displayText={__('Delete Permanently', 'give')} |
| 68 | hiddenText={item?.name} |
| 69 | highlight |
| 70 | /> |
| 71 | </> |
| 72 | ) : ( |
| 73 | <> |
| 74 | <RowAction href={item.edit} displayText={__('Edit', 'give')} hiddenText={item?.name} /> |
| 75 | <RowAction |
| 76 | onClick={confirmTrashModal} |
| 77 | actionId={item.id} |
| 78 | highlight={true} |
| 79 | displayText={trashEnabled ? __('Trash', 'give') : __('Delete', 'give')} |
| 80 | hiddenText={item?.name} |
| 81 | /> |
| 82 | <RowAction href={item.permalink} displayText={__('View', 'give')} hiddenText={item?.name} /> |
| 83 | <RowAction |
| 84 | onClick={addRow(async (id) => await fetchAndUpdateErrors(parameters, '/duplicate', id, 'POST'))} |
| 85 | actionId={item.id} |
| 86 | displayText={__('Duplicate', 'give')} |
| 87 | hiddenText={item?.name} |
| 88 | /> |
| 89 | </> |
| 90 | )} |
| 91 | </> |
| 92 | ); |
| 93 | } |
| 94 |