about summary refs log tree commit diff
path: root/src/view/com/auth/create/Step2.tsx
blob: f938bb9cea573fc8a03d10cd2eacb698ac8ae2c0 (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React from 'react'
import {
  ActivityIndicator,
  StyleSheet,
  TouchableWithoutFeedback,
  View,
} from 'react-native'
import {
  CreateAccountState,
  CreateAccountDispatch,
  requestVerificationCode,
} from './state'
import {Text} from 'view/com/util/text/Text'
import {StepHeader} from './StepHeader'
import {s} from 'lib/styles'
import {usePalette} from 'lib/hooks/usePalette'
import {TextInput} from '../util/TextInput'
import {Button} from '../../util/forms/Button'
import {ErrorMessage} from 'view/com/util/error/ErrorMessage'
import {isWeb} from 'platform/detection'
import {Trans, msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
import parsePhoneNumber from 'libphonenumber-js'

export function Step2({
  uiState,
  uiDispatch,
}: {
  uiState: CreateAccountState
  uiDispatch: CreateAccountDispatch
}) {
  const pal = usePalette('default')
  const {_} = useLingui()
  const {isMobile} = useWebMediaQueries()

  const onPressRequest = React.useCallback(() => {
    if (
      uiState.verificationPhone.length >= 9 &&
      parsePhoneNumber(uiState.verificationPhone, 'US')
    ) {
      requestVerificationCode({uiState, uiDispatch, _})
    } else {
      uiDispatch({
        type: 'set-error',
        value: _(
          msg`There's something wrong with this number. Please include your country and/or area code!`,
        ),
      })
    }
  }, [uiState, uiDispatch, _])

  const onPressRetry = React.useCallback(() => {
    uiDispatch({type: 'set-has-requested-verification-code', value: false})
  }, [uiDispatch])

  const phoneNumberFormatted = React.useMemo(
    () =>
      uiState.hasRequestedVerificationCode
        ? parsePhoneNumber(
            uiState.verificationPhone,
            'US',
          )?.formatInternational()
        : '',
    [uiState.hasRequestedVerificationCode, uiState.verificationPhone],
  )

  return (
    <View>
      <StepHeader uiState={uiState} title={_(msg`SMS verification`)} />

      {!uiState.hasRequestedVerificationCode ? (
        <>
          <View style={s.pb20}>
            <Text
              type="md-medium"
              style={[pal.text, s.mb2]}
              nativeID="phoneNumber">
              <Trans>Phone number</Trans>
            </Text>
            <TextInput
              testID="phoneInput"
              icon="phone"
              placeholder={_(msg`Enter your phone number`)}
              value={uiState.verificationPhone}
              editable
              onChange={value =>
                uiDispatch({type: 'set-verification-phone', value})
              }
              accessibilityLabel={_(msg`Email`)}
              accessibilityHint={_(
                msg`Input phone number for SMS verification`,
              )}
              accessibilityLabelledBy="phoneNumber"
              keyboardType="phone-pad"
              autoCapitalize="none"
              autoComplete="tel"
              autoCorrect={false}
              autoFocus={true}
            />
            <Text type="sm" style={[pal.textLight, s.mt5]}>
              <Trans>
                Please enter a phone number that can receive SMS text messages.
              </Trans>
            </Text>
          </View>

          <View style={isMobile ? {} : {flexDirection: 'row'}}>
            {uiState.isProcessing ? (
              <ActivityIndicator />
            ) : (
              <Button
                testID="requestCodeBtn"
                type="primary"
                label={_(msg`Request code`)}
                labelStyle={isMobile ? [s.flex1, s.textCenter, s.f17] : []}
                style={
                  isMobile ? {paddingVertical: 12, paddingHorizontal: 20} : {}
                }
                onPress={onPressRequest}
              />
            )}
          </View>
        </>
      ) : (
        <>
          <View style={s.pb20}>
            <View
              style={[
                s.flexRow,
                s.mb5,
                s.alignCenter,
                {justifyContent: 'space-between'},
              ]}>
              <Text
                type="md-medium"
                style={pal.text}
                nativeID="verificationCode">
                <Trans>Verification code</Trans>{' '}
              </Text>
              <TouchableWithoutFeedback
                onPress={onPressRetry}
                accessibilityLabel={_(msg`Retry.`)}
                accessibilityHint="">
                <View style={styles.touchable}>
                  <Text
                    type="md-medium"
                    style={pal.link}
                    nativeID="verificationCode">
                    <Trans>Retry</Trans>
                  </Text>
                </View>
              </TouchableWithoutFeedback>
            </View>
            <TextInput
              testID="codeInput"
              icon="hashtag"
              placeholder={_(msg`XXXXXX`)}
              value={uiState.verificationCode}
              editable
              onChange={value =>
                uiDispatch({type: 'set-verification-code', value})
              }
              accessibilityLabel={_(msg`Email`)}
              accessibilityHint={_(
                msg`Input the verification code we have texted to you`,
              )}
              accessibilityLabelledBy="verificationCode"
              keyboardType="phone-pad"
              autoCapitalize="none"
              autoComplete="one-time-code"
              textContentType="oneTimeCode"
              autoCorrect={false}
              autoFocus={true}
            />
            <Text type="sm" style={[pal.textLight, s.mt5]}>
              <Trans>Please enter the verification code sent to</Trans>{' '}
              {phoneNumberFormatted}.
            </Text>
          </View>
        </>
      )}

      {uiState.error ? (
        <ErrorMessage message={uiState.error} style={styles.error} />
      ) : undefined}
    </View>
  )
}

const styles = StyleSheet.create({
  error: {
    borderRadius: 6,
    marginTop: 10,
  },
  // @ts-expect-error: Suppressing error due to incomplete `ViewStyle` type definition in react-native-web, missing `cursor` prop as discussed in https://github.com/necolas/react-native-web/issues/832.
  touchable: {
    ...(isWeb && {cursor: 'pointer'}),
  },
})