-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/block.json", | ||
"apiVersion": 2, | ||
"name": "givewp/campaign-donate-button", | ||
"version": "1.0.0", | ||
"title": "Donate Button", | ||
"category": "give", | ||
"icon": "button", | ||
"description": "The GiveWP Donate Button inserts an donate button into the page.", | ||
"supports": { | ||
"align": [ | ||
"wide", | ||
"full" | ||
] | ||
}, | ||
"attributes": { | ||
"campaignId": { | ||
"type": "number" | ||
}, | ||
"useDefaultForm": { | ||
"type": "boolean", | ||
"default": true | ||
}, | ||
"selectedForm": { | ||
"type": "string" | ||
}, | ||
"buttonText": { | ||
"type": "string", | ||
"default": "Donate now" | ||
} | ||
}, | ||
"textdomain": "give", | ||
"editorStyle": "file:../../../../build/campaignDonateButtonBlock.css", | ||
"editorScript": "file:../../../../build/campaignDonateButtonBlock.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {__} from '@wordpress/i18n'; | ||
import {useSelect} from '@wordpress/data'; | ||
import {InspectorControls, useBlockProps} from '@wordpress/block-editor'; | ||
import {BlockEditProps} from '@wordpress/blocks'; | ||
import {PanelBody, SelectControl, TextControl, ToggleControl} from '@wordpress/components'; | ||
import useCampaign from '../shared/hooks/useCampaign'; | ||
import {CampaignSelector} from '../shared/components/CampaignSelector'; | ||
|
||
export default function Edit({attributes, setAttributes,}: BlockEditProps<{ | ||
campaignId: number; | ||
buttonText: string; | ||
useDefaultForm: boolean; | ||
selectedForm: string; | ||
}>) { | ||
const blockProps = useBlockProps(); | ||
const {campaign, hasResolved} = useCampaign(attributes.campaignId); | ||
|
||
const adminBaseUrl = useSelect( | ||
// @ts-ignore | ||
(select) => select('core').getSite()?.url + '/wp-admin/edit.php?post_type=give_forms&page=give-campaigns', | ||
[] | ||
); | ||
|
||
const campaignForms = (() => { | ||
const {forms, isLoading} = campaign.forms(); | ||
|
||
if (isLoading) { | ||
return [] | ||
} | ||
|
||
return forms.map((form: { name: string, id: string }) => ({ | ||
label: form.name, | ||
value: form.id | ||
})) | ||
})(); | ||
|
||
return ( | ||
<div {...blockProps}> | ||
|
||
<CampaignSelector attributes={attributes} setAttributes={setAttributes}> | ||
<div> | ||
{attributes.buttonText} | ||
</div> | ||
</CampaignSelector> | ||
|
||
{hasResolved && campaign?.id && ( | ||
<InspectorControls> | ||
<PanelBody title="Settings" initialOpen={true}> | ||
<TextControl | ||
label={__('Donate button', 'give')} | ||
value={attributes.buttonText} | ||
onChange={(buttonText: string) => setAttributes({buttonText})} | ||
/> | ||
<ToggleControl | ||
label={__('Use default form', 'give')} | ||
checked={attributes.useDefaultForm} | ||
onChange={(useDefaultForm: boolean) => setAttributes({useDefaultForm})} | ||
help={ | ||
<> | ||
{__('Uses the campaign’s default form', 'give')}. | ||
{` `} | ||
<a | ||
href={`${adminBaseUrl}&id=${attributes.campaignId}&tab=forms`} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
aria-label={__('Change campaign default form', 'give')} | ||
> | ||
{__('Change default form', 'give')} | ||
</a> | ||
</> | ||
} | ||
/> | ||
<SelectControl | ||
label={__('Form', 'give')} | ||
onChange={(selectedForm: string) => setAttributes({selectedForm})} | ||
options={campaignForms} | ||
value={attributes.selectedForm} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import metadata from './block.json'; | ||
import Edit from './edit'; | ||
import initBlock from '../shared/utils/init-block'; | ||
|
||
const {name} = metadata; | ||
|
||
export const init = () => initBlock({ | ||
name, | ||
metadata, | ||
settings: { | ||
edit: Edit, | ||
} | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
import {useEntityRecord} from '@wordpress/core-data'; | ||
import {Campaign} from '@givewp/campaigns/admin/components/types'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import {addQueryArgs} from '@wordpress/url'; | ||
import useSWR from 'swr'; | ||
|
||
export default function useCampaign(campaignId: number) { | ||
const data = useEntityRecord('givewp', 'campaign', campaignId); | ||
const campaignData = useEntityRecord('givewp', 'campaign', campaignId); | ||
|
||
return { | ||
campaign: data?.record as Campaign, | ||
hasResolved: data?.hasResolved, | ||
campaign: { | ||
...campaignData?.record as Campaign, | ||
forms: (params: FormsApiParams = {status: 'publish'}) => { | ||
const {data, isLoading}: { data: { items: [] }, isLoading: boolean } = useSWR( | ||
addQueryArgs('/give-api/v2/admin/forms', {campaignId, ...params}), | ||
path => apiFetch({path}) | ||
) | ||
|
||
return { | ||
forms: data?.items || [], | ||
isLoading | ||
} | ||
} | ||
}, | ||
hasResolved: campaignData?.hasResolved, | ||
}; | ||
} | ||
|
||
interface FormsApiParams { | ||
status?: 'publish' | 'draft' | 'pending' | 'trash' | 'upgraded' | 'any' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters