diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 787a657f3b..3b7a7ce371 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -3,6 +3,7 @@ namespace Give\Campaigns\Controllers; use Exception; +use Give\Campaigns\CampaignDonationQuery; use Give\Campaigns\Models\Campaign; use Give\Campaigns\Repositories\CampaignRepository; use Give\Campaigns\ValueObjects\CampaignGoalType; @@ -31,7 +32,11 @@ 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( + array_merge($campaign->toArray(), [ + 'goalProgress' => $campaign->goalProgress(), + ]) + ); } /** diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index 8c71988c28..c269c19843 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -5,6 +5,7 @@ use DateTime; use Exception; use Give\Campaigns\Actions\ConvertQueryDataToCampaign; +use Give\Campaigns\CampaignDonationQuery; use Give\Campaigns\Factories\CampaignFactory; use Give\Campaigns\Repositories\CampaignPageRepository; use Give\Campaigns\Repositories\CampaignRepository; @@ -193,6 +194,12 @@ public function merge(Campaign ...$campaignsToMerge): bool return give(CampaignRepository::class)->mergeCampaigns($this, ...$campaignsToMerge); } + public function goalProgress() + { + $query = new CampaignDonationQuery($this); + return $query->sumIntendedAmount(); + } + /** * @unreleased * diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index d649991071..d6b5e0ad25 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -219,6 +219,11 @@ public function getSchema(): array 'description' => esc_html__('Campaign goal', 'give'), 'errorMessage' => esc_html__('Must be a number', 'give'), ], + 'goalProgress' => [ + 'type' => 'number', + 'readonly' => true, + 'description' => esc_html__('Campaign goal progress', 'give'), + ], 'goalType' => [ 'enum' => [ 'amount', diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx index 997529e9ba..2f569e9378 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx @@ -8,6 +8,7 @@ import HeaderText from './HeaderText'; import HeaderSubText from './HeaderSubText'; import DefaultFormWidget from "./DefaultForm"; import {GiveCampaignDetails} from "@givewp/campaigns/admin/components/CampaignDetailsPage/types"; +import useCampaignEntityRecord from "@givewp/campaigns/admin/components/CampaignDetailsPage/useCampaignEntityRecord"; const campaignId = new URLSearchParams(window.location.search).get('id'); @@ -178,6 +179,9 @@ const RevenueWidget = () => { } const GoalProgressWidget = () => { + + const {campaign} = useCampaignEntityRecord(); + return (
{ }}> {__('Goal Progress')} {__('Show your campaign performance')} - +
) } diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/index.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/index.tsx index 28fddf3006..22ca49b948 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/index.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/index.tsx @@ -17,6 +17,7 @@ import NotificationPlaceholder from '../Notifications'; import cx from 'classnames'; import styles from './CampaignDetailsPage.module.scss'; +import useCampaignEntityRecord from "@givewp/campaigns/admin/components/CampaignDetailsPage/useCampaignEntityRecord"; declare const window: { GiveCampaignDetails: GiveCampaignDetails; @@ -74,16 +75,11 @@ export default function CampaignsDetailsPage({campaignId}) { }, []); const { - record: campaign, + campaign, hasResolved, save, edit, - }: { - record: Campaign; - hasResolved: boolean; - save: () => any; - edit: (data: Campaign) => void; - } = useEntityRecord('givewp', 'campaign', campaignId); + } = useCampaignEntityRecord(); const methods = useForm({ mode: 'onChange', diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/useCampaignEntityRecord.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/useCampaignEntityRecord.tsx new file mode 100644 index 0000000000..8eb37c6d33 --- /dev/null +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/useCampaignEntityRecord.tsx @@ -0,0 +1,24 @@ +import {useEntityRecord} from "@wordpress/core-data"; +import {Campaign} from "@givewp/campaigns/admin/components/types"; + +const urlParams = new URLSearchParams(window.location.search); +const campaignId = urlParams.get('id'); + +/** + * @unreleased + */ +export default () => { + const { + record: campaign, + hasResolved, + save, + edit, + }: { + record: Campaign; + hasResolved: boolean; + save: () => any; + edit: (data: Campaign) => void; + } = useEntityRecord('givewp', 'campaign', campaignId); + + return {campaign, hasResolved, save, edit}; +} diff --git a/src/Campaigns/resources/admin/components/types.ts b/src/Campaigns/resources/admin/components/types.ts index a28052bd24..9dbf3e0291 100644 --- a/src/Campaigns/resources/admin/components/types.ts +++ b/src/Campaigns/resources/admin/components/types.ts @@ -10,6 +10,7 @@ export type Campaign = { secondaryColor: string; goalType: string; goal: number; + goalProgress: number; status: string; startDateTime: { date: string;