Skip to content

Commit

Permalink
fix(transfer): add validation for quick setup numberpad
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed May 3, 2024
1 parent c664cfd commit fae800b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
43 changes: 37 additions & 6 deletions src/screens/Lightning/NumberPadLightning.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import React, { memo, ReactElement, useState } from 'react';
import { StyleProp, StyleSheet, ViewStyle } from 'react-native';
import { useAppSelector } from '../../hooks/redux';
import { useTranslation } from 'react-i18next';

import NumberPad from '../../components/NumberPad';
import NumberPadButtons from '../Wallets/NumberPadButtons';
import {
conversionUnitSelector,
numberPadSelector,
} from '../../store/reselect/settings';
import { handleNumberPadPress } from '../../utils/numberpad';
import { convertToSats } from '../../utils/conversion';
import { useAppSelector } from '../../hooks/redux';
import { vibrate } from '../../utils/helpers';
import { showToast } from '../../utils/notifications';
import { convertToSats } from '../../utils/conversion';
import { handleNumberPadPress } from '../../utils/numberpad';

const NumberPadLightning = ({
value,
minAmount = 0,
maxAmount,
style,
onChange,
onMax,
onChangeUnit,
onDone,
style,
}: {
value: string;
minAmount?: number;
maxAmount: number;
style?: StyleProp<ViewStyle>;
onChange: (value: string) => void;
onMax: () => void;
onChangeUnit: () => void;
onDone: () => void;
style?: StyleProp<ViewStyle>;
}): ReactElement => {
const { t } = useTranslation('lightning');
const [errorKey, setErrorKey] = useState<string>();
const conversionUnit = useAppSelector(conversionUnitSelector);
const { maxLength, maxDecimals, type } = useAppSelector(numberPadSelector);
Expand All @@ -50,6 +55,32 @@ const NumberPadLightning = ({
}
};

const onDonePress = (): void => {
const amount = convertToSats(value, conversionUnit);

if (amount < minAmount && amount !== 0) {
vibrate({ type: 'notificationWarning' });
showToast({
type: 'warning',
title: t('error_channel_purchase'),
description: t('transfer.error_min_amount', { amount: minAmount }),
});
return;
}

if (amount > maxAmount) {
vibrate({ type: 'notificationWarning' });
showToast({
type: 'warning',
title: t('error_channel_purchase'),
description: t('transfer.error_max_amount', { amount: maxAmount }),
});
return;
}

onDone();
};

return (
<NumberPad
style={[styles.numberpad, style]}
Expand All @@ -60,7 +91,7 @@ const NumberPadLightning = ({
color="white"
onMax={onMax}
onChangeUnit={onChangeUnit}
onDone={onDone}
onDone={onDonePress}
/>
</NumberPad>
);
Expand Down
1 change: 1 addition & 0 deletions src/screens/Lightning/QuickSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ const QuickSetup = ({
<NumberPadLightning
style={styles.numberpad}
value={textFieldValue}
minAmount={lnSetup.initialClientBalance}
maxAmount={lnSetup.slider.maxValue}
onChange={setTextFieldValue}
onChangeUnit={onChangeUnit}
Expand Down
14 changes: 4 additions & 10 deletions src/utils/i18n/locales/en/lightning.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@
"title_numpad": "Spending <accent>Balance</accent>",
"text_slider": "Choose how much money you want to be able to spend instantly and how much you want to keep in savings.",
"text_numpad": "Enter the amount of money you want to be able to spend instantly.",
"swipe": "Swipe To Transfer"
},
"quick_setup": {
"nav_title": "Transfer Funds",
"title_slider": "Balance\n<accent>your funds</accent>",
"title_numpad": "Spending <accent>Balance</accent>",
"text_slider": "Choose how much money you want to be able to spend instantly and how much you want to keep in savings.",
"text_numpad": "Enter the amount of money you want to be able to spend instantly.",
"swipe": "Swipe To Transfer"
"swipe": "Swipe To Transfer",
"error_min_amount": "Spending amount needs to be at least ₿ {amount}",
"error_max_amount": "Spending amount needs to be smaller than ₿ {amount}"
},
"error_channel_purchase": "Instant Payments Setup Failed",
"error_channel_receiving": "Receiving amount needs to be greater than ${usdValue}",
Expand Down Expand Up @@ -134,7 +128,7 @@
"support": "Support",
"connection_closed": {
"title": "Connection Closed",
"description": "A Lightning Connection has closed. The funds will move back to your savings."
"description": "Your Lightning Connection has successfully closed. Your funds will be transferred to your savings."
},
"close_conn": "Close Connection",
"close_error": "Transfer Failed",
Expand Down

0 comments on commit fae800b

Please sign in to comment.