import { Author, Book, Checkout, Copy, Repository } from '@/payload-types'
import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/react'
import { EllipsisVerticalIcon } from '@heroicons/react/20/solid'
import clsx from 'clsx'
import { Loader2Icon } from 'lucide-react'
import { PaginatedDocs, User } from 'payload'
import { useCallback, useState } from 'react'
const statuses = {
'Passed Due': 'text-gray-200 bg-red-500 ring-red-700/10',
'Due Soon': 'text-amber-800 bg-amber-50 ring-amber-600/20',
'': '',
}
type Row = {
id: number
title: string
authors: string[]
owners: string[]
repositoryAbbreviation: string
status: 'Passed Due' | 'Due Soon' | ''
dueDate: Date
href: string
}
type ListProps = { rows: Row[] }
const CheckedOutBooksList = (props: ListProps) => {
const { rows } = props
const [returningBookId, setReturningBookId] = useState(0)
const isReturningBook = useCallback((id: number) => id === returningBookId, [returningBookId])
const handleReturnClick = useCallback((id: number) => {
setReturningBookId(id)
}, [])
return (
{rows.map((r) => (
-
{`[${r.repositoryAbbreviation}] `}
{r.title}
{!!r.status && (
{r.status}
)}
Due on
Borrowed from {r.owners.join(', ')}
))}
)
}
type Props = { initialCheckoutPage: PaginatedDocs | null }
const CheckedOutBooks = (props: Props) => {
const { initialCheckoutPage } = props
const checkouts = initialCheckoutPage?.docs
const rows: Row[] =
checkouts?.map((c) => {
const copy = c.copy as Copy
const book = copy.book as Book
const authors = book.authors as Author[]
const repository = copy.repository as Repository
const owners = repository.owner as (number | User)[]
const parsedDateDue = c.dateDue ? new Date(c.dateDue) : new Date()
const milisecondsUntilDue = parsedDateDue.getTime() - new Date().getTime()
const secondsUntilDue = Math.floor(milisecondsUntilDue / 1000)
const minutesUntilDue = Math.floor(secondsUntilDue / 60)
const hoursUntilDue = Math.floor(minutesUntilDue / 60)
const daysUntilDue = Math.floor(hoursUntilDue / 24)
let status = ''
if (daysUntilDue <= 0) status = 'Passed Due'
else if (daysUntilDue <= 5) status = 'Due Soon'
return {
id: c.id || 0,
title: book.title || '',
authors: authors.map((a) => a.lf) || ([] as string[]),
owners:
owners.map((o) => `${(o as User).firstName} ${(o as User).lastName}`) || ([] as string[]),
repositoryAbbreviation: repository.abbreviation || '',
status,
dueDate: parsedDateDue,
href: `/books/${book.id}`,
} as Row
}) || []
if (!rows.length) return null
return (
Inbound Checked Out Books
)
}
export default CheckedOutBooks