| 1 |
import {useEffect, useState} from 'react'; |
| 2 |
import {useBlockProps} from '@wordpress/block-editor'; |
| 3 |
import {BlockEditProps} from '@wordpress/blocks'; |
| 4 |
import ServerSideRender from '@wordpress/server-side-render'; |
| 5 |
import {__} from '@wordpress/i18n'; |
| 6 |
import useFormOptions from './hooks/useFormOptions'; |
| 7 |
import DonationFormBlockControls from './components/DonationFormBlockControls'; |
| 8 |
import type {BlockPreviewProps} from './components/DonationFormBlockPreview'; |
| 9 |
import DonationFormBlockPreview from './components/DonationFormBlockPreview'; |
| 10 |
import EntitySelector from '@givewp/src/Campaigns/Blocks/shared/components/EntitySelector/EntitySelector'; |
| 11 |
import './styles/index.scss'; |
| 12 |
|
| 13 |
/** |
| 14 |
* @since 4.3.0 replace DonationFormSelector with Campaigns EntitySelector. |
| 15 |
* @since 3.2.1 |
| 16 |
* |
| 17 |
* @see 'class-give-block-donation-form.php' |
| 18 |
*/ |
| 19 |
type DonationFormBlockAttributes = { |
| 20 |
id: number; |
| 21 |
prevId: number; |
| 22 |
blockId: string; |
| 23 |
displayStyle: BlockPreviewProps['formFormat']; |
| 24 |
continueButtonTitle: string; |
| 25 |
showTitle: boolean; |
| 26 |
showGoal: boolean; |
| 27 |
showContent: boolean; |
| 28 |
contentDisplay: string; |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* @since 3.2.1 added isResolving loading state to prevent forms from prematurely being rendered. |
| 33 |
* @since 3.2.0 updated to handle v2 forms. |
| 34 |
* @since 3.0.0 |
| 35 |
*/ |
| 36 |
export default function Edit({attributes, isSelected, setAttributes, className, clientId}: BlockEditProps<any>) { |
| 37 |
const {id, blockId, displayStyle, continueButtonTitle} = attributes as DonationFormBlockAttributes; |
| 38 |
const {formOptions, isResolving} = useFormOptions(); |
| 39 |
const [showPreview, setShowPreview] = useState<boolean>(!!id); |
| 40 |
|
| 41 |
const handleSelect = (id) => { |
| 42 |
setAttributes({id}); |
| 43 |
setShowPreview(true); |
| 44 |
}; |
| 45 |
|
| 46 |
useEffect(() => { |
| 47 |
if (!blockId) { |
| 48 |
setAttributes({blockId: clientId}); |
| 49 |
} |
| 50 |
|
| 51 |
if (!isLegacyForm && displayStyle === 'reveal') { |
| 52 |
setAttributes({displayStyle: 'modal'}); |
| 53 |
} |
| 54 |
}, []); |
| 55 |
|
| 56 |
const [isLegacyForm, isLegacyTemplate, link] = (() => { |
| 57 |
const form = formOptions.find((form) => form.value === id); |
| 58 |
|
| 59 |
return [form?.isLegacyForm, form?.isLegacyTemplate, form?.link]; |
| 60 |
})(); |
| 61 |
|
| 62 |
if (isResolving !== false) { |
| 63 |
return ( |
| 64 |
<div {...useBlockProps()}> |
| 65 |
<p>{__('Loading...', 'give')}</p> |
| 66 |
</div> |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return ( |
| 71 |
<div {...useBlockProps()}> |
| 72 |
{id && showPreview ? ( |
| 73 |
<> |
| 74 |
<DonationFormBlockControls |
| 75 |
attributes={attributes} |
| 76 |
setAttributes={setAttributes} |
| 77 |
formOptions={formOptions} |
| 78 |
isResolving={isResolving} |
| 79 |
isLegacyTemplate={isLegacyTemplate} |
| 80 |
isLegacyForm={isLegacyForm} |
| 81 |
/> |
| 82 |
|
| 83 |
{isLegacyForm ? ( |
| 84 |
<div className={!!isSelected ? `${className} isSelected` : className}> |
| 85 |
<ServerSideRender block="give/donation-form" attributes={attributes} /> |
| 86 |
</div> |
| 87 |
) : ( |
| 88 |
<DonationFormBlockPreview |
| 89 |
clientId={clientId} |
| 90 |
formId={id} |
| 91 |
formFormat={displayStyle} |
| 92 |
openFormButton={continueButtonTitle} |
| 93 |
link={link} |
| 94 |
/> |
| 95 |
)} |
| 96 |
</> |
| 97 |
) : ( |
| 98 |
<EntitySelector |
| 99 |
id={'formId'} |
| 100 |
label={__('Choose a donation form', 'give')} |
| 101 |
options={formOptions} |
| 102 |
isLoading={isResolving} |
| 103 |
emptyMessage={__('No donation forms were found.', 'give')} |
| 104 |
loadingMessage={__('Loading Donation Forms...', 'give')} |
| 105 |
buttonText={__('Confirm', 'give')} |
| 106 |
onConfirm={handleSelect} |
| 107 |
/> |
| 108 |
)} |
| 109 |
</div> |
| 110 |
); |
| 111 |
} |
| 112 |
|