render both string and JSON amounts correctly to HTML

This commit is contained in:
Florian Dold 2018-02-20 09:33:17 +01:00
parent 774a79d9de
commit 65228afb87
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -48,12 +48,21 @@ import * as React from "react";
* Render amount as HTML, which non-breaking space between * Render amount as HTML, which non-breaking space between
* decimal value and currency. * decimal value and currency.
*/ */
export function renderAmount(amount: AmountJson) { export function renderAmount(amount: AmountJson | string) {
const x = amount.value + amount.fraction / Amounts.fractionalBase; let a;
return <span>{x}&nbsp;{amount.currency}</span>; if (typeof amount === "string") {
a = Amounts.parse(amount);
} else {
a = amount;
}
if (!a) {
return <span>(invalid amount)</span>;
}
const x = a.value + a.fraction / Amounts.fractionalBase;
return <span>{x}&nbsp;{a.currency}</span>;
} }
export const AmountDisplay = ({amount}: {amount: AmountJson}) => renderAmount(amount); export const AmountDisplay = ({amount}: {amount: AmountJson | string}) => renderAmount(amount);
/** /**