Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import {useCallback, useState} from 'react'import {Keyboard, View} from 'react-native'import {Trans, useLingui} from '@lingui/react/macro'import * as EmailValidator from 'email-validator'
import {createServiceClient} from '#/lib/lexClient'import {cleanError, isNetworkError} from '#/lib/strings/errors'import {logger} from '#/logger'import {atoms as a, useTheme, web} from '#/alf'import {Admonition} from '#/components/Admonition'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import {HostingProvider} from '#/components/forms/HostingProvider'import * as TextField from '#/components/forms/TextField'import {At_Stroke2_Corner0_Rounded as At} from '#/components/icons/At'import {Loader} from '#/components/Loader'import {Text} from '#/components/Typography'import {IS_WEB} from '#/env'import {com} from '#/lexicons'import {FormContainer} from './FormContainer'
type ServiceDescription = com.atproto.server.describeServer.$OutputBody
export const ForgotPasswordForm = ({ error, serviceUrl, serviceDescription, setError, setServiceUrl, onPressBack, onEmailSent,}: { error: string serviceUrl: string serviceDescription: ServiceDescription | undefined setError: (v: string) => void setServiceUrl: (v: string) => void onPressBack: () => void onEmailSent: () => void}) => { const t = useTheme() const [isProcessing, setIsProcessing] = useState<boolean>(false) const [email, setEmail] = useState<string>('') const {t: l} = useLingui()
const onPressSelectService = useCallback(() => { Keyboard.dismiss() }, [])
const onPressNext = async () => { if (!EmailValidator.validate(email)) { return setError(l`Your email appears to be invalid.`) }
setError('') setIsProcessing(true)
try { /* * Pre-auth request against a user-chosen host, so it goes through a * one-off service client rather than a session-scoped one. */ const client = createServiceClient(serviceUrl) await client.call(com.atproto.server.requestPasswordReset, {email}) onEmailSent() } catch (err) { logger.warn('Failed to request password reset', {error: err}) setIsProcessing(false) if (isNetworkError(err)) { setError( l`Unable to contact your service. Please check your Internet connection.`, ) } else { setError(cleanError(err)) } } }
return ( <FormContainer testID="forgotPasswordForm" titleText={<Trans>Reset password</Trans>}> <View> <TextField.LabelText> <Trans>Hosting provider</Trans> </TextField.LabelText> <HostingProvider serviceUrl={serviceUrl} onSelectServiceUrl={setServiceUrl} onOpenDialog={onPressSelectService} /> </View> <View> <TextField.LabelText> <Trans>Email address</Trans> </TextField.LabelText> <TextField.Root> <TextField.Icon icon={At} /> <TextField.Input testID="forgotPasswordEmail" label={l`Enter your email address`} autoCapitalize="none" autoFocus autoCorrect={false} autoComplete="email" value={email} onChangeText={setEmail} editable={!isProcessing} accessibilityHint={l`Sets email for password reset`} /> </TextField.Root> </View> <Text style={[t.atoms.text_contrast_high, a.leading_snug]}> <Trans> Enter the email you used to create your account. We'll send you a "reset code" so you can set a new password. </Trans> </Text> {error && <Admonition type="error">{error}</Admonition>} <View style={[web([a.flex_row, a.align_center]), a.pt_md]}> {IS_WEB && ( <> <Button label={l`Back`} color="secondary" size="large" onPress={onPressBack}> <ButtonText> <Trans>Back</Trans> </ButtonText> </Button> <View style={a.flex_1} /> </> )} {!serviceDescription ? ( <Button label={l`Connecting to service...`} size="large" color="secondary" disabled> <ButtonIcon icon={Loader} /> <ButtonText> <Trans>Connecting…</Trans> </ButtonText> </Button> ) : ( <Button label={l`Next`} accessibilityHint={l`Navigates to the next screen`} color="primary" size="large" onPress={() => void onPressNext()} disabled={isProcessing}> <ButtonText> <Trans>Next</Trans> </ButtonText> {isProcessing && <ButtonIcon icon={Loader} />} </Button> )} </View> <View style={[ t.atoms.border_contrast_medium, a.border_t, a.pt_xl, a.mt_md, a.flex_row, a.justify_center, ]}> <Button testID="skipSendEmailButton" onPress={onEmailSent} label={l`Go to next`} accessibilityHint={l`Navigates to the next screen`} size="large" variant="ghost" color="secondary"> <ButtonText> <Trans>Already have a code?</Trans> </ButtonText> </Button> </View> </FormContainer> )}