Advertisement
AlexAnker

FriendRequests.tsx

Apr 2nd, 2024
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //external interfaces
  2. profile{  
  3.     id: string;
  4.     username: string | null;
  5.     avatarUrl: string | null;
  6.     email: string;
  7. }
  8.  
  9. friendRequest{
  10.     id:string
  11.     user:profile
  12. }
  13.  
  14. //friendRequestActions.tsx
  15. export async function acceptFriendRequest(id: string) {
  16.     await db.update(friendships)
  17.         .set({ status: "accepted" })
  18.         .where(eq(friendships.id, id));
  19.  
  20.     revalidatePath("/")
  21. }
  22.  
  23. export async function rejectFriendRequest(id: string) {
  24.     await db.delete(friendships).where(eq(friendships.id, id))
  25.  
  26.     revalidatePath("/")
  27. }
  28.  
  29. //friendRequests.tsx
  30. "use client"
  31. import { Button } from '@/components/ui/button'
  32. import { acceptFriendRequest, friendRequest, rejectFriendRequest } from '@/app/addfriend/actions'
  33. import { Check, X } from 'lucide-react'
  34. import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
  35. import { useState } from 'react'
  36.  
  37. interface Props {
  38.     requests: Array<friendRequest> | undefined
  39. }
  40.  
  41. export default function FriendRequests(props: Props) {
  42.     const [requests, setRequests] = useState(props.requests);
  43.  
  44.     if (requests === undefined || requests.length <= 0) {
  45.         return <h2 className="text-sm font-semibold">no pending friend requests</h2>
  46.     }
  47.  
  48.     return (
  49.         <div>
  50.             {requests.map((request) => {
  51.                 return <div key={request.id} className='flex justify-between'>
  52.                     <div className='flex items-center gap-4'>
  53.                         <Avatar>
  54.                             <AvatarImage src={request.profile.avatarUrl ?? undefined} />
  55.                             <AvatarFallback>{request.profile.username!.substring(0, 2)}</AvatarFallback>
  56.                         </Avatar>
  57.                         {request.profile.username}
  58.                     </div>
  59.                     <div className='flex items-center gap-4' >
  60.                         <Button size="icon" variant={"secondary"} className={"rounded-full w-6 h-6"} onClick={async () => {
  61.                             setRequests(requests.filter(r => r.id !== request.id))
  62.                             await rejectFriendRequest(request.id);
  63.                         }}><X className='w-4 h-4' /></Button>
  64.  
  65.                         <Button size="icon" variant={"default"} className={"rounded-full w-6 h-6"} onClick={async () => {
  66.                             setRequests(requests.filter(r => r.id !== request.id))
  67.                             await acceptFriendRequest(request.id);
  68.                         }}><Check className='w-4 h-4' /></Button>
  69.                     </div>
  70.                 </div>
  71.             })}
  72.         </div>
  73.     )
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement