Fix double-posting bug

zio/stable
Paul Frazee 2022-11-14 15:10:34 -06:00
parent ef071c0915
commit 4a0b79da4a
2 changed files with 31 additions and 17 deletions

View File

@ -1,5 +1,12 @@
import React, {useEffect, useMemo, useState} from 'react' import React, {useEffect, useMemo, useState} from 'react'
import {StyleSheet, Text, TextInput, TouchableOpacity, View} from 'react-native' import {
ActivityIndicator,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native'
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 * as GetFollows from '../../../third-party/api/src/client/types/app/bsky/graph/getFollows' import * as GetFollows from '../../../third-party/api/src/client/types/app/bsky/graph/getFollows'
@ -25,6 +32,7 @@ export function ComposePost({
onClose: () => void onClose: () => void
}) { }) {
const store = useStores() const store = useStores()
const [isProcessing, setIsProcessing] = useState(false)
const [error, setError] = useState('') const [error, setError] = useState('')
const [text, setText] = useState('') const [text, setText] = useState('')
const [followedUsers, setFollowedUsers] = useState< const [followedUsers, setFollowedUsers] = useState<
@ -70,11 +78,15 @@ export function ComposePost({
onClose() onClose()
} }
const onPressPublish = async () => { const onPressPublish = async () => {
if (isProcessing) {
return
}
setError('') setError('')
if (text.trim().length === 0) { if (text.trim().length === 0) {
setError('Did you want to say anything?') setError('Did you want to say anything?')
return false return false
} }
setIsProcessing(true)
try { try {
await apilib.post(store, text, replyTo) await apilib.post(store, text, replyTo)
} catch (e: any) { } catch (e: any) {
@ -82,6 +94,7 @@ export function ComposePost({
setError( setError(
'Post failed to upload. Please check your Internet connection and try again.', 'Post failed to upload. Please check your Internet connection and try again.',
) )
setIsProcessing(false)
return return
} }
onPost?.() onPost?.()
@ -126,15 +139,21 @@ export function ComposePost({
<Text style={[s.blue3, s.f16]}>Cancel</Text> <Text style={[s.blue3, s.f16]}>Cancel</Text>
</TouchableOpacity> </TouchableOpacity>
<View style={s.flex1} /> <View style={s.flex1} />
<TouchableOpacity onPress={onPressPublish}> {isProcessing ? (
<LinearGradient <View style={styles.postBtn}>
colors={[gradients.primary.start, gradients.primary.end]} <ActivityIndicator />
start={{x: 0, y: 0}} </View>
end={{x: 1, y: 1}} ) : (
style={styles.postBtn}> <TouchableOpacity onPress={onPressPublish}>
<Text style={[s.white, s.f16, s.bold]}>Post</Text> <LinearGradient
</LinearGradient> colors={[gradients.primary.start, gradients.primary.end]}
</TouchableOpacity> start={{x: 0, y: 0}}
end={{x: 1, y: 1}}
style={styles.postBtn}>
<Text style={[s.white, s.f16, s.bold]}>Post</Text>
</LinearGradient>
</TouchableOpacity>
)}
</View> </View>
{error !== '' && ( {error !== '' && (
<View style={styles.errorLine}> <View style={styles.errorLine}>

View File

@ -126,10 +126,8 @@ export function Component({}: {}) {
/> />
</View> </View>
{error !== '' && ( {error !== '' && (
<View style={styles.errorContainer}> <View style={s.mb10}>
<View style={s.mb10}> <ErrorMessage message={error} numberOfLines={3} />
<ErrorMessage message={error} numberOfLines={3} />
</View>
</View> </View>
)} )}
{handle.length >= 2 && !isProcessing ? ( {handle.length >= 2 && !isProcessing ? (
@ -209,9 +207,6 @@ const styles = StyleSheet.create({
height: 70, height: 70,
textAlignVertical: 'top', textAlignVertical: 'top',
}, },
errorContainer: {
height: 80,
},
btn: { btn: {
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center', alignItems: 'center',