|
| 1 | +// Third party dependencies |
| 2 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 3 | +import { useNavigation } from '@react-navigation/native'; |
| 4 | + |
| 5 | +// External dependencies |
| 6 | +import Row from '../../components/Row'; |
| 7 | +import ScreenLayout from '../../components/ScreenLayout'; |
| 8 | +import Text, { |
| 9 | + TextVariant, |
| 10 | +} from '../../../../../component-library/components/Texts/Text'; |
| 11 | +import TextField from '../../../../../component-library/components/Form/TextField'; |
| 12 | +import Label from '../../../../../component-library/components/Form/Label'; |
| 13 | +import Button, { |
| 14 | + ButtonVariants, |
| 15 | + ButtonSize, |
| 16 | +} from '../../../../../component-library/components/Buttons/Button'; |
| 17 | + |
| 18 | +import { getNavigationOptionsTitle } from '../../../Navbar'; |
| 19 | +import { |
| 20 | + createNavigationDetails, |
| 21 | + useParams, |
| 22 | +} from '../../../../../util/navigation/navUtils'; |
| 23 | +import { useTheme } from '../../../../../util/theme'; |
| 24 | +import Routes from '../../../../../constants/navigation/Routes'; |
| 25 | +import { strings } from '../../../../../../locales/i18n'; |
| 26 | +import { regex } from '../../../../../util/regex'; |
| 27 | + |
| 28 | +// Internal dependencies |
| 29 | +import styles from './Settings.styles'; |
| 30 | + |
| 31 | +interface ActivationKeyFormParams { |
| 32 | + onSubmit: (key: string, label: string, active: boolean) => void; |
| 33 | + key: string; |
| 34 | + active: boolean; |
| 35 | + label: string; |
| 36 | +} |
| 37 | + |
| 38 | +export const createActivationKeyFormNavDetails = |
| 39 | + createNavigationDetails<ActivationKeyFormParams>( |
| 40 | + Routes.RAMP.ACTIVATION_KEY_FORM, |
| 41 | + ); |
| 42 | + |
| 43 | +function ActivationKeyForm() { |
| 44 | + const navigation = useNavigation(); |
| 45 | + const { |
| 46 | + key, |
| 47 | + label: initialLabel, |
| 48 | + active, |
| 49 | + onSubmit, |
| 50 | + } = useParams<ActivationKeyFormParams>(); |
| 51 | + const [activationKey, setActivationKey] = useState(key ?? ''); |
| 52 | + const [label, setLabel] = useState(initialLabel ?? ''); |
| 53 | + const { colors } = useTheme(); |
| 54 | + const style = styles(); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + navigation.setOptions( |
| 58 | + getNavigationOptionsTitle( |
| 59 | + key |
| 60 | + ? strings('app_settings.fiat_on_ramp.edit_activation_key') |
| 61 | + : strings('app_settings.fiat_on_ramp.add_activation_key'), |
| 62 | + navigation, |
| 63 | + false, |
| 64 | + colors, |
| 65 | + ), |
| 66 | + ); |
| 67 | + }, [colors, key, navigation]); |
| 68 | + |
| 69 | + const handleSubmit = useCallback(() => { |
| 70 | + if (!regex.activationKey.test(activationKey)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + onSubmit(activationKey, label, active); |
| 74 | + navigation.goBack(); |
| 75 | + }, [activationKey, navigation, onSubmit, active, label]); |
| 76 | + |
| 77 | + const handleCancel = useCallback(() => { |
| 78 | + navigation.goBack(); |
| 79 | + }, [navigation]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <ScreenLayout> |
| 83 | + <ScreenLayout.Body> |
| 84 | + <ScreenLayout.Content> |
| 85 | + <Text variant={TextVariant.BodyLGMedium}> |
| 86 | + {key |
| 87 | + ? strings('app_settings.fiat_on_ramp.edit_activation_key') |
| 88 | + : strings('app_settings.fiat_on_ramp.add_activation_key')} |
| 89 | + </Text> |
| 90 | + |
| 91 | + <Row> |
| 92 | + <Label>{strings('app_settings.fiat_on_ramp.label')}</Label> |
| 93 | + <TextField |
| 94 | + autoCapitalize={'none'} |
| 95 | + onChangeText={setLabel} |
| 96 | + placeholder={strings('app_settings.fiat_on_ramp.add_label')} |
| 97 | + numberOfLines={1} |
| 98 | + value={label} |
| 99 | + returnKeyType={'done'} |
| 100 | + onSubmitEditing={handleSubmit} |
| 101 | + autoFocus |
| 102 | + /> |
| 103 | + </Row> |
| 104 | + <Row> |
| 105 | + <Label>{strings('app_settings.fiat_on_ramp.key')}</Label> |
| 106 | + <TextField |
| 107 | + autoCapitalize={'none'} |
| 108 | + autoCorrect={false} |
| 109 | + onChangeText={setActivationKey} |
| 110 | + placeholder={strings( |
| 111 | + 'app_settings.fiat_on_ramp.paste_or_type_activation_key', |
| 112 | + )} |
| 113 | + spellCheck={false} |
| 114 | + numberOfLines={1} |
| 115 | + value={activationKey} |
| 116 | + returnKeyType={'done'} |
| 117 | + onSubmitEditing={handleSubmit} |
| 118 | + isReadonly={Boolean(key)} |
| 119 | + /> |
| 120 | + </Row> |
| 121 | + |
| 122 | + <Row style={style.buttons}> |
| 123 | + <Button |
| 124 | + variant={ButtonVariants.Secondary} |
| 125 | + size={ButtonSize.Lg} |
| 126 | + style={style.button} |
| 127 | + onPress={handleCancel} |
| 128 | + label={strings('app_settings.fiat_on_ramp.cancel')} |
| 129 | + /> |
| 130 | + <Button |
| 131 | + variant={ButtonVariants.Primary} |
| 132 | + size={ButtonSize.Lg} |
| 133 | + style={style.button} |
| 134 | + onPress={handleSubmit} |
| 135 | + label={ |
| 136 | + key |
| 137 | + ? strings('app_settings.fiat_on_ramp.update') |
| 138 | + : strings('app_settings.fiat_on_ramp.add') |
| 139 | + } |
| 140 | + isDisabled={!regex.activationKey.test(activationKey)} |
| 141 | + /> |
| 142 | + </Row> |
| 143 | + </ScreenLayout.Content> |
| 144 | + </ScreenLayout.Body> |
| 145 | + </ScreenLayout> |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +export default ActivationKeyForm; |
0 commit comments