| 1 |
import {__} from '@wordpress/i18n'; |
| 2 |
import {useSelect} from '@wordpress/data'; |
| 3 |
import {addQueryArgs} from '@wordpress/url'; |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import useSWR from 'swr'; |
| 6 |
import {InspectorControls, useBlockProps} from '@wordpress/block-editor'; |
| 7 |
import {BlockEditProps} from '@wordpress/blocks'; |
| 8 |
import {PanelBody, SelectControl, TextControl, ToggleControl} from '@wordpress/components'; |
| 9 |
import ServerSideRender from '@wordpress/server-side-render'; |
| 10 |
import useCampaign from '../shared/hooks/useCampaign'; |
| 11 |
import CampaignSelector from '../shared/components/CampaignSelector'; |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* @since 4.0.0 |
| 16 |
*/ |
| 17 |
export default function Edit({attributes, setAttributes}: BlockEditProps<{ |
| 18 |
campaignId: number; |
| 19 |
buttonText: string; |
| 20 |
useDefaultForm: boolean; |
| 21 |
selectedForm: string; |
| 22 |
}>) { |
| 23 |
const blockProps = useBlockProps(); |
| 24 |
const {buttonText = __('Donate', 'give')} = attributes; |
| 25 |
const {campaign, hasResolved} = useCampaign(attributes.campaignId); |
| 26 |
|
| 27 |
const adminBaseUrl = useSelect( |
| 28 |
// @ts-ignore |
| 29 |
(select) => select('core').getSite()?.url + '/wp-admin/edit.php?post_type=give_forms&page=give-campaigns', |
| 30 |
[] |
| 31 |
); |
| 32 |
|
| 33 |
const campaignForms = (() => { |
| 34 |
const {data, isLoading}: { data: { items: [] }, isLoading: boolean } = useSWR( |
| 35 |
addQueryArgs('/give-api/v2/admin/forms', {campaignId: attributes.campaignId, status: 'publish'}), |
| 36 |
path => apiFetch({path}) |
| 37 |
) |
| 38 |
|
| 39 |
if (isLoading) { |
| 40 |
return [{label: __('Loading...', 'give'), value: ''}] |
| 41 |
} |
| 42 |
|
| 43 |
const options = data?.items.map((form: { name: string, id: string }) => ({ |
| 44 |
label: form.name, |
| 45 |
value: form.id |
| 46 |
})) |
| 47 |
|
| 48 |
return [ |
| 49 |
{label: __('Select form', 'give'), value: ''}, |
| 50 |
...options |
| 51 |
]; |
| 52 |
})(); |
| 53 |
|
| 54 |
return ( |
| 55 |
<div {...blockProps}> |
| 56 |
<CampaignSelector |
| 57 |
campaignId={attributes.campaignId} |
| 58 |
handleSelect={(campaignId: number) => setAttributes({campaignId})} |
| 59 |
> |
| 60 |
<ServerSideRender block="givewp/campaign-donate-button" attributes={attributes} /> |
| 61 |
</CampaignSelector> |
| 62 |
|
| 63 |
{hasResolved && campaign?.id && ( |
| 64 |
<InspectorControls> |
| 65 |
<PanelBody title="Settings" initialOpen={true}> |
| 66 |
<TextControl |
| 67 |
label={__('Donate button', 'give')} |
| 68 |
value={buttonText} |
| 69 |
onChange={(buttonText: string) => setAttributes({buttonText})} |
| 70 |
/> |
| 71 |
<ToggleControl |
| 72 |
label={__('Use default form', 'give')} |
| 73 |
checked={attributes.useDefaultForm} |
| 74 |
onChange={(useDefaultForm: boolean) => setAttributes({useDefaultForm})} |
| 75 |
help={ |
| 76 |
<> |
| 77 |
{__('Uses the campaign’s default form.', 'give')} |
| 78 |
{` `} |
| 79 |
<a |
| 80 |
href={`${adminBaseUrl}&id=${attributes.campaignId}&tab=forms`} |
| 81 |
target="_blank" |
| 82 |
rel="noopener noreferrer" |
| 83 |
aria-label={__('Change campaign default form', 'give')} |
| 84 |
> |
| 85 |
{__('Change default form', 'give')} |
| 86 |
</a> |
| 87 |
</> |
| 88 |
} |
| 89 |
/> |
| 90 |
{!attributes.useDefaultForm && ( |
| 91 |
<SelectControl |
| 92 |
label={__('Form', 'give')} |
| 93 |
onChange={(selectedForm: string) => setAttributes({selectedForm})} |
| 94 |
options={campaignForms} |
| 95 |
value={attributes.selectedForm} |
| 96 |
help={__('Donations are collected through this form.', 'give')} |
| 97 |
/> |
| 98 |
)} |
| 99 |
</PanelBody> |
| 100 |
</InspectorControls> |
| 101 |
)} |
| 102 |
</div> |
| 103 |
); |
| 104 |
} |
| 105 |
|