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

Fix: Prevent NaN error in Campaign Overview Stat Widget #7698

Open
wants to merge 3 commits into
base: epic/campaigns
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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";
Expand All @@ -21,11 +21,11 @@ declare const window: {
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', {
Expand All @@ -46,7 +46,7 @@ const CampaignStats = () => {
const onDayRangeChange = async (days: number) => {
setDayRange(days)

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

Expand All @@ -56,10 +56,13 @@ const CampaignStats = () => {
<>
<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} />
Expand Down Expand Up @@ -93,9 +96,12 @@ const StatWidget = ({label, values, description, formatter = null}) => {
</header>
<div className={styles.statWidgetAmount}>
<DisplayText>
{formatter?.format(values[0]) ?? values[0]}
{'undefined' !== typeof values[0]
? formatter?.format(values[0]) ?? values[0]
: <span>&nbsp;</span>
}
</DisplayText>
{!! values[1] && (
{!!values[1] && (
<PercentChangePill value={values[0]} comparison={values[1]} />
)}
</div>
Expand Down
Loading