commit 77c3574fe5037c42c2351cedd4206f1b7be21cf7
parent cfd4303db0a908fca7cb2540e58fa75e36a7d4d2
Author: ms <ms@taler.net>
Date: Thu, 10 Feb 2022 17:28:40 +0100
extracting currency from balance
Diffstat:
1 file changed, 37 insertions(+), 7 deletions(-)
diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx
@@ -1,6 +1,6 @@
import useSWR, { SWRConfig, useSWRConfig } from "swr";
-import { h, Fragment, ComponentChildren, VNode } from "preact";
-import { useState, useEffect, StateUpdater } from "preact/hooks";
+import { h, Fragment, ComponentChildren, VNode, createContext } from "preact";
+import { useState, useEffect, StateUpdater, useContext } from "preact/hooks";
import { Buffer } from "buffer";
import { useTranslator, Translate } from "../../i18n";
import { QR } from "../../components/QR";
@@ -24,6 +24,11 @@ import "../../scss/main.scss";
* - Many strings need to be i18n-wrapped.
*/
+/************
+ * Contexts *
+ ***********/
+var CurrencyContext = createContext(null);
+
/**********************************************
* Type definitions for states and API calls. *
*********************************************/
@@ -53,6 +58,11 @@ interface CredentialsRequestType {
password: string;
}
+interface Amount {
+ value: string;
+ currency: string;
+}
+
/**
* Track page state.
*/
@@ -82,10 +92,27 @@ interface AccountStateType {
}
/************
+ * Contexts *
+ ***********/
+const currencyContext = createContext<string>()
+
+
+/************
* Helpers. *
***********/
/**
+ * Parse amount.
+ */
+function parseAmount(val: string): Amount {
+ const format = /^[A-Z]+:[0-9]+(\.[0-9]+)?$/;
+ if (!format.test(val))
+ throw Error("Backend gave invalid amount", val)
+ const amountSplit = val.split(":");
+ return {value: amountSplit[1], currency: amountSplit[0]}
+}
+
+/**
* Get username from the backend state, and throw
* exception if not found.
*/
@@ -624,6 +651,7 @@ async function registrationCall(
*/
function TalerWithdrawal(Props: any): VNode {
const {backendState, pageStateSetter} = Props;
+ const currency = useContext(CurrencyContext);
const i18n = useTranslator();
const amountRegex = "^[0-9]+(\.[0-9]+)?$";
var submitAmount = ""; // without currency.
@@ -638,13 +666,14 @@ function TalerWithdrawal(Props: any): VNode {
}
console.log("Valid amount", submitAmount);
createWithdrawalCall(
- `EUR:${submitAmount}`, // FIXME: take currency from the balance.
+ `${currency}:${submitAmount}`,
backendState,
pageStateSetter
)}}>{i18n`Charge Taler wallet`}
</button>;
return <Fragment>
+ <label>{currency}</label>
<input
type="text"
placeholder="amount"
@@ -653,7 +682,6 @@ function TalerWithdrawal(Props: any): VNode {
onInput={(e): void => {
submitAmount = e.currentTarget.value
}} />
- <label>FIXME: currency here!</label>
{ submitButton }
</Fragment>
}
@@ -858,14 +886,16 @@ function Account(Props: any): VNode {
{Props.children}
</Fragment>);
}
-
+ const balance = parseAmount(data.balance.amount)
return (<Fragment>
- <p>Your balance is {data.balance.amount}.</p>
+ <p>Your balance is {`${balance.value} ${balance.currency}`}.</p>
<div>
<span>{i18n`Last transactions:`}</span> { txsPages }
<button onClick={() => setTxPageNumber(txPageNumber + 1)}>{i18n`Load more transactions`}</button>
</div>
- {Props.children}
+ <CurrencyContext.Provider value={balance.currency}>
+ {Props.children}
+ </CurrencyContext.Provider>
</Fragment>);
}