From bb51c5308b3ce4898226ac329e312e36d6a014ca Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Thu, 7 Nov 2024 14:14:46 -0500 Subject: [PATCH 1/3] feature: Add campaign setting for default form --- .../Actions/LoadCampaignDetailsAssets.php | 7 +++++ .../CampaignDetailsPage.module.scss | 1 + .../CampaignDetailsPage/Tabs/Settings.tsx | 31 +++++++++++++++++++ .../components/CampaignDetailsPage/types.ts | 1 + 4 files changed, 40 insertions(+) diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index 79df96bdc5..dadc2428bf 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; /** @@ -39,6 +40,12 @@ public function __invoke() 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, 'defaultForm' => $defaultForm ? $defaultForm->settings->formTitle : null, + 'donationForms' => array_map(function(DonationForm $form) { + return [ + 'id' => $form->id, + 'title' => $form->settings->formTitle, + ]; + }, $campaign->forms()->getAll()), ] ); 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/Tabs/Settings.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx index 9d29a72a68..72b727815c 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx @@ -136,6 +136,37 @@ export default () => { {errors.goal &&
{`${errors.goal.message}`}
} + + {/* DEFAULT CAMPAIGN FORM SETTINGS */} + { window.GiveCampaignDetails.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..56581bc891 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts @@ -5,6 +5,7 @@ export interface GiveCampaignDetails { currency: string; isRecurringEnabled: boolean; defaultForm: string; + donationForms: { id: number; title: string; }[]; } export type CampaignFormOption = { From 3f0e276e59c5cf6e1560c46f849c5b1a0ee6704a Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Fri, 8 Nov 2024 11:17:10 -0500 Subject: [PATCH 2/3] refactor: Update default form state --- .../Actions/LoadCampaignDetailsAssets.php | 5 +-- .../Controllers/CampaignRequestController.php | 15 +++++++- .../CampaignDetailsData.php | 38 +++++++++++++++++++ .../Routes/RegisterCampaignRoutes.php | 4 ++ .../Components/CampaignStats.tsx | 7 +--- .../Components/DefaultForm.tsx | 19 ++++++++-- .../CampaignDetailsPage/Tabs/Settings.tsx | 6 +-- .../components/CampaignDetailsPage/types.ts | 2 +- 8 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 src/Campaigns/DataTransferObjects/CampaignDetailsData.php diff --git a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php index dadc2428bf..df2e509c49 100644 --- a/src/Campaigns/Actions/LoadCampaignDetailsAssets.php +++ b/src/Campaigns/Actions/LoadCampaignDetailsAssets.php @@ -33,17 +33,16 @@ public function __invoke() true ); - $defaultForm = $campaign->defaultForm(); wp_localize_script($handleName, 'GiveCampaignDetails', [ 'adminUrl' => admin_url(), 'currency' => give_get_currency(), 'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null, - 'defaultForm' => $defaultForm ? $defaultForm->settings->formTitle : null, + 'defaultFormId' => $campaign->defaultForm()->id, 'donationForms' => array_map(function(DonationForm $form) { return [ 'id' => $form->id, - 'title' => $form->settings->formTitle, + '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 6820800e53..53a5bb9147 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -2,6 +2,7 @@ namespace Give\Campaigns\Controllers; +use Give\Campaigns\DataTransferObjects\CampaignDetailsData; use Give\Campaigns\Models\Campaign; use Give\Campaigns\Repositories\CampaignRepository; use Give\Campaigns\ValueObjects\CampaignGoalType; @@ -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() + ); } /** @@ -124,6 +127,12 @@ public function updateCampaign(WP_REST_Request $request) case 'goalType': $campaign->goalType = new CampaignGoalType($value); break; + case 'defaultForm': + give(CampaignRepository::class)->updateDefaultCampaignForm( + $campaign, + $request->get_param('defaultForm') + ); + break; default: if ($campaign->hasProperty($key)) { $campaign->$key = $value; @@ -135,7 +144,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..b2f6251b68 --- /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(), + [ + 'defaultForm' => $this->campaign->defaultForm()->id, + ] + ); + } +} diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index 8f4739fc08..5dcf98546a 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -230,6 +230,10 @@ public function getSchema(): array ], 'description' => esc_html__('Campaign goal type', 'give'), ], + 'defaultForm' => [ + 'type' => 'number', + 'description' => esc_html__('Default donation form ID', 'give'), + ], ], 'required' => ['id', 'title', 'goal', 'goalType'], 'allOf' => [ 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..82cb58055a 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 [defaultForm] = watch(['defaultForm']); + + const defaultFormTitle = window.GiveCampaignDetails.donationForms + .find(form => Number(form.id) === Number(defaultForm))?.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 72b727815c..82a87eb2c9 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx @@ -19,7 +19,7 @@ export default () => { formState: {errors}, } = useFormContext(); - const [goalType, image, status, shortDescription] = watch(['goalType', 'image', 'status', 'shortDescription']); + const [goalType, image, status, shortDescription, defaultForm] = watch(['goalType', 'image', 'status', 'shortDescription', 'defaultForm']); const isDisabled = status === 'archived'; return ( @@ -151,10 +151,10 @@ export default () => {
{__('Select your default campaign form', 'give')}
- {window.GiveCampaignDetails.donationForms && ( <> - {window.GiveCampaignDetails.donationForms.map(({id, title}) => )} )} diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts b/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts index 56581bc891..4a8d19efa8 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/types.ts @@ -4,7 +4,7 @@ export interface GiveCampaignDetails { adminUrl: string; currency: string; isRecurringEnabled: boolean; - defaultForm: string; + defaultFormId: number; donationForms: { id: number; title: string; }[]; } From f50c964fad57fc5072741b29841364711f5faa0a Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Fri, 8 Nov 2024 13:24:34 -0500 Subject: [PATCH 3/3] wip --- .../CampaignDetailsPage/Components/DefaultForm.tsx | 2 +- .../components/CampaignDetailsPage/Tabs/Settings.tsx | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx index ff71f98685..965e1d1157 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/DefaultForm.tsx @@ -13,7 +13,7 @@ declare const window: { */ const DefaultFormWidget = () => { - const {watch,} = useFormContext(); + const {watch} = useFormContext(); const [defaultFormId] = watch(['defaultFormId']); diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Tabs/Settings.tsx index 4ae85bfbcc..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, @@ -138,7 +141,7 @@ export default () => { {/* DEFAULT CAMPAIGN FORM SETTINGS */} - { window.GiveCampaignDetails.donationForms.length > 1 && ( + { donationForms.length > 1 && (
{__('Default campaign form', 'give')}
@@ -152,12 +155,7 @@ export default () => { className={styles.sectionSubtitle}>{__('Select your default campaign form', 'give')}
{errors.defaultForm &&