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