summaryrefslogtreecommitdiff
path: root/packages/bank-ui/src/pages/regional/CreateCashout.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/bank-ui/src/pages/regional/CreateCashout.tsx')
-rw-r--r--packages/bank-ui/src/pages/regional/CreateCashout.tsx76
1 files changed, 48 insertions, 28 deletions
diff --git a/packages/bank-ui/src/pages/regional/CreateCashout.tsx b/packages/bank-ui/src/pages/regional/CreateCashout.tsx
index 393240dee..c51b96b8b 100644
--- a/packages/bank-ui/src/pages/regional/CreateCashout.tsx
+++ b/packages/bank-ui/src/pages/regional/CreateCashout.tsx
@@ -59,6 +59,7 @@ interface Props {
account: string;
focus?: boolean;
onAuthorizationRequired: () => void;
+ onCashout: () => void;
routeClose: RouteDefinition;
routeHere: RouteDefinition;
}
@@ -76,6 +77,7 @@ type ErrorFrom<T> = {
export function CreateCashout({
account: accountName,
onAuthorizationRequired,
+ onCashout,
focus,
routeHere,
routeClose,
@@ -90,7 +92,10 @@ export function CreateCashout({
const creds = credentials.status !== "loggedIn" ? undefined : credentials;
const [, updateBankState] = useBankState();
- const { lib: { bank: api }, config, hints } = useBankCoreApiContext();
+ const {
+ lib: { bank: api },
+ config,
+ } = useBankCoreApiContext();
const [form, setForm] = useState<Partial<FormType>>({ isDebit: true });
const [notification, notify, handleError] = useLocalNotification();
const info = useConversionInfo();
@@ -163,13 +168,6 @@ export function CreateCashout({
);
}
- const account = {
- balance: Amounts.parseOrThrow(resultAccount.body.balance.amount),
- balanceIsDebit:
- resultAccount.body.balance.credit_debit_indicator == "debit",
- debitThreshold: Amounts.parseOrThrow(resultAccount.body.debit_threshold),
- };
-
const {
fiat_currency,
regional_currency,
@@ -178,6 +176,15 @@ export function CreateCashout({
} = info.body;
const regionalZero = Amounts.zeroOfCurrency(regional_currency);
const fiatZero = Amounts.zeroOfCurrency(fiat_currency);
+
+ const account = {
+ balance: Amounts.parseOrThrow(resultAccount.body.balance.amount),
+ balanceIsDebit:
+ resultAccount.body.balance.credit_debit_indicator == "debit",
+ debitThreshold: Amounts.parseOrThrow(resultAccount.body.debit_threshold),
+ minCashout: resultAccount.body.min_cashout === undefined ? regionalZero : Amounts.parseOrThrow(resultAccount.body.min_cashout)
+ };
+
const limit = account.balanceIsDebit
? Amounts.sub(account.debitThreshold, account.balance).amount
: Amounts.add(account.balance, account.debitThreshold).amount;
@@ -196,7 +203,8 @@ export function CreateCashout({
* depending on the isDebit flag
*/
const inputAmount = Amounts.parseOrThrow(
- `${form.isDebit ? regional_currency : fiat_currency}:${!form.amount ? "0" : form.amount
+ `${form.isDebit ? regional_currency : fiat_currency}:${
+ !form.amount ? "0" : form.amount
}`,
);
@@ -236,15 +244,23 @@ export function CreateCashout({
? i18n.str`Invalid`
: Amounts.cmp(limit, calc.debit) === -1
? i18n.str`Balance is not enough`
- : form.isDebit &&
- Amounts.cmp(inputAmount, conversionInfo.cashout_min_amount) < 1
- ? i18n.str`Needs to be higher than ${Amounts.stringifyValueWithSpec(
- Amounts.parseOrThrow(conversionInfo.cashout_min_amount),
- regional_currency_specification,
- ).normal
- }`
- : calculationResult === "amount-is-too-small"
+ : calculationResult === "amount-is-too-small"
? i18n.str`Amount needs to be higher`
+ : Amounts.cmp(calc.debit, conversionInfo.cashout_min_amount) < 0
+ ? i18n.str`No account can't cashout less than ${
+ Amounts.stringifyValueWithSpec(
+ Amounts.parseOrThrow(conversionInfo.cashout_min_amount),
+ regional_currency_specification,
+ ).normal
+ }`
+ : Amounts.cmp(calc.debit, account.minCashout) < 0
+ ? i18n.str`Your account can't cashout less than ${
+ Amounts.stringifyValueWithSpec(
+ Amounts.parseOrThrow(account.minCashout),
+ regional_currency_specification,
+ ).normal
+ }`
+
: Amounts.isZero(calc.credit)
? i18n.str`The total transfer at destination will be zero`
: undefined,
@@ -254,21 +270,17 @@ export function CreateCashout({
async function createCashout() {
const request_uid = encodeCrock(getRandomBytes(32));
await handleError(async () => {
- // new cashout api doesn't require channel
- const validChannel = config.supported_tan_channels.length === 0 ||
- form.channel;
-
- if (!creds || !form.subject || !validChannel) return;
+ if (!creds || !form.subject) return;
const request = {
request_uid,
amount_credit: Amounts.stringify(calc.credit),
amount_debit: Amounts.stringify(calc.debit),
subject: form.subject,
- tan_channel: form.channel,
};
const resp = await api.createCashout(creds, request);
if (resp.type === "ok") {
notifyInfo(i18n.str`Cashout created`);
+ onCashout();
} else {
switch (resp.case) {
case HttpStatusCode.Accepted: {
@@ -329,6 +341,15 @@ export function CreateCashout({
debug: resp.detail,
when: AbsoluteTime.now(),
});
+ case TalerErrorCode.BANK_CONVERSION_AMOUNT_TO_SMALL:
+ return notify({
+ type: "error",
+ title: i18n.str`The amount is less than the minimum allowed.`,
+ description: resp.detail.hint as TranslatedString,
+ debug: resp.detail,
+ when: AbsoluteTime.now(),
+ });
+
case TalerErrorCode.BANK_TAN_CHANNEL_SCRIPT_FAILED:
return notify({
type: "error",
@@ -606,9 +627,9 @@ export function CreateCashout({
cashoutDisabled
? undefined
: (value) => {
- form.amount = value;
- updateForm(structuredClone(form));
- }
+ form.amount = value;
+ updateForm(structuredClone(form));
+ }
}
/>
<ShowInputErrorLabel
@@ -649,7 +670,7 @@ export function CreateCashout({
</dd>
</div>
{Amounts.isZero(sellFee) ||
- Amounts.isZero(calc.beforeFee) ? undefined : (
+ Amounts.isZero(calc.beforeFee) ? undefined : (
<div class="flex items-center justify-between border-t-2 afu pt-4">
<dt class="flex items-center text-sm text-gray-600">
<span>
@@ -679,7 +700,6 @@ export function CreateCashout({
</dl>
</div>
)}
-
</div>
</div>