Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alaca committed Jan 22, 2025
1 parent a9ba5df commit f7b29b7
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/Campaigns/Blocks/DonateButton/block.json
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"
}
84 changes: 84 additions & 0 deletions src/Campaigns/Blocks/DonateButton/edit.tsx
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>
);
}
13 changes: 13 additions & 0 deletions src/Campaigns/Blocks/DonateButton/index.ts
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.
6 changes: 5 additions & 1 deletion src/Campaigns/Blocks/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as campaignTitleBlock from './CampaignTitleBlock';
import * as campaignDonateButton from './DonateButton';

const getAllBlocks = () => {
return [campaignTitleBlock];
return [
campaignTitleBlock,
campaignDonateButton
];
};

getAllBlocks().forEach((block) => {
Expand Down
26 changes: 23 additions & 3 deletions src/Campaigns/Blocks/shared/hooks/useCampaign.ts
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'
}
5 changes: 5 additions & 0 deletions src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function getCampaigns(WP_REST_Request $request): WP_REST_Response
$totalCampaigns = empty($campaigns) ? 0 : Campaign::query()->count();
$totalPages = (int)ceil($totalCampaigns / $perPage);

// todo: remove - temporary solution
$campaigns = array_map(function ($campaign) {
return $campaign->toArray();
}, $campaigns);

$response = rest_ensure_response($campaigns);
$response->header('X-WP-Total', $totalCampaigns);
$response->header('X-WP-TotalPages', $totalPages);
Expand Down

0 comments on commit f7b29b7

Please sign in to comment.