import type { Book, Checkout, HoldRequest, Repository, User } from '@/payload-types' import { getPayload, PaginatedDocs } from 'payload' import config from '@/payload.config' import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '../ui/card' import Image from 'next/image' import clsx from 'clsx' import Link from 'next/link' import { LoginForm } from '../login-form' type Props = { user?: User repos: PaginatedDocs | null } const UserFeed = async (props: Props) => { const { user, repos } = props const isLoggedIn = !!user if (!isLoggedIn) return (
) const payloadConfig = await config const payload = await getPayload({ config: payloadConfig }) const borrowRequests = (await payload.find({ collection: 'holdRequests', limit: 10, depth: 3, select: { copy: true, dateRequested: true, repository: true, book: true, }, where: { userRequested: { equals: user?.id, }, isCheckedOut: { not_equals: true, }, isRejected: { not_equals: true, }, }, })) as PaginatedDocs const currentlyHeldBooks = (await payload.find({ collection: 'holdRequests', limit: 10, depth: 3, select: { copy: true, dateRequested: true, repository: true, book: true, }, where: { 'repository.owner': { equals: user?.id, }, isHolding: { equals: true, }, }, })) as PaginatedDocs let loanedOutBooks: PaginatedDocs | null = null if (user.id) loanedOutBooks = (await payload.find({ collection: 'checkouts', limit: 10, depth: 2, select: { book: true, dateDue: true, loanerReturnedDate: true, isReturned: true, ownerVerifiedReturnedDate: true, }, where: { and: [ { 'copy.repository.owner': { equals: user.id, }, }, { isReturned: { not_equals: true, }, }, ], }, })) as PaginatedDocs const totalHoldNotifications = repos?.docs .flatMap((r) => r.holdRequests?.docs) .filter((r) => { const repo = r as HoldRequest return !repo.isRejected && !repo.isCheckedOut }).length || 0 const outBoundLoanCount = loanedOutBooks?.totalDocs || 0 const currentlyHoldingCount = currentlyHeldBooks?.totalDocs || 0 const stats = [ { name: 'Hold Request', iconSrc: '/images/mail.svg', value: totalHoldNotifications, href: '/manage', ctaText: 'Answer Requests', shouldAnimate: totalHoldNotifications > 0, }, { name: 'Loaned Out', iconSrc: '/images/book-loan.svg', value: outBoundLoanCount, href: '/manage', ctaText: 'Handle Returns', shouldAnimate: false, }, { name: 'Currently Holding', iconSrc: '/images/book-shelf.svg', value: currentlyHoldingCount, href: '/manage', ctaText: 'Loan Out', shouldAnimate: false, }, ] return (

Loan Activity

{stats.map((s) => { return (
{s.name}

{s.name}

{s.value}

) })}

Borrow Activity

Your Holds

    {borrowRequests.docs?.map((h) => { const book = h.book as Book const repository = h.repository as Repository const formatedDateRequested = h.dateRequested ? new Date(h.dateRequested).toDateString() : '' return (
  • {book.title} {repository.abbreviation} book cover Requested On {formatedDateRequested}
  • ) })}
) } export default UserFeed