From 3fef40afca59dd799c042c528ff680317b3c4e8f Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Fri, 21 Jul 2023 15:18:39 -0400 Subject: [PATCH] feature: add currency setting exchange rate fraction digits (#218) Co-authored-by: Jon Waldstein --- .../DonationAmountCurrencySwitcherMessage.tsx | 12 ++++++---- src/DonationForms/resources/types.ts | 1 + .../DonationForm/CurrencySwitcherSetting.php | 23 +++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/DonationForms/resources/registrars/templates/groups/DonationAmount/DonationAmountCurrencySwitcherMessage.tsx b/src/DonationForms/resources/registrars/templates/groups/DonationAmount/DonationAmountCurrencySwitcherMessage.tsx index f56602f1f8..05c852fe7b 100644 --- a/src/DonationForms/resources/registrars/templates/groups/DonationAmount/DonationAmountCurrencySwitcherMessage.tsx +++ b/src/DonationForms/resources/registrars/templates/groups/DonationAmount/DonationAmountCurrencySwitcherMessage.tsx @@ -54,21 +54,25 @@ export default function DonationAmountCurrencySwitcherMessage({ }: DonationAmountCurrencySwitcherMessageProps) { const {useWatch, useCurrencyFormatter} = window.givewp.form.hooks; const currency = useWatch({name: 'currency'}); - const newCurrencyFormatter = useCurrencyFormatter(currency); const baseCurrency = currencySettings.find((setting) => setting.exchangeRate === 0)?.id ?? 'USD'; const baseCurrencyFormatter = useCurrencyFormatter(baseCurrency); - const newCurrencyRate = - currencySettings.find((setting) => setting.id === currency)?.exchangeRate.toFixed(2) ?? '1.00'; + const newCurrencySetting = currencySettings.find((setting) => setting.id === currency); + + const newCurrencyRate = newCurrencySetting?.exchangeRate ?? Number('1.00'); + + const newCurrencyRateFormatter = useCurrencyFormatter(currency, { + minimumFractionDigits: newCurrencySetting.exchangeRateFractionDigits, + }); return ( ); } diff --git a/src/DonationForms/resources/types.ts b/src/DonationForms/resources/types.ts index 59a241f321..2eb36565d5 100644 --- a/src/DonationForms/resources/types.ts +++ b/src/DonationForms/resources/types.ts @@ -23,6 +23,7 @@ export type CurrencySwitcherSetting = { id: string; exchangeRate: number; gateways: string[]; + exchangeRateFractionDigits: number; }; export interface FormData { diff --git a/src/Framework/FieldsAPI/Properties/DonationForm/CurrencySwitcherSetting.php b/src/Framework/FieldsAPI/Properties/DonationForm/CurrencySwitcherSetting.php index dc6b47f02c..a567a58504 100644 --- a/src/Framework/FieldsAPI/Properties/DonationForm/CurrencySwitcherSetting.php +++ b/src/Framework/FieldsAPI/Properties/DonationForm/CurrencySwitcherSetting.php @@ -21,15 +21,24 @@ class CurrencySwitcherSetting implements JsonSerializable * @var string[] */ protected $gateways = []; + /** + * @var int + */ + protected $exchangeRateFractionDigits; /** * @unreleased */ - public function __construct(string $id, float $exchangeRate = 0, array $gateways = []) - { + public function __construct( + string $id, + float $exchangeRate = 0, + array $gateways = [], + int $exchangeRateFractionDigits = 2 + ) { $this->id = $id; $this->exchangeRate = $exchangeRate; $this->gateways = $gateways; + $this->exchangeRateFractionDigits = $exchangeRateFractionDigits; } /** @@ -60,6 +69,16 @@ public function exchangeRate(float $rate): CurrencySwitcherSetting return $this; } + /** + * @unreleased + */ + public function exchangeRateFractionDigits(int $exchangeRateFractionDigits): CurrencySwitcherSetting + { + $this->exchangeRateFractionDigits = $exchangeRateFractionDigits; + + return $this; + } + /** * @unreleased */