diff options
author | dan <dan.abramov@gmail.com> | 2023-11-15 01:55:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-14 17:55:54 -0800 |
commit | e699df21c66f2f55d34af4d2a14c03d02274b43e (patch) | |
tree | 3112b234b0870ababc3eb8c0834d90bb61bf8fee /src/view/com/profile/FollowButton.tsx | |
parent | d1cb74febea9725b028dca372e1b7d7e157ad24d (diff) | |
download | voidsky-e699df21c66f2f55d34af4d2a14c03d02274b43e.tar.zst |
Port Profile Followers/Follows to RQ (#1893)
* Port user followers to RQ * Port user follows to RQ * Start porting FollowButton to RQ * Fix RQ key * Check pending * Fix shadow and pending states * Rm unused * Remove last usage of useFollowProfile
Diffstat (limited to 'src/view/com/profile/FollowButton.tsx')
-rw-r--r-- | src/view/com/profile/FollowButton.tsx | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/src/view/com/profile/FollowButton.tsx b/src/view/com/profile/FollowButton.tsx index adb496f6d..032a910c7 100644 --- a/src/view/com/profile/FollowButton.tsx +++ b/src/view/com/profile/FollowButton.tsx @@ -1,47 +1,76 @@ import React from 'react' import {StyleProp, TextStyle, View} from 'react-native' -import {observer} from 'mobx-react-lite' import {AppBskyActorDefs} from '@atproto/api' import {Button, ButtonType} from '../util/forms/Button' import * as Toast from '../util/Toast' -import {FollowState} from 'state/models/cache/my-follows' -import {useFollowProfile} from 'lib/hooks/useFollowProfile' +import { + useProfileFollowMutation, + useProfileUnfollowMutation, +} from '#/state/queries/profile' +import {Shadow} from '#/state/cache/types' -export const FollowButton = observer(function FollowButtonImpl({ +export function FollowButton({ unfollowedType = 'inverted', followedType = 'default', profile, - onToggleFollow, labelStyle, }: { unfollowedType?: ButtonType followedType?: ButtonType - profile: AppBskyActorDefs.ProfileViewBasic - onToggleFollow?: (v: boolean) => void + profile: Shadow<AppBskyActorDefs.ProfileViewBasic> labelStyle?: StyleProp<TextStyle> }) { - const {state, following, toggle} = useFollowProfile(profile) + const followMutation = useProfileFollowMutation() + const unfollowMutation = useProfileUnfollowMutation() - const onPress = React.useCallback(async () => { + const onPressFollow = async () => { + if (profile.viewer?.following) { + return + } try { - const {following} = await toggle() - onToggleFollow?.(following) + await followMutation.mutateAsync({did: profile.did}) } catch (e: any) { - Toast.show('An issue occurred, please try again.') + Toast.show(`An issue occurred, please try again.`) } - }, [toggle, onToggleFollow]) + } - if (state === FollowState.Unknown) { + const onPressUnfollow = async () => { + if (!profile.viewer?.following) { + return + } + try { + await unfollowMutation.mutateAsync({ + did: profile.did, + followUri: profile.viewer?.following, + }) + } catch (e: any) { + Toast.show(`An issue occurred, please try again.`) + } + } + + if (!profile.viewer) { return <View /> } - return ( - <Button - type={following ? followedType : unfollowedType} - labelStyle={labelStyle} - onPress={onPress} - label={following ? 'Unfollow' : 'Follow'} - withLoading={true} - /> - ) -}) + if (profile.viewer.following) { + return ( + <Button + type={followedType} + labelStyle={labelStyle} + onPress={onPressUnfollow} + label="Unfollow" + withLoading={true} + /> + ) + } else { + return ( + <Button + type={unfollowedType} + labelStyle={labelStyle} + onPress={onPressFollow} + label="Follow" + withLoading={true} + /> + ) + } +} |