| 1 |
import {__, sprintf} 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 {UpgradeModalContent} from './Migration'; |
| 9 |
import {createInterpolateElement} from '@wordpress/element'; |
| 10 |
|
| 11 |
const donationFormsApi = new ListTableApi(window.GiveDonationForms); |
| 12 |
|
| 13 |
export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdateErrors, parameters, entity}) { |
| 14 |
const {mutate} = useSWRConfig(); |
| 15 |
const showConfirmModal = useContext(ShowConfirmModalContext); |
| 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) => { |
| 27 |
try { |
| 28 |
await fetchAndUpdateErrors(parameters, deleteEndpoint, item.id, 'DELETE'); |
| 29 |
} catch (error) { |
| 30 |
const errorData = JSON.parse(error.message.replace('Error: ', '')); |
| 31 |
alert(errorData.message); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
const confirmDeleteForm = (selected) => ( |
| 36 |
<p> |
| 37 |
{__('Really delete the following form?', 'give')} |
| 38 |
<br /> |
| 39 |
<Interweave content={item?.title} /> |
| 40 |
</p> |
| 41 |
); |
| 42 |
|
| 43 |
const confirmTrashForm = (selected) => ( |
| 44 |
<p> |
| 45 |
{__('Are you sure you want to trash the following donation form? ', 'give')} |
| 46 |
<br /> |
| 47 |
<Interweave content={item?.title} /> |
| 48 |
</p> |
| 49 |
); |
| 50 |
|
| 51 |
const confirmModal = (event) => { |
| 52 |
showConfirmModal(__('Delete', 'give'), confirmDeleteForm, deleteForm, 'danger'); |
| 53 |
}; |
| 54 |
|
| 55 |
const confirmTrashModal = (event) => { |
| 56 |
showConfirmModal(__('Trash', 'give'), confirmTrashForm, deleteForm, 'danger'); |
| 57 |
}; |
| 58 |
|
| 59 |
const confirmUpgradeModal = (event) => { |
| 60 |
showConfirmModal(__('Upgrade', 'give'), UpgradeModalContent, async (selected) => { |
| 61 |
const response = await donationFormsApi.fetchWithArgs('/migrate/' + item.id, {}, 'POST'); |
| 62 |
await mutate(parameters); |
| 63 |
return response; |
| 64 |
}); |
| 65 |
}; |
| 66 |
|
| 67 |
const urlParams = new URLSearchParams(window.location.search); |
| 68 |
const isCampaignDetailsPage = |
| 69 |
urlParams.get('id') && urlParams.get('page') && 'give-campaigns' === urlParams.get('page'); |
| 70 |
|
| 71 |
const defaultCampaignModalContent = createInterpolateElement( |
| 72 |
__('This will set <title_link/> as the default form for this campaign. Do you want to proceed?', 'give'), |
| 73 |
{ |
| 74 |
title_link: <Interweave content={item?.title} />, |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
const confirmDefaultCampaignFormModal = (event) => { |
| 79 |
showConfirmModal( |
| 80 |
__('Make as default', 'give'), |
| 81 |
(selected) => <p>{defaultCampaignModalContent}</p>, |
| 82 |
async () => { |
| 83 |
await entity.edit({ |
| 84 |
defaultFormId: item.id, |
| 85 |
}); |
| 86 |
|
| 87 |
const response = await entity.save(); |
| 88 |
|
| 89 |
await mutate(parameters); |
| 90 |
return response; |
| 91 |
}, |
| 92 |
__('Yes proceed', 'give') |
| 93 |
); |
| 94 |
}; |
| 95 |
|
| 96 |
const copyShortcode = (event) => { |
| 97 |
navigator.clipboard.writeText(`[give_form id="${item.id}"]`); |
| 98 |
alert(sprintf(__('The shortcode for Donation Form #%d has been copied to your clipboard!', 'give'), item.id)); |
| 99 |
}; |
| 100 |
|
| 101 |
return ( |
| 102 |
<> |
| 103 |
{parameters.status === 'trash' ? ( |
| 104 |
<> |
| 105 |
<RowAction |
| 106 |
onClick={removeRow( |
| 107 |
async () => await fetchAndUpdateErrors(parameters, '/restore', item.id, 'POST') |
| 108 |
)} |
| 109 |
actionId={item.id} |
| 110 |
displayText={__('Restore', 'give')} |
| 111 |
hiddenText={item?.name} |
| 112 |
/> |
| 113 |
<RowAction |
| 114 |
onClick={confirmModal} |
| 115 |
actionId={item.id} |
| 116 |
displayText={__('Delete Permanently', 'give')} |
| 117 |
hiddenText={item?.name} |
| 118 |
highlight |
| 119 |
/> |
| 120 |
</> |
| 121 |
) : ( |
| 122 |
<> |
| 123 |
<RowAction href={item.edit} displayText={__('Edit', 'give')} hiddenText={item?.name} /> |
| 124 |
{!item.isDefaultCampaignForm && ( |
| 125 |
<RowAction |
| 126 |
onClick={confirmTrashModal} |
| 127 |
actionId={item.id} |
| 128 |
highlight={true} |
| 129 |
displayText={trashEnabled ? __('Trash', 'give') : __('Delete', 'give')} |
| 130 |
hiddenText={item?.name} |
| 131 |
/> |
| 132 |
)} |
| 133 |
<RowAction href={item.permalink} displayText={__('View', 'give')} hiddenText={item?.name} /> |
| 134 |
<RowAction |
| 135 |
onClick={addRow(async (id) => await fetchAndUpdateErrors(parameters, '/duplicate', id, 'POST'))} |
| 136 |
actionId={item.id} |
| 137 |
displayText={__('Duplicate', 'give')} |
| 138 |
hiddenText={item?.name} |
| 139 |
/> |
| 140 |
{!item.v3form && ( |
| 141 |
<RowAction |
| 142 |
onClick={confirmUpgradeModal} |
| 143 |
actionId={item.id} |
| 144 |
displayText={__('Upgrade', 'give')} |
| 145 |
hiddenText={item?.name} |
| 146 |
/> |
| 147 |
)} |
| 148 |
<RowAction |
| 149 |
onClick={copyShortcode} |
| 150 |
actionId={item.id} |
| 151 |
displayText={__('Copy shortcode', 'give')} |
| 152 |
hiddenText={item?.name} |
| 153 |
/> |
| 154 |
{isCampaignDetailsPage && !item.isDefaultCampaignForm && ( |
| 155 |
<RowAction |
| 156 |
onClick={confirmDefaultCampaignFormModal} |
| 157 |
actionId={item.id} |
| 158 |
displayText={__('Make as default', 'give')} |
| 159 |
hiddenText={item?.name} |
| 160 |
/> |
| 161 |
)} |
| 162 |
</> |
| 163 |
)} |
| 164 |
</> |
| 165 |
); |
| 166 |
} |
| 167 |
|