-
Notifications
You must be signed in to change notification settings - Fork 8
/
SendAmount.tsx
135 lines (130 loc) · 3.69 KB
/
SendAmount.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { View, Text, useColorScheme, StyleSheet } from 'react-native';
import Colors from '../constants/Colors';
import CustomButton from '../components/utils/CustomButton';
import i18n from '../translationService';
import { useAppDispatch, useAppSelector } from '../hooks';
import {
selectAmount,
setAmount,
setReason,
unsetChosenContact,
} from '../store/sendFormSlice';
import { useLocalSearchParams, useRouter } from 'expo-router';
import { useState } from 'react';
import { setIsModalVisible } from '../store/appStateSlice';
import { CustomModal } from '../components/utils/CustomModal';
import NumberInputNumberPad, {
NumberLabel,
} from '../components/form/NumberInputNumberPad';
import { parseNumberPadInputForDeposit } from '../utils/functions';
export default function SendAmount() {
const colorScheme = useColorScheme();
const dispatch = useAppDispatch();
const amount = useAppSelector<number>(selectAmount);
const router = useRouter();
const [amountError, setAmountError] = useState<boolean>(false);
const params = useLocalSearchParams();
const { isRequest } = params;
const onChangeAmount = (amount: string) => {
if (!amount) {
setAmountError(true);
return;
}
if (!parseInt(amount)) {
setAmountError(true);
return;
}
if (Number.isNaN(parseInt(amount))) {
setAmountError(true);
return;
}
setAmountError(false);
dispatch(setAmount(parseInt(amount)));
};
const onModalCancel = () => {
dispatch(setAmount(0));
dispatch(setReason(''));
dispatch(setIsModalVisible(false));
router.push('/Home');
};
const onModalChangeContact = () => {
dispatch(setIsModalVisible(false));
dispatch(unsetChosenContact());
router.back();
};
const onPressNumberPadInput = (n: NumberLabel) => {
const newValue = parseNumberPadInputForDeposit(n, amount.toString());
// NB! conditional on false required because 0 falsy value
if (newValue !== false) {
dispatch(setAmount(newValue));
}
};
return (
<View
style={[
styles.container,
{
backgroundColor: Colors[colorScheme ?? 'light'].backgroundHighlight1,
},
]}
>
<CustomModal
type='error'
buttons={[
{ text: i18n.t('cancel'), onPress: onModalCancel },
{ text: i18n.t('sendamount_back'), onPress: onModalChangeContact },
]}
/>
<View>
<Text
style={{ fontSize: 24, color: Colors[colorScheme ?? 'light'].text }}
>
{isRequest
? i18n.t('request_how_much')
: i18n.t('sendamount_how_much')}
</Text>
{amountError ? (
<Text
style={{ fontSize: 10, color: Colors[colorScheme ?? 'light'].tint }}
>
{i18n.t('sendamount_validate_amount')}
</Text>
) : null}
<NumberInputNumberPad
onButtonPress={onPressNumberPadInput}
value={amount.toString()}
onChangeText={onChangeAmount}
/>
</View>
<View style={styles.buttonContainer}>
<CustomButton
disabled={!amount || amountError}
onPress={() => {
router.push(
isRequest ? '/SendReason?isRequest=true' : '/SendReason'
);
}}
text={i18n.t('sendamount_continue')}
/>
<CustomButton
onPress={() => router.back()}
text={i18n.t('sendamount_back')}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 30,
justifyContent: 'center',
},
buttonContainer: {
flexDirection: 'row',
gap: 35,
justifyContent: 'center',
top: 40,
padding: 10,
},
});