Skip to content

Commit

Permalink
change to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Aug 26, 2024
1 parent d7573bd commit 786d22f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 110 deletions.
83 changes: 28 additions & 55 deletions src/app/[locale]/(user)/user/[profileUrl]/page.client.tsx
Original file line number Diff line number Diff line change
@@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/app/[locale]/(user)/user/[profileUrl]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ export default async function UserProfile({ params: { profileUrl } }) {
{userData.displayName}
</CardTitle>
<FollowerButton
userId={account?.$id}
displayName={userData?.displayName}
followerId={userData.$id}
isFollowing={isFollowing}
Expand Down
39 changes: 12 additions & 27 deletions src/utils/actions/followers/addFollow.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
'use server'
'use client'

import { createSessionServerClient } from '@/app/appwrite-session'
import { ID } from 'node-appwrite'
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 addFollow(userId: string, followerId: string) {
try {
const { databases } = await createSessionServerClient()
const account = await getUser()
export async function addFollow(followerId: string) {
const data = await functions.createExecution(
'user-endpoints',
'',
false,
`/user/follow?followerId=${followerId}`,
ExecutionMethod.POST
)

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: 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)
}
39 changes: 12 additions & 27 deletions src/utils/actions/followers/removeFollow.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 786d22f

Please sign in to comment.