| 1 |
import {useEffect} from 'react'; |
| 2 |
import {useBlockProps} from '@wordpress/block-editor'; |
| 3 |
import {BlockEditProps} from '@wordpress/blocks'; |
| 4 |
import {__} from '@wordpress/i18n'; |
| 5 |
import BlockInspectorControls from './components/BlockInspectorControls'; |
| 6 |
import type {BlockPreviewProps} from './components/BlockPreview'; |
| 7 |
import BlockPreview from './components/BlockPreview'; |
| 8 |
import useFormOptions from '../../shared/hooks/useFormOptions'; |
| 9 |
import CampaignSelector from '../../shared/components/CampaignSelector'; |
| 10 |
import '../../shared/components/EntitySelector/styles/index.scss'; |
| 11 |
|
| 12 |
import './styles.scss'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @since 4.3.0 |
| 16 |
* |
| 17 |
* @see 'class-give-block-donation-form.php' |
| 18 |
*/ |
| 19 |
type CampaignFormBlockAttributes = { |
| 20 |
campaignId: number; |
| 21 |
id: number; |
| 22 |
prevId: number; |
| 23 |
blockId: string; |
| 24 |
displayStyle: BlockPreviewProps['displayStyle']; |
| 25 |
continueButtonTitle: string; |
| 26 |
showTitle: boolean; |
| 27 |
showGoal: boolean; |
| 28 |
showContent: boolean; |
| 29 |
contentDisplay: string; |
| 30 |
useDefaultForm: boolean; |
| 31 |
}; |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 4.3.0 |
| 35 |
*/ |
| 36 |
export default function Edit({attributes, isSelected, setAttributes, className, clientId}: BlockEditProps<any>) { |
| 37 |
const { |
| 38 |
id, |
| 39 |
blockId, |
| 40 |
displayStyle, |
| 41 |
continueButtonTitle = __('Donate now', 'give'), |
| 42 |
} = attributes as CampaignFormBlockAttributes; |
| 43 |
const {formOptions, isResolving} = useFormOptions(attributes?.campaignId); |
| 44 |
|
| 45 |
useEffect(() => { |
| 46 |
if (!blockId) { |
| 47 |
setAttributes({blockId: clientId}); |
| 48 |
} |
| 49 |
|
| 50 |
if (!isLegacyForm && displayStyle === 'reveal') { |
| 51 |
setAttributes({displayStyle: 'modal'}); |
| 52 |
} |
| 53 |
}, []); |
| 54 |
|
| 55 |
const [isLegacyForm, isLegacyTemplate, link] = (() => { |
| 56 |
const form = formOptions.find((form) => form.value === id); |
| 57 |
|
| 58 |
return [form?.isLegacyForm, form?.isLegacyTemplate, form?.link]; |
| 59 |
})(); |
| 60 |
|
| 61 |
return ( |
| 62 |
<div {...useBlockProps()}> |
| 63 |
<CampaignSelector |
| 64 |
campaignId={attributes.campaignId} |
| 65 |
handleSelect={(campaignId: number) => setAttributes({campaignId: Number(campaignId)})} |
| 66 |
> |
| 67 |
{id && ( |
| 68 |
<BlockPreview |
| 69 |
clientId={clientId} |
| 70 |
formId={id} |
| 71 |
displayStyle={displayStyle} |
| 72 |
continueButtonTitle={continueButtonTitle} |
| 73 |
link={link} |
| 74 |
isLegacyForm={isLegacyForm} |
| 75 |
attributes={attributes} |
| 76 |
isSelected={isSelected} |
| 77 |
className={className} |
| 78 |
/> |
| 79 |
)} |
| 80 |
<BlockInspectorControls |
| 81 |
attributes={attributes} |
| 82 |
setAttributes={setAttributes} |
| 83 |
entityOptions={formOptions} |
| 84 |
isResolving={isResolving} |
| 85 |
isLegacyTemplate={isLegacyTemplate} |
| 86 |
isLegacyForm={isLegacyForm} |
| 87 |
/> |
| 88 |
</CampaignSelector> |
| 89 |
</div> |
| 90 |
); |
| 91 |
} |
| 92 |
|