import {FormProvider, SubmitHandler, useForm} from 'react-hook-form'; import {__} from '@wordpress/i18n'; import styles from './Form.module.scss'; import FormModal from '../../FormModal'; import {MergeCampaignFormInputs, MergeCampaignFormProps} from './types'; import {useState} from 'react'; import apiFetch from '@wordpress/api-fetch'; import {addQueryArgs} from '@wordpress/url'; import {getGiveCampaignsListTableWindowData} from '../../CampaignsListTable'; /** * Campaign Form Modal component * * @since 4.0.0 */ export default function MergeCampaignsForm({isOpen, handleClose, title, campaigns}: MergeCampaignFormProps) { const [step, setStep] = useState(1); const methods = useForm({ defaultValues: { destinationCampaignId: '', }, }); const { register, handleSubmit, formState: {isDirty, isSubmitting}, watch, } = methods; const destinationCampaignId = watch('destinationCampaignId'); const requiredAsterisk = *; const onSubmit: SubmitHandler = async (inputs, event) => { event.preventDefault(); if (step !== 2) { return; } const campaignsToMergeIds = campaigns.selected.filter((id) => id != inputs.destinationCampaignId); try { const response = await apiFetch({ path: addQueryArgs('/givewp/v3/campaigns/' + destinationCampaignId + '/merge', { campaignsToMergeIds: campaignsToMergeIds, }), method: 'PATCH', }); console.log('Merge campaigns response: ', response); // Go to success page setStep(3); //Reset bulk actions selector const selects = document.querySelectorAll('#give-admin-campaigns-root select'); selects.forEach((select) => { const selectElement = select as HTMLSelectElement; selectElement.selectedIndex = 0; }); // Uncheck all checkboxes const checkboxes = document.querySelectorAll(".giveListTable input[type='checkbox']"); checkboxes.forEach((checkbox) => { const input = checkbox as HTMLInputElement; input.checked = false; }); // @ts-ignore document.querySelector('.giveListTable #giveListTableSelectAll').checked = false; //Remove campaignsToMergeIds from the list table. const adminFormsListViewItems = document.querySelectorAll('tr'); if (adminFormsListViewItems.length > 0) { adminFormsListViewItems.forEach((itemElement) => { const select = itemElement.querySelector('.giveListTableSelect'); if (!select) { return; } const campaignId = select.getAttribute('data-id'); if (campaignsToMergeIds.includes(campaignId)) { itemElement.remove(); } }); } //handleClose(response); } catch (error) { // Go to error page setStep(4); console.error('Error merging campaigns: ', error); } }; const extractTextFromLink = (link) => { const parser = new DOMParser(); const doc = parser.parseFromString(link, 'text/html'); return doc.querySelector('a')?.textContent || link; }; return ( {step === 1 && ( <>

{__( 'All selected campaigns will be merged into the destination campaign. This means that forms, donors, donations, and all related data will be added to the destination campaign, and the merged campaigns will cease to exist.', 'give' )}

)} {step === 2 && ( <>
{__('All selected campaigns will be merged into this campaign.', 'give')}
{isDirty && (

{__('Once completed, this action is irreversible.', 'give')}

)} )} {step === 3 && ( <>
{__( 'All donations, donors, and forms from selected campaigns now belong to your destination campaign.', 'give' )}
)} {step === 4 && ( <>
{__( 'An error occurred during the merging process. Please try again, or contact our support team if the issue persists.', 'give' )}
)}
); }