diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index 56339ae55d..3b1767c6e1 100644 --- a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php +++ b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php @@ -3,6 +3,7 @@ namespace Give\Campaigns\Actions; use Give\Campaigns\Models\Campaign; +use Give\DonationForms\Models\DonationForm; use Give\Framework\Support\Facades\Scripts\ScriptAsset; use Give\Helpers\Form\Utils; @@ -33,14 +34,17 @@ public function __invoke() true ); - $defaultForm = $campaign->defaultForm(); - $defaultFormTitle = Utils::isV3Form($defaultForm->id) ? $defaultForm->settings->formTitle : $defaultForm->title; wp_localize_script($handleName, 'GiveCampaignDetails', [ 'adminUrl' => admin_url(), 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, - 'defaultForm' => $defaultFormTitle, + 'donationForms' => array_map(function(DonationForm $form) { + return [ + 'id' => $form->id, + 'title' => $form->hasProperty('settings') ? $form->settings->formTitle : $form->title, + ]; + }, $campaign->forms()->getAll()), ] ); diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 787a657f3b..822f5c5191 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -2,13 +2,14 @@ namespace Give\Campaigns\Controllers; -use Exception; use Give\Campaigns\Models\Campaign; use Give\Campaigns\Repositories\CampaignRepository; use Give\Campaigns\ValueObjects\CampaignGoalType; use Give\Campaigns\ValueObjects\CampaignRoute; use Give\Campaigns\ValueObjects\CampaignStatus; use Give\Campaigns\ValueObjects\CampaignType; +use Give\Campaigns\DataTransferObjects\CampaignDetailsData; +use Exception; use WP_Error; use WP_REST_Request; use WP_REST_Response; @@ -31,7 +32,9 @@ public function getCampaign(WP_REST_Request $request) return new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); } - return new WP_REST_Response($campaign->toArray()); + return new WP_REST_Response( + (new CampaignDetailsData($campaign))->toArray() + ); } /** @@ -139,7 +142,9 @@ public function updateCampaign(WP_REST_Request $request) $campaign->save(); } - return new WP_REST_Response($campaign->toArray()); + return new WP_REST_Response( + (new CampaignDetailsData($campaign))->toArray() + ); } diff --git a/src/Campaigns/DataTransferObjects/CampaignDetailsData.php b/src/Campaigns/DataTransferObjects/CampaignDetailsData.php new file mode 100644 index 0000000000..d823cf8afa --- /dev/null +++ b/src/Campaigns/DataTransferObjects/CampaignDetailsData.php @@ -0,0 +1,38 @@ +campaign = $campaign; + } + + /** + * @unreleased + */ + public function toArray(): array + { + return array_merge( + $this->campaign->toArray(), + [ + 'defaultFormId' => $this->campaign->defaultForm()->id, + ] + ); + } +} diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index 7c56edeaf9..10d11522dc 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -231,7 +231,7 @@ public function getSchema(): array 'description' => esc_html__('Campaign goal type', 'give'), ], 'defaultFormId' => [ - 'type' => 'integer', + 'type' => 'number', 'description' => esc_html__('Default campaign form ID', 'give'), ], ], diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/CampaignDetailsPage.module.scss b/src/Campaigns/resources/admin/components/CampaignDetailsPage/CampaignDetailsPage.module.scss index 7764dff4a6..fc7998a8ec 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/CampaignDetailsPage.module.scss +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/CampaignDetailsPage.module.scss @@ -267,6 +267,7 @@ select[name="campaignId"] { .sectionDescription { font-size: 16px; + line-height: 24px; color: #4b5563; } diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx index 997529e9ba..acdef312dd 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx @@ -7,14 +7,9 @@ import { addQueryArgs } from '@wordpress/url'; import HeaderText from './HeaderText'; import HeaderSubText from './HeaderSubText'; import DefaultFormWidget from "./DefaultForm"; -import {GiveCampaignDetails} from "@givewp/campaigns/admin/components/CampaignDetailsPage/types"; const campaignId = new URLSearchParams(window.location.search).get('id'); -declare const window: { - GiveCampaignDetails: GiveCampaignDetails; -} & Window; - const pluck = (array: any[], property: string) => array.map(element => element[property]) const filterOptions = [ @@ -65,7 +60,7 @@ const CampaignStats = () => { - + diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx index 1b60515073..965e1d1157 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx @@ -1,12 +1,25 @@ import {__} from "@wordpress/i18n"; import HeaderText from './HeaderText'; import HeaderSubText from './HeaderSubText'; -import {CampaignFormOption} from "@givewp/campaigns/admin/components/CampaignDetailsPage/types"; +import {GiveCampaignDetails} from "@givewp/campaigns/admin/components/CampaignDetailsPage/types"; +import {useFormContext} from "react-hook-form"; + +declare const window: { + GiveCampaignDetails: GiveCampaignDetails; +} & Window; /** * @unreleased */ -const DefaultFormWidget = ({defaultForm}: {defaultForm: string}) => { +const DefaultFormWidget = () => { + + const {watch} = useFormContext(); + + const [defaultFormId] = watch(['defaultFormId']); + + const defaultFormTitle = window.GiveCampaignDetails.donationForms + .find(form => Number(form.id) === Number(defaultFormId))?.title; + return (
{ padding: '12px 16px', borderRadius: '4px', }}> - {defaultForm} + {defaultFormTitle}
) diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx index 9d29a72a68..b990938f01 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx @@ -12,6 +12,9 @@ declare const window: { * @unreleased */ export default () => { + + const {donationForms} = window.GiveCampaignDetails + const { register, watch, @@ -136,6 +139,32 @@ export default () => { {errors.goal &&
{`${errors.goal.message}`}
} + + {/* DEFAULT CAMPAIGN FORM SETTINGS */} + { donationForms.length > 1 && ( +
+
+
{__('Default campaign form', 'give')}
+
+ {__('Your campaign page and blocks will collect donations through this form by default. You can change the default form at any time.', 'give')} +
+
+
+
+
{__('Select your default campaign form', 'give')}
+ + + + {errors.defaultForm && +
{`${errors.defaultForm.message}`}
} +
+
+
+ )} + ); }; diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts b/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts index a0e3f24082..4a8d19efa8 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts @@ -4,7 +4,8 @@ export interface GiveCampaignDetails { adminUrl: string; currency: string; isRecurringEnabled: boolean; - defaultForm: string; + defaultFormId: number; + donationForms: { id: number; title: string; }[]; } export type CampaignFormOption = {