Skip to content

Commit

Permalink
feat: Use local state for follow button to improve responsiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-software committed Oct 20, 2021
1 parent 5f3f0c2 commit c1ed996
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
26 changes: 2 additions & 24 deletions src/screens/profile/generic-profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Callout,
Icons,
LazyListProps,
FollowButton,
} from "@greeneggs/ui";
import { useMutation, useQuery } from "@apollo/client";
import { useSafeAreaInsets } from "react-native-safe-area-context";
Expand Down Expand Up @@ -155,20 +156,6 @@ export const GenericProfile = ({
},
});

const [followUser] = useMutation<FollowUser>(Mutations.FOLLOW_USER, {
variables: {
userId,
},
refetchQueries: [Queries.GET_PROFILE, "profile"],
});

const [unfollowUser] = useMutation<FollowUser>(Mutations.UNFOLLOW_USER, {
variables: {
userId,
},
refetchQueries: [Queries.GET_PROFILE, "profile"],
});

const [myRecipeQuery, setMyRecipeQuery] = useState("");

if (profileResult.loading) {
Expand Down Expand Up @@ -239,16 +226,7 @@ export const GenericProfile = ({
EDIT
</Button>
) : (
<Button
size="small"
onPress={
profile.isFollowing
? () => unfollowUser()
: () => followUser()
}
>
{profile.isFollowing ? "UNFOLLOW" : "FOLLOW"}
</Button>
<FollowButton isFollowing={profile.isFollowing ?? false} userId={userId} />
)}
</View>
{profile.bio ? (
Expand Down
57 changes: 57 additions & 0 deletions src/ui/follow-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { FC, useState } from 'react';
import { Button } from '@ui-kitten/components';
import { useMutation } from '@apollo/client';
import { FollowUser, UnfollowUser } from '@greeneggs/types/graphql';
import { Mutations, Queries } from '@greeneggs/graphql';

interface FollowButtonProps {
userId: string;
isFollowing: boolean;

}

export const FollowButton: FC<FollowButtonProps> = ({ userId, isFollowing }) => {
const [isFollowingState, setIsFollowingState] = useState(isFollowing);
const [followUser] = useMutation<FollowUser>(Mutations.FOLLOW_USER, {
variables: {
userId,
},
refetchQueries: [Queries.GET_PROFILE, "profile"],
});

const [unfollowUser] = useMutation<UnfollowUser>(Mutations.UNFOLLOW_USER, {
variables: {
userId,
},
refetchQueries: [Queries.GET_PROFILE, "profile"],
});

function handleFollowUser() {
setIsFollowingState(true);
followUser().catch(() => {
// Undo local state change if mutation fails
setIsFollowingState(false);
});
}

function handleUnfollowUser() {
setIsFollowingState(false);
unfollowUser().catch(() => {
// Undo local state change if mutation fails
setIsFollowingState(true);
});
}

return (
<Button
size="small"
onPress={
isFollowingState
? () => handleUnfollowUser()
: () => handleFollowUser()
}
>
{isFollowingState ? "UNFOLLOW" : "FOLLOW"}
</Button>
)
}
3 changes: 2 additions & 1 deletion src/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export * from './counters/comment-like-counter';
export * from './counters/comment-counter';
export * from './save-recipe-button';
export * from './empty-state';
export * from './hide-on-keyboard';
export * from './hide-on-keyboard';
export * from './follow-button';

0 comments on commit c1ed996

Please sign in to comment.