diff --git a/src/app/[locale]/(user)/user/[profileUrl]/page.client.tsx b/src/app/[locale]/(user)/user/[profileUrl]/page.client.tsx index f244d89b..494ef34b 100644 --- a/src/app/[locale]/(user)/user/[profileUrl]/page.client.tsx +++ b/src/app/[locale]/(user)/user/[profileUrl]/page.client.tsx @@ -1,74 +1,47 @@ 'use client' import { Button } from '@/components/ui/button' -import { useToast } from '@/components/ui/use-toast' import { addFollow } from '@/utils/actions/followers/addFollow' import { useState } from 'react' import { removeFollow } from '@/utils/actions/followers/removeFollow' +import { toast } from 'sonner' -export function FollowerButton({ - displayName, - userId, - followerId, - isFollowing, -}) { - const { toast } = useToast() +export function FollowerButton({ displayName, followerId, isFollowing }) { const [isFollowingState, setIsFollowingState] = useState(isFollowing || false) const handleFollow = async () => { - const data = await addFollow(userId, followerId) - console.log(data) - if (data.code === 401) { - return toast({ - title: 'Error', - description: 'You must be logged in to follow users', - variant: 'destructive', - }) - } else if (data.code === 409) { - return toast({ - title: 'Error', - description: 'You cannot follow yourself', - variant: 'destructive', - }) - } else if (data.code === 404) { - return toast({ - title: 'Error', - description: 'You are already following this user', - variant: 'destructive', - }) + const data = await addFollow(followerId) + + if (data.type === 'addfollow_missing_id') { + return toast.error('No user ID provided') + } else if (data.type === 'addfollow_unauthorized') { + return toast.error('You must be logged in to follow users') + } else if (data.type === 'addfollow_same_user') { + return toast.error('You cannot follow yourself') + } else if (data.type === 'addfollow_already_following') { + return toast.error('You are already following this user') + } else if (data.type === 'addfollow_delete_error') { + return toast.error('There was an error following this user.') } else { - toast({ - title: 'Follow added', - description: `You have followed ${displayName}`, - }) + toast.success(`You have followed ${displayName}`) setIsFollowingState(true) } } const handleUnfollow = async () => { - const data = await removeFollow(followerId, userId) - if (data.code === 401) { - return toast({ - title: 'Error', - description: 'You must be logged in to unfollow users', - variant: 'destructive', - }) - } else if (data.code === 409) { - return toast({ - title: 'Error', - description: 'You cannot unfollow yourself', - variant: 'destructive', - }) - } else if (data.code === 403) { - return toast({ - title: 'Error', - description: 'You are not following this user', - variant: 'destructive', - }) + const data = await removeFollow(followerId) + + if (data.type === 'removefollow_missing_id') { + return toast.error('No user ID provided') + } else if (data.type === 'removefollow_unauthorized') { + return toast.error('You must be logged in to follow users') + } else if (data.type === 'removefollow_same_user') { + return toast.error('You cannot unfollow yourself') + } else if (data.type === 'removefollow_not_following') { + return toast.error('You are not following this user') + } else if (data.type === 'removefollow_delete_error') { + return toast.error('There was an error unfollowing this user.') } else { - toast({ - title: 'Follow removed', - description: `You have unfollowed ${displayName}`, - }) + toast.success(`You have unfollowed ${displayName}`) setIsFollowingState(false) } } diff --git a/src/app/[locale]/(user)/user/[profileUrl]/page.tsx b/src/app/[locale]/(user)/user/[profileUrl]/page.tsx index 41e01ca4..5b4fecb0 100644 --- a/src/app/[locale]/(user)/user/[profileUrl]/page.tsx +++ b/src/app/[locale]/(user)/user/[profileUrl]/page.tsx @@ -216,7 +216,6 @@ export default async function UserProfile({ params: { profileUrl } }) { {userData.displayName} 0) { - return { code: 404 } - } - - return await databases.createDocument('hp_db', 'followers', ID.unique(), { - userId: account.$id, - followerId: followerId, - }) - } catch (error) { - return JSON.parse(JSON.stringify(error)) - } + return JSON.parse(data.responseBody) } diff --git a/src/utils/actions/followers/removeFollow.ts b/src/utils/actions/followers/removeFollow.ts index 32aff2af..393e7f21 100644 --- a/src/utils/actions/followers/removeFollow.ts +++ b/src/utils/actions/followers/removeFollow.ts @@ -1,31 +1,16 @@ -'use server' +'use client' -import { createSessionServerClient } from '@/app/appwrite-session' -import { getUser } from '@/utils/server-api/account/user' -import { getIsFollowing } from '@/utils/server-api/followers/getFollowing' +import { functions } from '@/app/appwrite-client' +import { ExecutionMethod } from 'node-appwrite' -export async function removeFollow(followerId: string, userId: string) { - try { - const { databases } = await createSessionServerClient() - const account = await getUser() +export async function removeFollow(followerId: string) { + const data = await functions.createExecution( + 'user-endpoints', + '', + false, + `/user/follow?followerId=${followerId}`, + ExecutionMethod.DELETE + ) - if (!account.$id) { - return { code: 401 } - } - if (account.$id === followerId) { - return { code: 409 } - } - const following = await getIsFollowing(userId, followerId) - if (following.documents.length === 0) { - return { code: 403 } - } - - return await databases.deleteDocument( - 'hp_db', - 'followers', - following.documents[0].$id - ) - } catch (error) { - return JSON.parse(JSON.stringify(error)) - } + return JSON.parse(data.responseBody) }