Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: follow and unfollow #11

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions app/[displayName].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useEffect, useState} from 'react';
import styled from '@emotion/native';
import {Stack, useLocalSearchParams} from 'expo-router';
import {Icon, Typography, useDooboo} from 'dooboo-ui';
import {Button, Icon, Typography, useDooboo} from 'dooboo-ui';
import {css} from '@emotion/native';
import {Pressable} from 'react-native';
import ErrorBoundary from 'react-native-error-boundary';
Expand All @@ -13,6 +13,13 @@ import DoobooStats from '../src/components/fragments/DoobooStats';
import {t} from '../src/STRINGS';
import {fetchUserWithDisplayName} from '../src/apis/profileQueries';
import CustomLoadingIndicator from '../src/components/uis/CustomLoadingIndicator';
import {useRecoilValue} from 'recoil';
import {authRecoilState} from '../src/recoil/atoms';
import {
fetchFollowCounts,
fetchFollowUser,
fetchIsAFollowing,
} from '../src/apis/followQueries';

const Container = styled.SafeAreaView`
flex: 1;
Expand Down Expand Up @@ -108,17 +115,56 @@ export default function DisplayName(): JSX.Element {
displayName: string;
}>();
const {theme} = useDooboo();
const {authId} = useRecoilValue(authRecoilState);
const [user, setUser] = useState<any>(null);
const [tags, setTags] = useState<string[]>([]);
const [loading, setLoading] = useState(true);
const displayName = removeLeadingAt(displayNameWithLeading);
const [isFollowing, setIsFollowing] = useState(false);
const [followingCount, setFollowingCount] = useState(0);

const followUser = async () => {
try {
if (!authId || !user?.id) return;

const result = await fetchFollowUser({
authId,
followingId: user.id,
follow: !isFollowing,
});

if (result) {
setIsFollowing((prev) => !prev);
setFollowingCount(
isFollowing ? followingCount - 1 : followingCount + 1,
);
}
} catch (err: any) {
if (__DEV__) console.error(err.message);
}
};

useEffect(() => {
async function fetchData() {
try {
const {profile, userTags} = await fetchUserWithDisplayName(displayName);
setUser(profile);
setTags(userTags);

// Check if the current user is following this profile
if (authId) {
if (profile.id !== authId) {
const isUserFollowing = await fetchIsAFollowing({
authId,
followingId: profile.id,
});

setIsFollowing(isUserFollowing);
}

const followingsData = await fetchFollowCounts(authId); // 팔로우 수를 가져오는 API
setFollowingCount(followingsData.followingCount);
}
} catch (err: any) {
throw new Error(err.message);
} finally {
Expand All @@ -127,7 +173,7 @@ export default function DisplayName(): JSX.Element {
}

fetchData();
}, [displayName]);
}, [authId, displayName]);

if (loading) {
return (
Expand Down Expand Up @@ -157,6 +203,22 @@ export default function DisplayName(): JSX.Element {
{user?.introduction ? (
<UserBio>{user?.introduction}</UserBio>
) : null}
{authId && user?.id !== authId ? (
<Button
size="small"
borderRadius={30}
onPress={followUser}
color="secondary"
text={
isFollowing
? `${followingCount} ${t('common.followings', {
count: followingCount,
})}`
: `${t('common.follow')}`
}
type={isFollowing ? 'outlined' : 'solid'}
/>
) : null}
</ProfileHeader>
<Content>
<InfoCard>
Expand Down
49 changes: 49 additions & 0 deletions src/apis/followQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,52 @@ export async function fetchFollowings({
return false;
}
}

export async function fetchFollowCounts(
userId: string,
): Promise<{followingCount: number; followerCount: number}> {
if (!userId) {
throw new Error('ERR_NOT_AUTHORIZED');
}

try {
// Following count (user is following others)
const {count: followingCount, error: followingError} = await supabase
.from('follows')
.select('id', {count: 'exact'})
.eq('user_id', userId);

if (followingError) {
if (__DEV__) {
console.error(followingError.message);
}
throw new Error(followingError.message);
}

// Follower count (others are following user)
const {count: followerCount, error: followerError} = await supabase
.from('follows')
.select('id', {count: 'exact'})
.eq('following_id', userId);

if (followerError) {
if (__DEV__) {
console.error(followerError.message);
}
throw new Error(followerError.message);
}

return {
followingCount: followingCount || 0,
followerCount: followerCount || 0,
};
} catch (err: any) {
if (__DEV__) {
console.error(err);
}
return {
followingCount: 0,
followerCount: 0,
};
}
}
Loading