about summary refs log tree commit diff
path: root/src/view/com/modals/VerifyEmail.tsx
blob: fce1275fe7cd3f712f1c20cc8b10069380d5ce9b (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import React, {useState} from 'react'
import {
  ActivityIndicator,
  Pressable,
  SafeAreaView,
  StyleSheet,
  View,
} from 'react-native'
import {Circle, Path, Svg} from 'react-native-svg'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {usePalette} from '#/lib/hooks/usePalette'
import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
import {cleanError} from '#/lib/strings/errors'
import {colors, s} from '#/lib/styles'
import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
import {useModalControls} from '#/state/modals'
import {useAgent, useSession} from '#/state/session'
import {ErrorMessage} from '../util/error/ErrorMessage'
import {Button} from '../util/forms/Button'
import {Text} from '../util/text/Text'
import * as Toast from '../util/Toast'
import {ScrollView, TextInput} from './util'

export const snapPoints = ['90%']

enum Stages {
  Reminder,
  Email,
  ConfirmCode,
}

export function Component({
  showReminder,
  onSuccess,
}: {
  showReminder?: boolean
  onSuccess?: () => void
}) {
  const pal = usePalette('default')
  const agent = useAgent()
  const {currentAccount} = useSession()
  const {_} = useLingui()
  const [stage, setStage] = useState<Stages>(
    showReminder ? Stages.Reminder : Stages.Email,
  )
  const [confirmationCode, setConfirmationCode] = useState<string>('')
  const [isProcessing, setIsProcessing] = useState<boolean>(false)
  const [error, setError] = useState<string>('')
  const {isMobile} = useWebMediaQueries()
  const {openModal, closeModal} = useModalControls()

  React.useEffect(() => {
    if (!currentAccount) {
      logger.error(`VerifyEmail modal opened without currentAccount`)
      closeModal()
    }
  }, [currentAccount, closeModal])

  const onSendEmail = async () => {
    setError('')
    setIsProcessing(true)
    try {
      await agent.com.atproto.server.requestEmailConfirmation()
      setStage(Stages.ConfirmCode)
    } catch (e) {
      setError(cleanError(String(e)))
    } finally {
      setIsProcessing(false)
    }
  }

  const onConfirm = async () => {
    setError('')
    setIsProcessing(true)
    try {
      await agent.com.atproto.server.confirmEmail({
        email: (currentAccount?.email || '').trim(),
        token: confirmationCode.trim(),
      })
      await agent.resumeSession(agent.session!)
      Toast.show(_(msg`Email verified`))
      closeModal()
      onSuccess?.()
    } catch (e) {
      setError(cleanError(String(e)))
    } finally {
      setIsProcessing(false)
    }
  }

  const onEmailIncorrect = () => {
    closeModal()
    openModal({name: 'change-email'})
  }

  return (
    <SafeAreaView style={[pal.view, s.flex1]}>
      <ScrollView
        testID="verifyEmailModal"
        style={[s.flex1, isMobile && {paddingHorizontal: 18}]}>
        {stage === Stages.Reminder && <ReminderIllustration />}
        <View style={styles.titleSection}>
          <Text type="title-lg" style={[pal.text, styles.title]}>
            {stage === Stages.Reminder ? (
              <Trans>Please Verify Your Email</Trans>
            ) : stage === Stages.Email ? (
              <Trans>Verify Your Email</Trans>
            ) : stage === Stages.ConfirmCode ? (
              <Trans>Enter Confirmation Code</Trans>
            ) : (
              ''
            )}
          </Text>
        </View>

        <Text type="lg" style={[pal.textLight, {marginBottom: 10}]}>
          {stage === Stages.Reminder ? (
            <Trans>
              Your email has not yet been verified. This is an important
              security step which we recommend.
            </Trans>
          ) : stage === Stages.Email ? (
            <Trans>
              This is important in case you ever need to change your email or
              reset your password.
            </Trans>
          ) : stage === Stages.ConfirmCode ? (
            <Trans>
              An email has been sent to {currentAccount?.email || '(no email)'}.
              It includes a confirmation code which you can enter below.
            </Trans>
          ) : (
            ''
          )}
        </Text>

        {stage === Stages.Email ? (
          <>
            <View style={styles.emailContainer}>
              <FontAwesomeIcon
                icon="envelope"
                color={pal.colors.text}
                size={16}
              />
              <Text type="xl-medium" style={[pal.text, s.flex1, {minWidth: 0}]}>
                {currentAccount?.email || _(msg`(no email)`)}
              </Text>
            </View>
            <Pressable
              accessibilityRole="link"
              accessibilityLabel={_(msg`Change my email`)}
              accessibilityHint=""
              onPress={onEmailIncorrect}
              style={styles.changeEmailLink}>
              <Text type="lg" style={pal.link}>
                <Trans>Change</Trans>
              </Text>
            </Pressable>
          </>
        ) : stage === Stages.ConfirmCode ? (
          <TextInput
            testID="confirmCodeInput"
            style={[styles.textInput, pal.border, pal.text]}
            placeholder="XXXXX-XXXXX"
            placeholderTextColor={pal.colors.textLight}
            value={confirmationCode}
            onChangeText={setConfirmationCode}
            accessible={true}
            accessibilityLabel={_(msg`Confirmation code`)}
            accessibilityHint=""
            autoCapitalize="none"
            autoComplete="one-time-code"
            autoCorrect={false}
          />
        ) : undefined}

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

        <View style={[styles.btnContainer]}>
          {isProcessing ? (
            <View style={styles.btn}>
              <ActivityIndicator color="#fff" />
            </View>
          ) : (
            <View style={{gap: 6}}>
              {stage === Stages.Reminder && (
                <Button
                  testID="getStartedBtn"
                  type="primary"
                  onPress={() => setStage(Stages.Email)}
                  accessibilityLabel={_(msg`Get Started`)}
                  accessibilityHint=""
                  label={_(msg`Get Started`)}
                  labelContainerStyle={{justifyContent: 'center', padding: 4}}
                  labelStyle={[s.f18]}
                />
              )}
              {stage === Stages.Email && (
                <>
                  <Button
                    testID="sendEmailBtn"
                    type="primary"
                    onPress={onSendEmail}
                    accessibilityLabel={_(msg`Send Confirmation Email`)}
                    accessibilityHint=""
                    label={_(msg`Send Confirmation Email`)}
                    labelContainerStyle={{
                      justifyContent: 'center',
                      padding: 4,
                    }}
                    labelStyle={[s.f18]}
                  />
                  <Button
                    testID="haveCodeBtn"
                    type="default"
                    accessibilityLabel={_(msg`I have a code`)}
                    accessibilityHint=""
                    label={_(msg`I have a confirmation code`)}
                    labelContainerStyle={{
                      justifyContent: 'center',
                      padding: 4,
                    }}
                    labelStyle={[s.f18]}
                    onPress={() => setStage(Stages.ConfirmCode)}
                  />
                </>
              )}
              {stage === Stages.ConfirmCode && (
                <Button
                  testID="confirmBtn"
                  type="primary"
                  onPress={onConfirm}
                  accessibilityLabel={_(msg`Confirm`)}
                  accessibilityHint=""
                  label={_(msg`Confirm`)}
                  labelContainerStyle={{justifyContent: 'center', padding: 4}}
                  labelStyle={[s.f18]}
                />
              )}
              <Button
                testID="cancelBtn"
                type="default"
                onPress={() => {
                  closeModal()
                }}
                accessibilityLabel={
                  stage === Stages.Reminder
                    ? _(msg`Not right now`)
                    : _(msg`Cancel`)
                }
                accessibilityHint=""
                label={
                  stage === Stages.Reminder
                    ? _(msg`Not right now`)
                    : _(msg`Cancel`)
                }
                labelContainerStyle={{justifyContent: 'center', padding: 4}}
                labelStyle={[s.f18]}
              />
            </View>
          )}
        </View>
      </ScrollView>
    </SafeAreaView>
  )
}

function ReminderIllustration() {
  const pal = usePalette('default')
  const palInverted = usePalette('inverted')
  return (
    <View style={[pal.viewLight, {borderRadius: 8, marginBottom: 20}]}>
      <Svg viewBox="0 0 112 84" fill="none" height={200}>
        <Path
          fillRule="evenodd"
          clipRule="evenodd"
          d="M26 26.4264V55C26 60.5229 30.4772 65 36 65H76C81.5228 65 86 60.5229 86 55V27.4214L63.5685 49.8528C59.6633 53.7581 53.3316 53.7581 49.4264 49.8528L26 26.4264Z"
          fill={palInverted.colors.background}
        />
        <Path
          fillRule="evenodd"
          clipRule="evenodd"
          d="M83.666 19.5784C85.47 21.7297 84.4897 24.7895 82.5044 26.7748L60.669 48.6102C58.3259 50.9533 54.5269 50.9533 52.1838 48.6102L29.9502 26.3766C27.8241 24.2505 26.8952 20.8876 29.0597 18.8005C30.8581 17.0665 33.3045 16 36 16H76C79.0782 16 81.8316 17.3908 83.666 19.5784Z"
          fill={palInverted.colors.background}
        />
        <Circle cx="82" cy="61" r="13" fill="#20BC07" />
        <Path d="M75 61L80 66L89 57" stroke="white" strokeWidth="2" />
      </Svg>
    </View>
  )
}

const styles = StyleSheet.create({
  titleSection: {
    paddingTop: isWeb ? 0 : 4,
    paddingBottom: isWeb ? 14 : 10,
  },
  title: {
    textAlign: 'center',
    fontWeight: '600',
    marginBottom: 5,
  },
  error: {
    borderRadius: 6,
    marginTop: 10,
  },
  emailContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 6,
    paddingHorizontal: 14,
    marginTop: 10,
  },
  changeEmailLink: {
    marginHorizontal: 12,
    marginBottom: 12,
  },
  textInput: {
    borderWidth: 1,
    borderRadius: 6,
    paddingHorizontal: 14,
    paddingVertical: 10,
    fontSize: 16,
  },
  btn: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: 32,
    padding: 14,
    backgroundColor: colors.blue3,
  },
  btnContainer: {
    paddingTop: 20,
  },
})