Header.tsx
75 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 | import type {Form as DonationForm} from '@givewp/forms/types'; |
| 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 3.0.0 |
| 19 | */ |
| 20 | export default function Header({form}: {form: DonationForm}) { |
| 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 | isMultiStep={form.design.isMultiStep} |
| 31 | HeaderImage={() => |
| 32 | form.settings?.designSettingsImageUrl && ( |
| 33 | <HeaderImageTemplate |
| 34 | url={form.settings?.designSettingsImageUrl} |
| 35 | alt={form.settings?.designSettingsImageAlt || form.settings?.formTitle} |
| 36 | color={form.settings?.designSettingsImageColor} |
| 37 | opacity={form.settings?.designSettingsImageOpacity} |
| 38 | /> |
| 39 | ) |
| 40 | } |
| 41 | Title={() => form.settings?.showHeading && <HeaderTitleTemplate text={form.settings.heading} />} |
| 42 | Description={() => |
| 43 | form.settings?.showDescription && <HeaderDescriptionTemplate text={form.settings.description} /> |
| 44 | } |
| 45 | Goal={() => |
| 46 | form.goal?.show && ( |
| 47 | <GoalTemplate |
| 48 | currency={form.currency} |
| 49 | type={form.goal.type as GoalType} |
| 50 | goalLabel={form.goal.label} |
| 51 | progressPercentage={Math.round((form.goal.currentAmount / form.goal.targetAmount) * 100)} |
| 52 | currentAmount={form.goal.currentAmount} |
| 53 | currentAmountFormatted={ |
| 54 | form.goal.typeIsMoney |
| 55 | ? formatGoalAmount(form.goal.currentAmount) |
| 56 | : form.goal.currentAmount.toString() |
| 57 | } |
| 58 | targetAmount={form.goal.targetAmount} |
| 59 | targetAmountFormatted={ |
| 60 | form.goal.typeIsMoney |
| 61 | ? formatGoalAmount(form.goal.targetAmount) |
| 62 | : form.goal.targetAmount.toString() |
| 63 | } |
| 64 | totalRevenue={form.stats.totalRevenue} |
| 65 | totalRevenueFormatted={formatGoalAmount(form.stats.totalRevenue)} |
| 66 | totalCountValue={form.stats.totalCountValue} |
| 67 | totalCountLabel={form.stats.totalCountLabel} |
| 68 | /> |
| 69 | ) |
| 70 | } |
| 71 | /> |
| 72 | </DonationFormErrorBoundary> |
| 73 | ); |
| 74 | } |
| 75 |