import React, { useState } from 'react'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { Users, Pencil, Trash2, Package, Clock, Group as GroupIcon, Plus } from 'lucide-react'
import { AppContainer, p as MyText } from 'web-components'
import { useManualRefresh, useMarkDataFetchers } from '@/lib/refresh-context'
import { trpc } from '@/lib/trpc-client'
import { theme } from '@/lib/theme'
import dayjs from 'dayjs'
import type { ProductGroup } from '../components/ProductGroupForm'
export const Route = createFileRoute('/dashboard/product-groupings')({
component: ProductGroupings,
})
const GroupItem = ({
group,
onEdit,
onDelete,
onViewProducts,
index,
}: {
group: ProductGroup
onEdit: (group: ProductGroup) => void
onDelete: (id: number) => void
onViewProducts: (products: any[]) => void
index: number
}) => {
return (
Group Name
{group.groupName}
{group.description && (
{group.description}
)}
{dayjs(group.createdAt).format('MMM DD, YYYY')}
)
}
function ProductGroupings() {
const navigate = useNavigate()
const {
data: groupsData,
isLoading,
error,
refetch,
} = trpc.admin.product.getGroups.useQuery()
const deleteGroup = trpc.admin.product.deleteGroup.useMutation()
const groups = groupsData?.groups || []
const [viewProducts, setViewProducts] = useState(null)
useManualRefresh(refetch)
useMarkDataFetchers(() => {
refetch()
})
const handleCreate = () => {
navigate({ to: '/dashboard/product-groupings/new' })
}
const handleEdit = (group: ProductGroup) => {
navigate({ to: '/dashboard/product-groupings/$id/edit', params: { id: String(group.id) } })
}
const handleDelete = (id: number) => {
if (window.confirm('Are you sure you want to delete this group?')) {
deleteGroup.mutate(
{ id },
{
onSuccess: () => {
refetch()
},
}
)
}
}
if (isLoading) {
return (
Loading product groups...
)
}
if (error) {
return (
Error loading groups
)
}
return (
{groups.length === 0 ? (
No Groups Yet
Start by creating your first product group using the button below.
) : (
groups.map((group, index) => (
{index < groups.length - 1 && (
)}
))
)}
)
}