Skip to content

Commit

Permalink
feature: add currency setting exchange rate fraction digits (impress-…
Browse files Browse the repository at this point in the history
…org#218)

Co-authored-by: Jon Waldstein <jonwaldstein@jons-air.mynetworksettings.com>
  • Loading branch information
jonwaldstein and Jon Waldstein committed Jul 21, 2023
1 parent d9735ae commit 3fef40a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Expand Up @@ -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 (
<CurrencySwitcherMessage
baseCurrency={baseCurrency}
message={message.replace('1.00', baseCurrencyFormatter.format(1))}
newCurrency={currency}
newCurrencyRate={newCurrencyFormatter.format(Number(newCurrencyRate))}
newCurrencyRate={newCurrencyRateFormatter.format(newCurrencyRate)}
/>
);
}
1 change: 1 addition & 0 deletions src/DonationForms/resources/types.ts
Expand Up @@ -23,6 +23,7 @@ export type CurrencySwitcherSetting = {
id: string;
exchangeRate: number;
gateways: string[];
exchangeRateFractionDigits: number;
};

export interface FormData {
Expand Down
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 3fef40a

Please sign in to comment.