import {useState} from 'react'; import {__} from '@wordpress/i18n'; import {ListTableApi, ListTablePage} from '@givewp/components'; import {DonationFormsRowActions} from './DonationFormsRowActions'; import Onboarding, {OnboardingContext, OnboardingStateProps} from './Onboarding'; import styles from '@givewp/components/ListTable/ListTablePage/ListTablePage.module.scss'; import {BulkActionsConfig, ColumnFilterConfig, FilterConfig} from '@givewp/components/ListTable/ListTablePage'; import Select from '@givewp/components/ListTable/Select'; import {Interweave} from 'interweave'; import InterweaveSSR from '@givewp/components/ListTable/InterweaveSSR'; import BlankSlate from '@givewp/components/ListTable/BlankSlate'; import {CubeIcon} from '@givewp/components/AdminUI/Icons'; import AddCampaignFormModal from './AddCampaignFormModal'; import DefaultFormNotice from '@givewp/campaigns/admin/components/CampaignDetailsPage/Components/Notices/DefaultFormNotice'; import apiFetch from '@wordpress/api-fetch'; import {CampaignEntity} from '@givewp/campaigns/admin/components/types'; declare global { interface Window { GiveDonationForms: { apiNonce: string; bannerActionUrl: string; tooltipActionUrl: string; migrationApiRoot: string; defaultFormActionUrl: string; apiRoot: string; authors: Array<{id: string | number; name: string}>; table: {columns: Array}; pluginUrl: string; showUpgradedTooltip: boolean; isMigrated: boolean; migratedFormUrl: string; supportedAddons: Array; supportedGateways: Array; isOptionBasedFormEditorEnabled: boolean; locale: string; showDefaultFormTooltip: boolean; campaignUrl: string; }; GiveNextGen?: { newFormUrl: string; }; } } const API = new ListTableApi(window.GiveDonationForms); const donationStatus = [ { value: 'any', text: __('All Status', 'give'), }, { value: 'publish', text: __('Published', 'give'), }, { value: 'pending', text: __('Pending', 'give'), }, { value: 'draft', text: __('Draft', 'give'), }, { value: 'trash', text: __('Trash', 'give'), }, { value: 'upgraded', text: __('Upgraded', 'give'), }, ]; const urlParams = new URLSearchParams(window.location.search); const isCampaignDetailsPage = urlParams.get('id') && 'give-campaigns' === urlParams.get('page'); const campaignId = urlParams.get('id'); const donationFormsFilters: Array = [ { name: 'status', type: 'select', text: __('status', 'give'), ariaLabel: __('Filter donation forms by status', 'give'), options: donationStatus, }, { name: 'search', type: 'search', text: __('Search by name or ID', 'give'), ariaLabel: __('Search donation forms', 'give'), }, ]; if (isCampaignDetailsPage) { donationFormsFilters.push({ name: 'campaignId', type: 'hidden', options: [ { value: campaignId, text: __('All Campaign Forms', 'give'), }, ], }); } /** * @since 4.6.0 add option based form editor enabled check. */ const columnFilters: Array = [ { column: 'title', filter: (item) => { const CubeTooltip = () => (
{__('Uses the Visual Form Builder', 'give')}
); return ( <>
{item?.v3form && window?.GiveDonationForms?.isOptionBasedFormEditorEnabled && }
{item?.isDefaultCampaignForm && (
{__('Default')}
)}
); }, }, { column: 'status', filter: (item, column) => { if (window.GiveDonationForms.showUpgradedTooltip && item?.status_raw === 'upgraded') { return (
{__( 'The name of this form is already associated with an upgraded form. You can safely delete this form', 'give' )} .
{ e.currentTarget.parentElement.remove(); fetch(window.GiveDonationForms.tooltipActionUrl, {method: 'POST'}); }} > {__('Got it', 'give')}
); } return ; }, }, ]; const donationFormsBulkActions: Array = [ { label: __('Edit', 'give'), value: 'edit', action: async (selected) => { const authorSelect = document.getElementById('giveDonationFormsTableSetAuthor') as HTMLSelectElement; const author = authorSelect.value; const statusSelect = document.getElementById('giveDonationFormsTableSetStatus') as HTMLSelectElement; const status = statusSelect.value; if (!(author || status)) { return {errors: [], successes: []}; } const editParams = { ids: selected.join(','), author, status, }; return await API.fetchWithArgs('/edit', editParams, 'UPDATE'); }, confirm: (selected, names) => ( <>

{__(' Below are the donation forms to be edited.', 'give')}

    {selected.map((id, index) => (
  • ))}
), }, { label: __('Delete', 'give'), value: 'delete', type: 'danger', isVisible: (data, parameters) => parameters.status === 'trash' || !data?.trash, action: async (selected) => { try { return await API.fetchWithArgs('/delete', {ids: selected.join(',')}, 'DELETE'); } catch (error) { const errorData = JSON.parse(error.message.replace('Error: ', '')); alert(errorData.message); } }, confirm: (selected, names) => (

{__('Really delete the following donation forms?', 'give')}

    {selected.map((id, index) => (
  • ))}
), }, { label: __('Trash', 'give'), value: 'trash', type: 'danger', isVisible: (data, parameters) => parameters.status !== 'trash' && data?.trash, isIdSelectable: (id, data) => !(typeof data?.defaultForm === 'number') || data.defaultForm !== Number(id), action: async (selected) => { try { return await API.fetchWithArgs('/trash', {ids: selected.join(',')}, 'DELETE'); } catch (error) { const errorData = JSON.parse(error.message.replace('Error: ', '')); alert(errorData.message); } }, confirm: (selected, names) => (

{__('Are you sure you want to trash the following donation forms?', 'give')}

    {selected.map((id, index) => (
  • ))}
), }, ]; /** * Displays a blank slate for the Forms table. * @since 2.27.0 */ const ListTableBlankSlate = ( ); export default function DonationFormsListTable({entity}: {entity?: CampaignEntity}) { const [state, setState] = useState({ showFeatureNoticeDialog: false, showDefaultFormTooltip: window.GiveDonationForms.showDefaultFormTooltip, }); const handleDefaultFormTooltipDismiss = () => { apiFetch({ url: window.GiveDonationForms.defaultFormActionUrl, method: 'POST', }).then(() => { setState((prevState) => { return { ...prevState, showDefaultFormTooltip: false, }; }); }); }; const [isOpen, setOpen] = useState(false); const openModal = () => setOpen(true); const closeModal = () => setOpen(false); return ( { return DonationFormsRowActions({ data, item, removeRow, addRow, setUpdateErrors, parameters, entity, }); }} bulkActions={donationFormsBulkActions} apiSettings={window.GiveDonationForms} filterSettings={donationFormsFilters} listTableBlankSlate={ListTableBlankSlate} columnFilters={columnFilters} banner={Onboarding} contentMode={isCampaignDetailsPage} > {isCampaignDetailsPage ? (
{window.GiveDonationForms.isOptionBasedFormEditorEnabled ? ( <> ) : ( <> {__('Add campaign form', 'give')} )}
) : ( <> {window.GiveDonationForms.isOptionBasedFormEditorEnabled && ( )} )} {state.showDefaultFormTooltip && isCampaignDetailsPage && ( )}
); } const showLegacyDonationForms = async (event) => { await API.fetchWithArgs('/view', {isLegacy: 1}); window.location.href = '/wp-admin/edit.php?post_type=give_forms'; };