Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Update goal progress with campaign entity #7628

Merged
merged 8 commits into from
Nov 21, 2024
Merged
7 changes: 6 additions & 1 deletion src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
])
);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
5 changes: 5 additions & 0 deletions src/Campaigns/Routes/RegisterCampaignRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public function getSchema(): array
'description' => esc_html__('Campaign goal', 'give'),
'errorMessage' => esc_html__('Must be a number', 'give'),
],
'goalProgress' => [
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
'type' => 'number',
'readonly' => true,
'description' => esc_html__('Campaign goal progress', 'give'),
],
'goalType' => [
'enum' => [
'amount',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -178,6 +179,9 @@ const RevenueWidget = () => {
}

const GoalProgressWidget = () => {

const {campaign} = useCampaignEntityRecord();

return (
<div style={{
flex: 1,
Expand All @@ -187,7 +191,7 @@ const GoalProgressWidget = () => {
}}>
<HeaderText>{__('Goal Progress')}</HeaderText>
<HeaderSubText>{__('Show your campaign performance')}</HeaderSubText>
<GoalProgressChart value={450} goal={2000} />
<GoalProgressChart value={campaign.goalProgress} goal={campaign.goal} />
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Campaign>({
mode: 'onChange',
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
}
1 change: 1 addition & 0 deletions src/Campaigns/resources/admin/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type Campaign = {
secondaryColor: string;
goalType: string;
goal: number;
goalProgress: number;
status: string;
startDateTime: {
date: string;
Expand Down
Loading