Skip to content

Commit b32afe8

Browse files
authored
Fix: currency switcher inconsistencies (#8195)
1 parent 518356f commit b32afe8

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/DonationForms/resources/app/form/Header.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {useCallback} from 'react';
2-
import {withTemplateWrapper} from '../templates';
3-
import type {GoalType} from '@givewp/forms/propTypes';
4-
import amountFormatter from '@givewp/forms/app/utilities/amountFormatter';
51
import DonationFormErrorBoundary from '@givewp/forms/app/errors/boundaries/DonationFormErrorBoundary';
6-
import type {Form as DonationForm} from '@givewp/forms/types';
2+
import amountFormatter from '@givewp/forms/app/utilities/amountFormatter';
3+
import type { GoalType } from '@givewp/forms/propTypes';
4+
import type { Form as DonationForm } from '@givewp/forms/types';
5+
import { useCallback, useMemo } from 'react';
6+
import { withTemplateWrapper } from '../templates';
77

88
const formTemplates = window.givewp.form.templates;
99

@@ -15,12 +15,32 @@ const GoalTemplate = withTemplateWrapper(formTemplates.layouts.goal);
1515
const HeaderImageTemplate = withTemplateWrapper(formTemplates.layouts.headerImage);
1616

1717
/**
18+
* @unreleased Make the goal currency use the base currency always
1819
* @since 3.0.0
1920
*/
2021
export default function Header({form}: {form: DonationForm}) {
22+
23+
// Get the base currency for goal formatting
24+
// The goal progress bar should always use the form's base currency, not the selected currency
25+
// This matches the behavior of legacy forms where only donation amounts are converted
26+
const goalCurrency = useMemo(() => {
27+
// If currency switcher is enabled, find the base currency (exchangeRate === 0)
28+
if (form.currencySwitcherSettings && form.currencySwitcherSettings.length > 0) {
29+
const baseCurrencySetting = form.currencySwitcherSettings.find(
30+
(setting) => setting.exchangeRate === 0
31+
);
32+
if (baseCurrencySetting) {
33+
return baseCurrencySetting.id;
34+
}
35+
}
36+
// Fallback to form's default currency
37+
return form.currency;
38+
}, [form.currencySwitcherSettings, form.currency]);
39+
2140
const formatGoalAmount = useCallback((amount: number) => {
22-
return amountFormatter(form.currency).format(amount);
23-
}, []);
41+
// Use the base currency for goal amounts, not the selected currency
42+
return amountFormatter(goalCurrency).format(amount);
43+
}, [goalCurrency]);
2444

2545
return (
2646
<DonationFormErrorBoundary>
@@ -43,7 +63,7 @@ export default function Header({form}: {form: DonationForm}) {
4363
Goal={() =>
4464
form.goal?.show && (
4565
<GoalTemplate
46-
currency={form.currency}
66+
currency={goalCurrency}
4767
type={form.goal.type as GoalType}
4868
goalLabel={form.goal.label}
4969
progressPercentage={Math.round((form.goal.currentAmount / form.goal.targetAmount) * 100)}

src/FeatureFlags/OptionBasedFormEditor/Settings/General.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class General extends AbstractOptionBasedFormEditorSettings
99
{
1010
/**
11+
* @unreleased Remove auto_format_currency option from the list of disabled options
1112
* @since 3.18.0
1213
*/
1314
public function getDisabledOptionIds(): array
@@ -16,7 +17,6 @@ public function getDisabledOptionIds(): array
1617
// General Section
1718
'override_legacy_donation_management_pages',
1819
// Currency Section
19-
'auto_format_currency',
2020
'currency_position',
2121
'thousands_separator',
2222
'decimal_separator',

0 commit comments

Comments
 (0)