198 lines
6.3 KiB
TypeScript
198 lines
6.3 KiB
TypeScript
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 (
|
|
<div>
|
|
<div className="px-2 py-6">
|
|
<div className="mb-6 flex flex-row items-start justify-between">
|
|
<div className="flex flex-1 flex-row items-center">
|
|
<div
|
|
className="mr-4 flex h-12 w-12 items-center justify-center rounded-2xl"
|
|
style={{ backgroundColor: theme.colors.brand50 }}
|
|
>
|
|
<Users size={24} color={theme.colors.brand600} />
|
|
</div>
|
|
<div className="flex-1">
|
|
<MyText className="text-[10px] font-black uppercase tracking-widest text-slate-400">
|
|
Group Name
|
|
</MyText>
|
|
<MyText className="text-xl font-black text-slate-900" numberOfLines={1}>
|
|
{group.groupName}
|
|
</MyText>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex flex-row items-center">
|
|
<button
|
|
onClick={() => onEdit(group)}
|
|
className="mr-2 p-2"
|
|
>
|
|
<Pencil size={20} color="var(--color-slate-500)" />
|
|
</button>
|
|
<button
|
|
onClick={() => onDelete(group.id)}
|
|
className="p-2"
|
|
>
|
|
<Trash2 size={20} color="var(--color-red-500)" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{group.description && (
|
|
<div className="mb-6 rounded-2xl border border-slate-100 bg-slate-50 p-4">
|
|
<MyText className="font-medium text-slate-700">
|
|
{group.description}
|
|
</MyText>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex flex-row items-center justify-between">
|
|
<div className="flex flex-row items-center">
|
|
<button
|
|
onClick={() => onViewProducts(group.products)}
|
|
className="mr-4 flex flex-row items-center"
|
|
>
|
|
<Package size={14} color={theme.colors.brand500} />
|
|
<MyText className="ml-1.5 text-xs font-bold text-blue-600">
|
|
{group.productCount} Products
|
|
</MyText>
|
|
</button>
|
|
<div className="flex flex-row items-center">
|
|
<Clock size={14} color="var(--color-slate-400)" />
|
|
<MyText className="ml-1.5 text-xs font-bold text-slate-500">
|
|
{dayjs(group.createdAt).format('MMM DD, YYYY')}
|
|
</MyText>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
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<any[] | null>(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 (
|
|
<AppContainer>
|
|
<div className="flex flex-1 items-center justify-center">
|
|
<MyText className="text-gray-600">Loading product groups...</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<AppContainer>
|
|
<div className="flex flex-1 items-center justify-center">
|
|
<MyText className="text-red-600">Error loading groups</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="relative flex-1 bg-white px-4">
|
|
<div className="flex-1 overflow-y-auto pb-32">
|
|
{groups.length === 0 ? (
|
|
<div className="flex flex-1 flex-col items-center justify-center py-8">
|
|
<div className="mb-6 flex h-24 w-24 items-center justify-center rounded-full bg-slate-50">
|
|
<GroupIcon size={48} color="var(--color-slate-400)" />
|
|
</div>
|
|
<MyText className="text-xl font-black tracking-tight text-slate-900">
|
|
No Groups Yet
|
|
</MyText>
|
|
<MyText className="mt-2 px-8 text-center font-medium text-slate-500">
|
|
Start by creating your first product group using the button below.
|
|
</MyText>
|
|
</div>
|
|
) : (
|
|
groups.map((group, index) => (
|
|
<React.Fragment key={group.id}>
|
|
<GroupItem
|
|
group={group as ProductGroup}
|
|
index={index}
|
|
onEdit={handleEdit}
|
|
onDelete={handleDelete}
|
|
onViewProducts={setViewProducts}
|
|
/>
|
|
{index < groups.length - 1 && (
|
|
<div className="h-px w-full bg-slate-200" />
|
|
)}
|
|
</React.Fragment>
|
|
))
|
|
)}
|
|
</div>
|
|
<button
|
|
onClick={handleCreate}
|
|
className="fixed z-[100] flex h-16 w-16 items-center justify-center rounded-[24px] shadow-lg"
|
|
style={{ bottom: 32, right: 24, background: 'linear-gradient(135deg, var(--color-brand-600), var(--color-brand-900))' }}
|
|
aria-label="Create group"
|
|
>
|
|
<Plus size={32} color="white" />
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|