| 1 |
import DonationFormErrorBoundary from '@givewp/forms/app/errors/boundaries/DonationFormErrorBoundary'; |
| 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'; |
| 7 |
|
| 8 |
const formTemplates = window.givewp.form.templates; |
| 9 |
|
| 10 |
const HeaderTemplate = withTemplateWrapper(formTemplates.layouts.header); |
| 11 |
const HeaderTitleTemplate = withTemplateWrapper(formTemplates.layouts.headerTitle); |
| 12 |
const HeaderDescriptionTemplate = withTemplateWrapper(formTemplates.layouts.headerDescription); |
| 13 |
const GoalTemplate = withTemplateWrapper(formTemplates.layouts.goal); |
| 14 |
|
| 15 |
const HeaderImageTemplate = withTemplateWrapper(formTemplates.layouts.headerImage); |
| 16 |
|
| 17 |
/** |
| 18 |
* @since 4.14.0.0 Make the goal currency use the base currency always |
| 19 |
* @since 3.0.0 |
| 20 |
*/ |
| 21 |
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 |
|
| 40 |
const formatGoalAmount = useCallback((amount: number) => { |
| 41 |
// Use the base currency for goal amounts, not the selected currency |
| 42 |
return amountFormatter(goalCurrency).format(amount); |
| 43 |
}, [goalCurrency]); |
| 44 |
|
| 45 |
return ( |
| 46 |
<DonationFormErrorBoundary> |
| 47 |
<HeaderTemplate |
| 48 |
isMultiStep={form.design?.isMultiStep} |
| 49 |
HeaderImage={() => |
| 50 |
form.settings?.designSettingsImageUrl && ( |
| 51 |
<HeaderImageTemplate |
| 52 |
url={form.settings?.designSettingsImageUrl} |
| 53 |
alt={form.settings?.designSettingsImageAlt || form.settings?.formTitle} |
| 54 |
color={form.settings?.designSettingsImageColor} |
| 55 |
opacity={form.settings?.designSettingsImageOpacity} |
| 56 |
/> |
| 57 |
) |
| 58 |
} |
| 59 |
Title={() => form.settings?.showHeading && <HeaderTitleTemplate text={form.settings.heading} />} |
| 60 |
Description={() => |
| 61 |
form.settings?.showDescription && <HeaderDescriptionTemplate text={form.settings.description} /> |
| 62 |
} |
| 63 |
Goal={() => |
| 64 |
form.goal?.show && ( |
| 65 |
<GoalTemplate |
| 66 |
currency={goalCurrency} |
| 67 |
type={form.goal.type as GoalType} |
| 68 |
goalLabel={form.goal.label} |
| 69 |
progressPercentage={Math.round((form.goal.currentAmount / form.goal.targetAmount) * 100)} |
| 70 |
currentAmount={form.goal.currentAmount} |
| 71 |
currentAmountFormatted={ |
| 72 |
form.goal.typeIsMoney |
| 73 |
? formatGoalAmount(form.goal.currentAmount) |
| 74 |
: form.goal.currentAmount.toString() |
| 75 |
} |
| 76 |
targetAmount={form.goal.targetAmount} |
| 77 |
targetAmountFormatted={ |
| 78 |
form.goal.typeIsMoney |
| 79 |
? formatGoalAmount(form.goal.targetAmount) |
| 80 |
: form.goal.targetAmount.toString() |
| 81 |
} |
| 82 |
totalRevenue={form.stats.totalRevenue} |
| 83 |
totalRevenueFormatted={formatGoalAmount(form.stats.totalRevenue)} |
| 84 |
totalCountValue={form.stats.totalCountValue} |
| 85 |
totalCountLabel={form.stats.totalCountLabel} |
| 86 |
/> |
| 87 |
) |
| 88 |
} |
| 89 |
/> |
| 90 |
</DonationFormErrorBoundary> |
| 91 |
); |
| 92 |
} |
| 93 |
|