Header.tsx
65 lines
| 1 | import {withTemplateWrapper} from '../templates'; |
| 2 | import getWindowData from '../utilities/getWindowData'; |
| 3 | import type {GoalType} from '@givewp/forms/propTypes'; |
| 4 | import amountFormatter from '@givewp/forms/app/utilities/amountFormatter'; |
| 5 | import DonationFormErrorBoundary from '@givewp/forms/app/errors/boundaries/DonationFormErrorBoundary'; |
| 6 | |
| 7 | const {form} = getWindowData(); |
| 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 | /** |
| 16 | * @since 3.0.0 |
| 17 | */ |
| 18 | const formatGoalAmount = (amount: number) => { |
| 19 | return amountFormatter(form.currency, { |
| 20 | maximumFractionDigits: 0, |
| 21 | }).format(amount); |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * @since 3.0.0 |
| 26 | */ |
| 27 | export default function Header() { |
| 28 | return ( |
| 29 | <DonationFormErrorBoundary> |
| 30 | <HeaderTemplate |
| 31 | Title={() => form.settings?.showHeading && <HeaderTitleTemplate text={form.settings.heading} />} |
| 32 | Description={() => |
| 33 | form.settings?.showDescription && <HeaderDescriptionTemplate text={form.settings.description} /> |
| 34 | } |
| 35 | Goal={() => |
| 36 | form.goal?.show && ( |
| 37 | <GoalTemplate |
| 38 | currency={form.currency} |
| 39 | type={form.goal.type as GoalType} |
| 40 | goalLabel={form.goal.label} |
| 41 | progressPercentage={form.goal.progressPercentage} |
| 42 | currentAmount={form.goal.currentAmount} |
| 43 | currentAmountFormatted={ |
| 44 | form.goal.typeIsMoney |
| 45 | ? formatGoalAmount(form.goal.currentAmount) |
| 46 | : form.goal.currentAmount.toString() |
| 47 | } |
| 48 | targetAmount={form.goal.targetAmount} |
| 49 | targetAmountFormatted={ |
| 50 | form.goal.typeIsMoney |
| 51 | ? formatGoalAmount(form.goal.targetAmount) |
| 52 | : form.goal.targetAmount.toString() |
| 53 | } |
| 54 | totalRevenue={form.stats.totalRevenue} |
| 55 | totalRevenueFormatted={formatGoalAmount(form.stats.totalRevenue)} |
| 56 | totalCountValue={form.stats.totalCountValue} |
| 57 | totalCountLabel={form.stats.totalCountLabel} |
| 58 | /> |
| 59 | ) |
| 60 | } |
| 61 | /> |
| 62 | </DonationFormErrorBoundary> |
| 63 | ); |
| 64 | } |
| 65 |