Fixes to account creation

zio/stable
Paul Frazee 2022-11-10 17:05:26 -06:00
parent f6b3e5f9e0
commit 0fc8ef3358
4 changed files with 81 additions and 59 deletions

View File

@ -1,22 +1,9 @@
import {makeAutoObservable} from 'mobx' import {makeAutoObservable} from 'mobx'
import * as GetSuggestions from '../../third-party/api/src/client/types/app/bsky/actor/getSuggestions'
import {RootStoreModel} from './root-store' import {RootStoreModel} from './root-store'
import {Declaration} from './_common' import {Declaration} from './_common'
interface Response { type ResponseSuggestedActor = GetSuggestions.OutputSchema['actors'][number]
data: {
suggestions: ResponseSuggestedActor[]
}
}
export type ResponseSuggestedActor = {
did: string
handle: string
declaration: Declaration
displayName?: string
description?: string
createdAt?: string
indexedAt: string
}
export type SuggestedActor = ResponseSuggestedActor & { export type SuggestedActor = ResponseSuggestedActor & {
_reactKey: string _reactKey: string
} }
@ -90,14 +77,7 @@ export class SuggestedActorsViewModel {
private async _fetch(isRefreshing = false) { private async _fetch(isRefreshing = false) {
this._xLoading(isRefreshing) this._xLoading(isRefreshing)
try { try {
const debugRes = await this.rootStore.api.app.bsky.graph.getFollowers({ const res = await this.rootStore.api.app.bsky.actor.getSuggestions()
user: 'alice.test',
})
const res = {
data: {
suggestions: debugRes.data.followers,
},
}
this._replaceAll(res) this._replaceAll(res)
this._xIdle() this._xIdle()
} catch (e: any) { } catch (e: any) {
@ -105,10 +85,10 @@ export class SuggestedActorsViewModel {
} }
} }
private _replaceAll(res: Response) { private _replaceAll(res: GetSuggestions.Response) {
this.suggestions.length = 0 this.suggestions.length = 0
let counter = 0 let counter = 0
for (const item of res.data.suggestions) { for (const item of res.data.actors) {
this._append({ this._append({
_reactKey: `item-${counter++}`, _reactKey: `item-${counter++}`,
...item, ...item,

View File

@ -1,4 +1,4 @@
import React, {useMemo, useEffect} from 'react' import React, {useMemo, useEffect, useState} from 'react'
import { import {
ActivityIndicator, ActivityIndicator,
FlatList, FlatList,
@ -10,9 +10,12 @@ import {
import LinearGradient from 'react-native-linear-gradient' import LinearGradient from 'react-native-linear-gradient'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import _omit from 'lodash.omit'
import {ErrorScreen} from '../util/ErrorScreen' import {ErrorScreen} from '../util/ErrorScreen'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import Toast from '../util/Toast'
import {useStores} from '../../../state' import {useStores} from '../../../state'
import * as apilib from '../../../state/lib/api'
import { import {
SuggestedActorsViewModel, SuggestedActorsViewModel,
SuggestedActor, SuggestedActor,
@ -22,6 +25,7 @@ import {s, colors, gradients} from '../../lib/styles'
export const SuggestedFollows = observer( export const SuggestedFollows = observer(
({onNoSuggestions}: {onNoSuggestions?: () => void}) => { ({onNoSuggestions}: {onNoSuggestions?: () => void}) => {
const store = useStores() const store = useStores()
const [follows, setFollows] = useState<Record<string, string>>({})
const view = useMemo<SuggestedActorsViewModel>( const view = useMemo<SuggestedActorsViewModel>(
() => new SuggestedActorsViewModel(store), () => new SuggestedActorsViewModel(store),
@ -46,7 +50,39 @@ export const SuggestedFollows = observer(
.setup() .setup()
.catch((err: any) => console.error('Failed to fetch suggestions', err)) .catch((err: any) => console.error('Failed to fetch suggestions', err))
const renderItem = ({item}: {item: SuggestedActor}) => <User item={item} /> const onPressFollow = async (item: SuggestedActor) => {
try {
const res = await apilib.follow(store, item.did, item.declaration.cid)
setFollows({[item.did]: res.uri, ...follows})
} catch (e) {
console.log(e)
Toast.show('An issue occurred, please try again.', {
duration: Toast.durations.LONG,
position: Toast.positions.TOP,
})
}
}
const onPressUnfollow = async (item: SuggestedActor) => {
try {
await apilib.unfollow(store, follows[item.did])
setFollows(_omit(follows, [item.did]))
} catch (e) {
console.log(e)
Toast.show('An issue occurred, please try again.', {
duration: Toast.durations.LONG,
position: Toast.positions.TOP,
})
}
}
const renderItem = ({item}: {item: SuggestedActor}) => (
<User
item={item}
follow={follows[item.did]}
onPressFollow={onPressFollow}
onPressUnfollow={onPressUnfollow}
/>
)
return ( return (
<View style={styles.container}> <View style={styles.container}>
{view.isLoading ? ( {view.isLoading ? (
@ -75,7 +111,17 @@ export const SuggestedFollows = observer(
}, },
) )
const User = ({item}: {item: SuggestedActor}) => { const User = ({
item,
follow,
onPressFollow,
onPressUnfollow,
}: {
item: SuggestedActor
follow: string | undefined
onPressFollow: (item: SuggestedActor) => void
onPressUnfollow: (item: SuggestedActor) => void
}) => {
return ( return (
<View style={styles.actor}> <View style={styles.actor}>
<View style={styles.actorMeta}> <View style={styles.actorMeta}>
@ -88,23 +134,35 @@ const User = ({item}: {item: SuggestedActor}) => {
</View> </View>
<View style={styles.actorContent}> <View style={styles.actorContent}>
<Text style={[s.f17, s.bold]} numberOfLines={1}> <Text style={[s.f17, s.bold]} numberOfLines={1}>
{item.displayName} {item.displayName || item.handle}
</Text> </Text>
<Text style={[s.f14, s.gray5]} numberOfLines={1}> <Text style={[s.f14, s.gray5]} numberOfLines={1}>
@{item.handle} @{item.handle}
</Text> </Text>
</View> </View>
<View style={styles.actorBtn}> <View style={styles.actorBtn}>
<TouchableOpacity> {follow ? (
<LinearGradient <TouchableOpacity onPress={() => onPressUnfollow(item)}>
colors={[gradients.primary.start, gradients.primary.end]} <View style={[styles.btn, styles.secondaryBtn]}>
start={{x: 0, y: 0}} <Text style={[s.gray5, s.fw600, s.f15]}>Unfollow</Text>
end={{x: 1, y: 1}} </View>
style={[styles.btn, styles.gradientBtn]}> </TouchableOpacity>
<FontAwesomeIcon icon="plus" style={[s.white, s.mr5]} size={15} /> ) : (
<Text style={[s.white, s.fw600, s.f15]}>Follow</Text> <TouchableOpacity onPress={() => onPressFollow(item)}>
</LinearGradient> <LinearGradient
</TouchableOpacity> colors={[gradients.primary.start, gradients.primary.end]}
start={{x: 0, y: 0}}
end={{x: 1, y: 1}}
style={[styles.btn, styles.gradientBtn]}>
<FontAwesomeIcon
icon="plus"
style={[s.white, s.mr5]}
size={15}
/>
<Text style={[s.white, s.fw600, s.f15]}>Follow</Text>
</LinearGradient>
</TouchableOpacity>
)}
</View> </View>
</View> </View>
{item.description ? ( {item.description ? (

View File

@ -15,6 +15,7 @@ import * as EmailValidator from 'email-validator'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {Picker} from '../com/util/Picker' import {Picker} from '../com/util/Picker'
import {s, colors} from '../lib/styles' import {s, colors} from '../lib/styles'
import {makeValidHandle, createFullHandle} from '../lib/strings'
import {useStores, DEFAULT_SERVICE} from '../../state' import {useStores, DEFAULT_SERVICE} from '../../state'
import {ServiceDescription} from '../../state/models/session' import {ServiceDescription} from '../../state/models/session'
@ -256,7 +257,7 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
await store.session.createAccount({ await store.session.createAccount({
service: DEFAULT_SERVICE, service: DEFAULT_SERVICE,
email, email,
handle: `${handle}.${userDomain}`, handle: createFullHandle(handle, userDomain),
password, password,
inviteCode, inviteCode,
}) })
@ -264,15 +265,7 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
const errMsg = e.toString() const errMsg = e.toString()
console.log(e) console.log(e)
setIsProcessing(false) setIsProcessing(false)
// if (errMsg.includes('Authentication Required')) {
// setError('Invalid username or password')
// } else if (errMsg.includes('Network request failed')) {
// setError(
// 'Unable to contact your service. Please check your Internet connection.',
// )
// } else {
setError(errMsg.replace(/^Error:/, '')) setError(errMsg.replace(/^Error:/, ''))
// }
} }
} }
@ -380,7 +373,7 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
placeholderTextColor={colors.blue0} placeholderTextColor={colors.blue0}
autoCapitalize="none" autoCapitalize="none"
value={handle} value={handle}
onChangeText={v => setHandle(cleanUsername(v))} onChangeText={v => setHandle(makeValidHandle(v))}
editable={!isProcessing} editable={!isProcessing}
/> />
</View> </View>
@ -405,7 +398,7 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
<Text style={[s.white, s.p10]}> <Text style={[s.white, s.p10]}>
Your full username will be{' '} Your full username will be{' '}
<Text style={s.bold}> <Text style={s.bold}>
@{handle}.{userDomain} @{createFullHandle(handle, userDomain)}
</Text> </Text>
</Text> </Text>
</View> </View>
@ -431,14 +424,6 @@ const CreateAccount = ({onPressBack}: {onPressBack: () => void}) => {
) )
} }
function cleanUsername(v: string): string {
v = v.trim()
if (v.length > 63) {
v = v.slice(0, 63)
}
return v.toLowerCase().replace(/[^a-z0-9-]/g, '')
}
export const Login = observer( export const Login = observer(
(/*{navigation}: RootTabsScreenProps<'Login'>*/) => { (/*{navigation}: RootTabsScreenProps<'Login'>*/) => {
const [screenState, setScreenState] = useState<ScreenState>( const [screenState, setScreenState] = useState<ScreenState>(

View File

@ -36,7 +36,6 @@ Paul's todo list
- Follows list - Follows list
- Members list - Members list
- Bugs - Bugs
> Create account broken
> Auth token refresh seems broken > Auth token refresh seems broken
- Check that sub components arent reloading too much - Check that sub components arent reloading too much
- Titles are getting screwed up (possibly swipe related) - Titles are getting screwed up (possibly swipe related)