/
WalletNameForm.tsx
142 lines (127 loc) · 4.23 KB
/
WalletNameForm.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
136
137
138
139
140
141
142
import type {ReactNode} from 'react'
import React from 'react'
import {defineMessages, useIntl} from 'react-intl'
import {StyleSheet} from 'react-native'
import {ActivityIndicator, Image, SafeAreaView, View} from 'react-native'
import type {ImageSource} from 'react-native/Libraries/Image/ImageSource'
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet'
import {Button, ProgressStep, TextInput} from '../../../legacy/components/UiKit'
import {CONFIG} from '../../../legacy/config/config'
import globalMessages from '../../../legacy/i18n/global-messages'
import {spacing} from '../../../legacy/styles/config'
import {ignoreConcurrentAsyncHandler} from '../../../legacy/utils/utils'
import {getWalletNameError, validateWalletName} from '../../../legacy/utils/validators'
import {useWalletNames} from '../../hooks'
type Props = {
onSubmit: ({name: string}) => void
defaultWalletName?: string
image?: ImageSource
progress?: {
currentStep: number
totalSteps: number
}
containerStyle?: ViewStyleProp
buttonStyle?: ViewStyleProp
topContent?: ReactNode
bottomContent?: ReactNode
isWaiting?: boolean
}
export const WalletNameForm = ({
onSubmit,
image,
progress,
containerStyle,
buttonStyle,
topContent,
bottomContent,
isWaiting = false,
}: Props) => {
const strings = useStrings()
const [name, setName] = React.useState(CONFIG.HARDWARE_WALLETS.LEDGER_NANO.DEFAULT_WALLET_NAME || '')
const walletNames = useWalletNames()
const validationErrors = validateWalletName(name, null, walletNames || [])
const hasErrors = Object.keys(validationErrors).length > 0
const errorMessages = {
tooLong: strings.walletNameErrorTooLong,
nameAlreadyTaken: strings.walletNameErrorNameAlreadyTaken,
mustBeFilled: strings.walletNameErrorMustBeFilled,
}
const walletNameErrorText = getWalletNameError(errorMessages, validationErrors) || undefined
// eslint-disable-next-line react-hooks/exhaustive-deps
const submit = React.useCallback(ignoreConcurrentAsyncHandler(() => () => onSubmit({name}), 1000)(), [onSubmit, name])
return (
<SafeAreaView style={styles.safeAreaView}>
{progress != null && (
<ProgressStep currentStep={progress.currentStep} totalSteps={progress.totalSteps} displayStepNumber />
)}
<View style={[styles.container, containerStyle]}>
<View style={styles.heading}>{image != null && <Image source={image} />}</View>
{topContent}
<TextInput
autoFocus
label={strings.walletNameInputLabel}
value={name}
onChangeText={setName}
errorText={walletNameErrorText}
disabled={isWaiting}
/>
{bottomContent}
</View>
<View style={styles.buttonContainer}>
<Button
block
onPress={submit}
title={strings.save}
style={[styles.button, buttonStyle]}
disabled={hasErrors || isWaiting}
testID="saveWalletButton"
/>
</View>
{isWaiting ? <ActivityIndicator color="black" /> : null}
</SafeAreaView>
)
}
const styles = StyleSheet.create({
safeAreaView: {
flex: 1,
backgroundColor: '#fff',
},
container: {
paddingVertical: 24,
paddingHorizontal: 16,
flex: 1,
},
heading: {
alignItems: 'center',
justifyContent: 'center',
marginBottom: spacing.paragraphBottomMargin,
},
buttonContainer: {
flexDirection: 'row',
marginTop: 12,
},
button: {
marginHorizontal: 10,
marginVertical: 16,
},
})
const messages = defineMessages({
walletNameInputLabel: {
id: 'components.walletinit.walletform.walletNameInputLabel',
defaultMessage: '!!!Wallet name',
},
save: {
id: 'components.walletinit.connectnanox.savenanoxscreen.save',
defaultMessage: '!!!Save',
},
})
const useStrings = () => {
const intl = useIntl()
return {
walletNameInputLabel: intl.formatMessage(messages.walletNameInputLabel),
save: intl.formatMessage(messages.save),
walletNameErrorTooLong: intl.formatMessage(globalMessages.walletNameErrorTooLong),
walletNameErrorNameAlreadyTaken: intl.formatMessage(globalMessages.walletNameErrorNameAlreadyTaken),
walletNameErrorMustBeFilled: intl.formatMessage(globalMessages.walletNameErrorMustBeFilled),
}
}