import React, {useState, useEffect} from 'react' import { ActivityIndicator, Keyboard, KeyboardAvoidingView, ScrollView, StyleSheet, TextInput, TouchableOpacity, View, } from 'react-native' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {ComAtprotoAccountCreate} from '@atproto/api' import * as EmailValidator from 'email-validator' import {Logo} from './Logo' import {Picker} from '../util/Picker' import {TextLink} from '../util/Link' import {Text} from '../util/text/Text' import {s, colors} from '../../lib/styles' import { makeValidHandle, createFullHandle, toNiceDomain, } from '../../../lib/strings' import {useStores, DEFAULT_SERVICE} from '../../../state' import {ServiceDescription} from '../../../state/models/session' import {ServerInputModal} from '../../../state/models/shell-ui' export const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => { const store = useStores() const [isProcessing, setIsProcessing] = useState(false) const [serviceUrl, setServiceUrl] = useState(DEFAULT_SERVICE) const [error, setError] = useState('') const [serviceDescription, setServiceDescription] = useState< ServiceDescription | undefined >(undefined) const [userDomain, setUserDomain] = useState('') const [inviteCode, setInviteCode] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [handle, setHandle] = useState('') useEffect(() => { let aborted = false setError('') setServiceDescription(undefined) console.log('Fetching service description', serviceUrl) store.session.describeService(serviceUrl).then( desc => { if (aborted) return setServiceDescription(desc) setUserDomain(desc.availableUserDomains[0]) }, err => { if (aborted) return console.error(err) setError( 'Unable to contact your service. Please check your Internet connection.', ) }, ) return () => { aborted = true } }, [serviceUrl, store.session]) const onPressSelectService = () => { store.shell.openModal(new ServerInputModal(serviceUrl, setServiceUrl)) Keyboard.dismiss() } const onPressNext = async () => { if (!email) { return setError('Please enter your email.') } if (!EmailValidator.validate(email)) { return setError('Your email appears to be invalid.') } if (!password) { return setError('Please choose your password.') } if (!handle) { return setError('Please choose your username.') } setError('') setIsProcessing(true) try { await store.session.createAccount({ service: serviceUrl, email, handle: createFullHandle(handle, userDomain), password, inviteCode, }) } catch (e: any) { let errMsg = e.toString() if (e instanceof ComAtprotoAccountCreate.InvalidInviteCodeError) { errMsg = 'Invite code not accepted. Check that you input it correctly and try again.' } console.log(e) setIsProcessing(false) setError(errMsg.replace(/^Error:/, '')) } } const Policies = () => { if (!serviceDescription) { return } const tos = validWebLink(serviceDescription.links?.termsOfService) const pp = validWebLink(serviceDescription.links?.privacyPolicy) if (!tos && !pp) { return ( This service has not provided terms of service or a privacy policy. ) } const els = [] if (tos) { els.push( , ) } if (pp) { els.push( , ) } if (els.length === 2) { els.splice(1, 0, and ) } return ( By creating an account you agree to the {els}. ) } return ( {error ? ( {error} ) : undefined} Create a new account {toNiceDomain(serviceUrl)} Change {serviceDescription ? ( <> {serviceDescription?.inviteCodeRequired ? ( ) : undefined} ) : undefined} {serviceDescription ? ( <> Choose your username setHandle(makeValidHandle(v))} editable={!isProcessing} /> {serviceDescription.availableUserDomains.length > 1 && ( ({ label: `.${d}`, value: d, }))} onChange={itemValue => setUserDomain(itemValue)} enabled={!isProcessing} /> )} Your full username will be{' '} @{createFullHandle(handle, userDomain)} ) : undefined} Back {serviceDescription ? ( {isProcessing ? ( ) : ( Next )} ) : undefined} ) } function validWebLink(url?: string): string | undefined { return url && (url.startsWith('http://') || url.startsWith('https://')) ? url : undefined } const styles = StyleSheet.create({ logoHero: { paddingTop: 30, paddingBottom: 40, }, group: { borderWidth: 1, borderColor: colors.white, borderRadius: 10, marginBottom: 20, marginHorizontal: 20, backgroundColor: colors.blue3, }, groupTitle: { flexDirection: 'row', alignItems: 'center', paddingVertical: 8, paddingHorizontal: 12, }, groupContent: { borderTopWidth: 1, borderTopColor: colors.blue1, flexDirection: 'row', alignItems: 'center', }, groupContentIcon: { color: 'white', marginLeft: 10, }, textInput: { flex: 1, width: '100%', backgroundColor: colors.blue3, color: colors.white, paddingVertical: 10, paddingHorizontal: 12, fontSize: 18, borderRadius: 10, }, textBtn: { flexDirection: 'row', flex: 1, alignItems: 'center', }, textBtnLabel: { flex: 1, color: colors.white, paddingVertical: 10, paddingHorizontal: 12, fontSize: 18, }, textBtnFakeInnerBtn: { flexDirection: 'row', alignItems: 'center', backgroundColor: colors.blue2, borderRadius: 6, paddingVertical: 6, paddingHorizontal: 8, marginHorizontal: 6, }, textBtnFakeInnerBtnIcon: { color: colors.white, marginRight: 4, }, textBtnFakeInnerBtnLabel: { color: colors.white, }, picker: { flex: 1, width: '100%', backgroundColor: colors.blue3, color: colors.white, paddingVertical: 10, paddingHorizontal: 12, fontSize: 18, borderRadius: 10, }, pickerLabel: { color: colors.white, fontSize: 18, }, pickerIcon: { color: colors.white, }, policies: { flexDirection: 'row', alignItems: 'flex-start', paddingHorizontal: 20, paddingBottom: 20, }, error: { borderWidth: 1, borderColor: colors.red5, backgroundColor: colors.red4, flexDirection: 'row', alignItems: 'center', marginTop: -5, marginHorizontal: 20, marginBottom: 15, borderRadius: 8, paddingHorizontal: 8, paddingVertical: 8, }, errorFloating: { marginBottom: 20, marginHorizontal: 20, borderRadius: 8, }, errorIcon: { borderWidth: 1, borderColor: colors.white, color: colors.white, borderRadius: 30, width: 16, height: 16, alignItems: 'center', justifyContent: 'center', marginRight: 5, }, })