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

Feature: add new routes for donations and donors #7699

Open
wants to merge 43 commits into
base: epic/campaigns
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e22047b
feature: add get routes for donations
glaubersilva Jan 28, 2025
a2d2695
tests: add test for donations pagination
glaubersilva Jan 28, 2025
74af706
tests: add more asserts
glaubersilva Jan 28, 2025
ebd4d57
tests: add GetDonationRouteTest class
glaubersilva Jan 28, 2025
6996169
feature: add get endpoints for donors
glaubersilva Jan 29, 2025
aaecb8a
tests: add GetDonorRouteTest class
glaubersilva Jan 29, 2025
7505388
tests: add testGetDonorsWithPagination method
glaubersilva Jan 29, 2025
0850304
refactor: change permissions callback
glaubersilva Jan 29, 2025
df1697b
refactor: tweaks and polishments
glaubersilva Jan 29, 2025
a1900dd
tests: tweak property comments
glaubersilva Jan 29, 2025
5a1bb94
fix: wrong rest_url path
glaubersilva Jan 29, 2025
bfe2a6f
refactor: restrict access to sensitive data
glaubersilva Jan 30, 2025
4dcb2d2
refactor: uncomment code
glaubersilva Jan 30, 2025
7a086f5
refactor: add where conditions for donation mode and status
glaubersilva Jan 30, 2025
4fca5af
refactor: use query method
glaubersilva Jan 30, 2025
0aa85a1
refactor: remove unnecessary distinct clause
glaubersilva Jan 30, 2025
3ada42b
feature: add hideAnonymousDonations parameter
glaubersilva Jan 30, 2025
8ad5a7d
refactor: remove unnecessary select
glaubersilva Jan 30, 2025
205d1a1
refactor: improve query readability and consider status and payment mode
glaubersilva Jan 30, 2025
811a6a2
feature: add hideAnonymousDonors parameter
glaubersilva Jan 30, 2025
9a8a324
fix: broken unit tests
glaubersilva Jan 30, 2025
7e83d0d
feature: add onlyWithDonations param
glaubersilva Jan 30, 2025
e57c51e
doc: add comment
glaubersilva Jan 30, 2025
4ae26ea
refactor: remove unnecessary distinct
glaubersilva Jan 30, 2025
c64e98c
refactor: add sort and direction parameters
glaubersilva Jan 30, 2025
7aa9903
tests: add testGetDonorsSortedByTotalAmountDonated method
glaubersilva Jan 31, 2025
7fee060
tests: add sortableColumnsDataProvider and refactor test to use it
glaubersilva Jan 31, 2025
e0c0cc3
tests: simplify logic
glaubersilva Jan 31, 2025
b54e0b4
tests: add testGetDonorsShouldReturnSensitiveData method
glaubersilva Jan 31, 2025
783655b
tests: add new tests for the onlyWithDonations param
glaubersilva Jan 31, 2025
e3eb167
tests: add testGetDonationsShouldReturnSensitiveData method
glaubersilva Jan 31, 2025
bf7214a
refactor: simplify query
glaubersilva Jan 31, 2025
563a726
tests: add testGetDonationsSortedByColumns
glaubersilva Jan 31, 2025
ca5c7a7
refactor: replace validate_callback with enum
glaubersilva Jan 31, 2025
9fd33ff
refactor: change default sort direction
glaubersilva Jan 31, 2025
b508a7f
tests: change helper method names
glaubersilva Jan 31, 2025
35dc9ca
refactor: remove distinct
glaubersilva Jan 31, 2025
ed34c89
refactor: add mode parameter
glaubersilva Feb 7, 2025
ded974c
refactor: replace hideAnonymousDonations with includeAnonymousDonations
glaubersilva Feb 7, 2025
b241027
tests: rename helper methods
glaubersilva Feb 7, 2025
74d8d6e
refactor: change the routes namespace for donors, donations, and camp…
glaubersilva Feb 14, 2025
7def493
refactor prevent access to "anonymous data"
glaubersilva Feb 14, 2025
238fd0a
tests: add new methods to check anonymous data return
glaubersilva Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Campaigns/Actions/LoadCampaignsListTableAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Give\Campaigns\Actions;

use Give\Campaigns\ListTable\CampaignsListTable;
use Give\Campaigns\ValueObjects\CampaignRoute;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;

/**
Expand All @@ -28,7 +29,7 @@ public function __invoke()

wp_localize_script($handleName, 'GiveCampaignsListTable',
[
'apiRoot' => esc_url_raw(rest_url('give-api/v2/campaigns/list-table')),
'apiRoot' => esc_url_raw(rest_url(CampaignRoute::NAMESPACE . '/campaigns/list-table')),
'apiNonce' => wp_create_nonce('wp_rest'),
'table' => give(CampaignsListTable::class)->toArray(),
'adminUrl' => admin_url(),
Expand Down
3 changes: 2 additions & 1 deletion src/Campaigns/Routes/DeleteCampaignListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Give\API\RestRoute;
use Give\Campaigns\ListTable\CampaignsListTable;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\Campaigns\ValueObjects\CampaignRoute;
use Give\Framework\Exceptions\Primitives\Exception;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -37,7 +38,7 @@ class DeleteCampaignListTable implements RestRoute
public function registerRoute()
{
register_rest_route(
'give-api/v2',
CampaignRoute::NAMESPACE,
$this->endpoint,
[
[
Expand Down
3 changes: 2 additions & 1 deletion src/Campaigns/Routes/GetCampaignsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Give\Campaigns\ListTable\CampaignsListTable;
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\Campaigns\ValueObjects\CampaignRoute;
use Give\Framework\QueryBuilder\QueryBuilder;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -38,7 +39,7 @@ class GetCampaignsListTable implements RestRoute
public function registerRoute(): void
{
register_rest_route(
'give-api/v2',
CampaignRoute::NAMESPACE,
$this->endpoint,
[
[
Expand Down
2 changes: 1 addition & 1 deletion src/Campaigns/ValueObjects/CampaignRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class CampaignRoute extends Enum
{
const NAMESPACE = 'give-api/v2';
const NAMESPACE = 'givewp/v3';
const CAMPAIGN = 'campaigns/(?P<id>[0-9]+)';
const CAMPAIGNS = 'campaigns';
}
Original file line number Diff line number Diff line change
@@ -1,89 +1,94 @@
import {__} from '@wordpress/i18n';
import {useEffect, useState} from "react";
import RevenueChart from "../RevenueChart";
import GoalProgressChart from "../GoalProgressChart";
import {useEffect, useState} from 'react';
import RevenueChart from '../RevenueChart';
import GoalProgressChart from '../GoalProgressChart';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
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";
import DefaultFormWidget from '../DefaultForm';
import {GiveCampaignDetails} from '@givewp/campaigns/admin/components/CampaignDetailsPage/types';
import {useCampaignEntityRecord} from '@givewp/campaigns/utils';

import styles from "./styles.module.scss"
import styles from './styles.module.scss';

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 pluck = (array: any[], property: string) => array.map((element) => element[property]);

const filterOptions = [
{ label: __('Today', 'give'), value: 1, description: __('from today', 'give') },
{ label: __('Last 7 days', 'give'), value: 7, description: __('from the last 7 days', 'give') },
{ label: __('Last 30 days', 'give'), value: 30, description: __('from the last 30 days', 'give') },
{ label: __('Last 90 days', 'give'), value: 90, description: __('from the last 90 days', 'give') },
{ label: __('All-time', 'give'), value: 0, description: __('total for all-time', 'give') },
]
{label: __('Today', 'give'), value: 1, description: __('from today', 'give')},
{label: __('Last 7 days', 'give'), value: 7, description: __('from the last 7 days', 'give')},
{label: __('Last 30 days', 'give'), value: 30, description: __('from the last 30 days', 'give')},
{label: __('Last 90 days', 'give'), value: 90, description: __('from the last 90 days', 'give')},
{label: __('All-time', 'give'), value: 0, description: __('total for all-time', 'give')},
];

const currency = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
})
});

const CampaignStats = () => {

const [dayRange, setDayRange] = useState(null);
const [stats, setStats] = useState([]);
const {campaign} = useCampaignEntityRecord();

useEffect(() => {
onDayRangeChange(0)
}, [])
onDayRangeChange(0);
}, []);

const onDayRangeChange = async (days: number) => {
setDayRange(days)
setDayRange(days);

apiFetch({path: addQueryArgs( '/give-api/v2/campaigns/' + campaignId +'/statistics', {rangeInDays: days} ) } )
.then(setStats);
}
apiFetch({path: addQueryArgs('/givewp/v3/campaigns/' + campaignId + '/statistics', {rangeInDays: days})}).then(
setStats
);
};

const widgetDescription = filterOptions.find(option => option.value === dayRange)?.description
const widgetDescription = filterOptions.find((option) => option.value === dayRange)?.description;

return (
<>
<DateRangeFilters selected={dayRange} options={filterOptions} onSelect={onDayRangeChange} />
<div className={styles.mainGrid}>
<StatWidget label={__('Amount raised', 'give')} values={pluck(stats, 'amountRaised')} description={widgetDescription} formatter={currency} />
<StatWidget label={__('Number of donations', 'give')} values={pluck(stats, 'donationCount')} description={widgetDescription} />
<StatWidget label={__('Number of donors', 'give')} values={pluck(stats, 'donorCount')} description={widgetDescription} />
<RevenueWidget />
<StatWidget
label={__('Amount raised', 'give')}
values={pluck(stats, 'amountRaised')}
description={widgetDescription}
formatter={currency}
/>
<StatWidget
label={__('Number of donations', 'give')}
values={pluck(stats, 'donationCount')}
description={widgetDescription}
/>
<StatWidget
label={__('Number of donors', 'give')}
values={pluck(stats, 'donorCount')}
description={widgetDescription}
/>
<RevenueWidget />
<div className={styles.nestedGrid}>
<GoalProgressWidget />
<DefaultFormWidget defaultForm={campaign.defaultFormTitle} />
</div>
</div>
</>
)
}
);
};

const FooterText = ({children}) => {
return (
<div className={styles.footerText}>
{children}
</div>
)
}
return <div className={styles.footerText}>{children}</div>;
};

const DisplayText = ({children}) => {
return (
<div className={styles.statWidgetDisplay}>
{children}
</div>
)
}
return <div className={styles.statWidgetDisplay}>{children}</div>;
};

const StatWidget = ({label, values, description, formatter = null}) => {
return (
Expand All @@ -92,41 +97,32 @@ const StatWidget = ({label, values, description, formatter = null}) => {
<HeaderText>{label}</HeaderText>
</header>
<div className={styles.statWidgetAmount}>
<DisplayText>
{formatter?.format(values[0]) ?? values[0]}
</DisplayText>
{!! values[1] && (
<PercentChangePill value={values[0]} comparison={values[1]} />
)}
<DisplayText>{formatter?.format(values[0]) ?? values[0]}</DisplayText>
{!!values[1] && <PercentChangePill value={values[0]} comparison={values[1]} />}
</div>
<footer>
<FooterText>{description}</FooterText>
</footer>
</div>
)
}
);
};

const PercentChangePill = ({value, comparison}) => {
const change = Math.round(100 * ((value - comparison) / comparison)) ?? 0;

const change = Math.round(100 * ((value - comparison) / comparison)) ?? 0

const [color, backgroundColor, symbol] = change == 0
? ['#060c1a', '#f2f2f2', '⯈']
: change > 0
const [color, backgroundColor, symbol] =
change == 0
? ['#060c1a', '#f2f2f2', '⯈']
: change > 0
? ['#2d802f', '#f2fff3', '⯅']
: ['#e35f45', '#fff4f2', '⯆']
: ['#e35f45', '#fff4f2', '⯆'];

return (
<span
className={styles.percentChangePill}
style={{backgroundColor: backgroundColor, color: color,}}
>
<span className={styles.percentChangePill} style={{backgroundColor: backgroundColor, color: color}}>
<small>{symbol}</small> {Math.abs(change)}%
</span>
)

}

);
};

const RevenueWidget = () => {
return (
Expand All @@ -138,10 +134,9 @@ const RevenueWidget = () => {
<RevenueChart />
</div>
);
}
};

const GoalProgressWidget = () => {

const {campaign} = useCampaignEntityRecord();

return (
Expand All @@ -152,8 +147,8 @@ const GoalProgressWidget = () => {
</header>
<GoalProgressChart value={campaign.goalProgress} goal={campaign.goal} />
</div>
)
}
);
};

const DateRangeFilters = ({options, onSelect, selected}) => {
return (
Expand All @@ -168,8 +163,7 @@ const DateRangeFilters = ({options, onSelect, selected}) => {
</button>
))}
</div>
)
}

);
};

export default CampaignStats;
Loading