Update category table to show number of bookmarks

master
TZGyn 2 years ago
parent 76d406881e
commit 2fc94fc27b
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -1,6 +1,4 @@
import { import { bookmarkCategoryWithBookmarksSchema } from '@/types'
bookmarkCategorySchema,
} from '@/types'
import { db } from '@/lib/db' import { db } from '@/lib/db'
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
import { getUser } from '@/lib/auth' import { getUser } from '@/lib/auth'
@ -17,9 +15,14 @@ export default async function CategoriesPage() {
const data = await db.query.bookmarkCategory.findMany({ const data = await db.query.bookmarkCategory.findMany({
where: (bookmarkCategory, { eq }) => where: (bookmarkCategory, { eq }) =>
eq(bookmarkCategory.userId, session_user ? session_user.id : 0), eq(bookmarkCategory.userId, session_user ? session_user.id : 0),
with: {
bookmark: true,
},
}) })
const bookmarkCategories = bookmarkCategorySchema.array().parse(data) const bookmarkCategories = bookmarkCategoryWithBookmarksSchema
.array()
.parse(data)
return ( return (
<div className='flex w-full max-w-4xl flex-row flex-wrap justify-start gap-6'> <div className='flex w-full max-w-4xl flex-row flex-wrap justify-start gap-6'>

@ -10,19 +10,20 @@ import {
Tooltip, Tooltip,
} from '@nextui-org/react' } from '@nextui-org/react'
import { DeleteIcon, EditIcon } from '@/components/icons' import { DeleteIcon, EditIcon } from '@/components/icons'
import { BookmarkCategory } from '@/types' import { BookmarkCategoryWithBookmarks } from '@/types'
import { useRouter } from 'next/navigation' import { useRouter } from 'next/navigation'
const columns = [ const columns = [
{ name: 'NAME', uid: 'name' }, { name: 'NAME', uid: 'name' },
{ name: 'BOOKMARK COUNT', uid: 'bookmarkcount' },
{ name: 'ACTIONS', uid: 'actions' }, { name: 'ACTIONS', uid: 'actions' },
] ]
export default function TableComponent({ export default function TableComponent({
bookmarkCategories, bookmarkCategories,
}: { }: {
bookmarkCategories: BookmarkCategory[] bookmarkCategories: BookmarkCategoryWithBookmarks[]
}) { }) {
const router = useRouter() const router = useRouter()
@ -54,6 +55,7 @@ export default function TableComponent({
{(item) => ( {(item) => (
<TableRow key={item.id}> <TableRow key={item.id}>
<TableCell>{item.name}</TableCell> <TableCell>{item.name}</TableCell>
<TableCell>{item.bookmark.length}</TableCell>
<TableCell> <TableCell>
<div className='relative flex items-center gap-2'> <div className='relative flex items-center gap-2'>
<Tooltip content='Edit Category'> <Tooltip content='Edit Category'>

Loading…
Cancel
Save