Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transfer): add validation for quick setup numberpad #1784

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion src/screens/Lightning/QuickSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import FancySlider from '../../components/FancySlider';
import TransferTextField from '../../components/TransferTextField';
import NumberPadLightning from './NumberPadLightning';
import { useAppSelector } from '../../hooks/redux';
import { useSwitchUnit } from '../../hooks/wallet';
import { useBalance, useSwitchUnit } from '../../hooks/wallet';
import {
resetSendTransaction,
setupOnChainTransaction,
Expand Down Expand Up @@ -48,6 +48,7 @@ const QuickSetup = ({
}: LightningScreenProps<'QuickSetup'>): ReactElement => {
const { t } = useTranslation('lightning');
const switchUnit = useSwitchUnit();
const { lightningBalance } = useBalance();
const unit = useAppSelector(unitSelector);
const nextUnit = useAppSelector(nextUnitSelector);
const conversionUnit = useAppSelector(conversionUnitSelector);
Expand Down Expand Up @@ -325,6 +326,7 @@ const QuickSetup = ({
<NumberPadLightning
style={styles.numberpad}
value={textFieldValue}
minAmount={lightningBalance}
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
Loading