421 lines
13 KiB
TypeScript
421 lines
13 KiB
TypeScript
import React, { useState } from 'react'
|
|
import { createFileRoute } from '@tanstack/react-router'
|
|
import { AppContainer, p as MyText, div as MyTouchableOpacity } from 'web-components'
|
|
import { useManualRefresh, useMarkDataFetchers } from '@/lib/refresh-context'
|
|
import { theme } from '@/lib/theme'
|
|
import { trpc, getTrpcClient } from '@/lib/trpc-client'
|
|
import { Fingerprint, CalendarCheck, ShoppingBag, Link2, ChevronRight, Plus, Code2 } from 'lucide-react'
|
|
import dayjs from 'dayjs'
|
|
import VendorSnippetForm from '../components/VendorSnippetForm'
|
|
import SnippetOrdersView from '../components/SnippetOrdersView'
|
|
import { SnippetMenu } from '../components/SnippetMenu'
|
|
import { ProductListDialog } from '../components/ProductListDialog'
|
|
import type {
|
|
VendorSnippet,
|
|
VendorSnippetProduct,
|
|
VendorSnippetForm as VendorSnippetFormType,
|
|
} from '@/types/vendor-snippets'
|
|
|
|
export const Route = createFileRoute('/dashboard/vendor-snippets')({
|
|
component: VendorSnippets,
|
|
})
|
|
|
|
const SnippetItem = ({
|
|
snippet,
|
|
onEdit,
|
|
onDelete,
|
|
onViewOrders,
|
|
onViewProducts,
|
|
index,
|
|
}: {
|
|
snippet: VendorSnippet
|
|
onEdit: (snippet: VendorSnippet) => void
|
|
onDelete: (id: number) => void
|
|
onViewOrders: (snippetCode: string) => void
|
|
onViewProducts: (products: VendorSnippetProduct[]) => void
|
|
index: number
|
|
}) => {
|
|
|
|
const isExpired =
|
|
snippet.validTill && dayjs(snippet.validTill).isBefore(dayjs())
|
|
|
|
const handleCopyLink = async () => {
|
|
try {
|
|
if (!snippet.accessUrl) {
|
|
window.alert('Error: No link available')
|
|
return
|
|
}
|
|
if (typeof navigator.share === 'function') {
|
|
await navigator.share({ text: snippet.accessUrl })
|
|
} else {
|
|
await navigator.clipboard.writeText(snippet.accessUrl)
|
|
window.alert('Link copied to clipboard')
|
|
}
|
|
} catch (error: any) {
|
|
if (error?.name === 'AbortError') return
|
|
window.alert('Error: Failed to share link')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<div className="p-6">
|
|
<div className="mb-6 flex-row flex items-start justify-between">
|
|
<div className="flex-row flex flex-1 items-center">
|
|
<div
|
|
className="mr-4 flex h-12 w-12 items-center justify-center rounded-2xl"
|
|
style={{ backgroundColor: theme.colors.brand50 }}
|
|
>
|
|
<Fingerprint
|
|
size={24}
|
|
color={theme.colors.brand600}
|
|
/>
|
|
</div>
|
|
<div className="flex-1">
|
|
<MyText
|
|
className="text-[10px] font-black uppercase tracking-widest text-slate-400"
|
|
>
|
|
Identifier
|
|
</MyText>
|
|
<MyText
|
|
className="text-xl font-black text-slate-900"
|
|
numberOfLines={1}
|
|
>
|
|
{snippet.snippetCode}
|
|
</MyText>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex-row flex items-center">
|
|
<div
|
|
className="mr-2 flex-row flex items-center rounded-full px-3 py-1.5"
|
|
style={{ backgroundColor: isExpired ? '#FFF1F2' : '#F0FDF4' }}
|
|
>
|
|
<span
|
|
className="mr-2 h-1.5 w-1.5 rounded-full"
|
|
style={{ backgroundColor: isExpired ? '#E11D48' : '#10B981' }}
|
|
/>
|
|
<MyText
|
|
className="text-[10px] font-black uppercase tracking-tighter"
|
|
style={{ color: isExpired ? '#E11D48' : '#10B981' }}
|
|
>
|
|
{isExpired ? 'Expired' : 'Active'}
|
|
</MyText>
|
|
</div>
|
|
<SnippetMenu
|
|
snippet={snippet}
|
|
onEdit={onEdit}
|
|
onDelete={onDelete}
|
|
onViewOrders={onViewOrders}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
className="mb-6 flex-row flex items-center rounded-3xl border border-slate-100 bg-slate-50 p-4"
|
|
>
|
|
<div
|
|
className="flex h-10 w-10 items-center justify-center rounded-2xl bg-white shadow-sm"
|
|
>
|
|
<CalendarCheck size={20} color="#64748B" />
|
|
</div>
|
|
<div className="ml-4 flex-1">
|
|
{snippet.isPermanent ? (
|
|
<>
|
|
<MyText className="text-sm font-extrabold text-slate-900">
|
|
Permanent
|
|
</MyText>
|
|
<MyText className="text-[10px] font-bold uppercase text-slate-500">
|
|
Always Active
|
|
</MyText>
|
|
</>
|
|
) : snippet.slot?.deliveryTime ? (
|
|
<>
|
|
<MyText className="text-sm font-extrabold text-slate-900">
|
|
{dayjs(snippet.slot.deliveryTime).format('ddd, MMM DD')}
|
|
</MyText>
|
|
<MyText className="text-[10px] font-bold uppercase text-slate-500">
|
|
Time: {dayjs(snippet.slot.deliveryTime).format('hh:mm A')}
|
|
</MyText>
|
|
</>
|
|
) : (
|
|
<>
|
|
<MyText className="text-sm font-extrabold text-slate-900">
|
|
Schedule Pending
|
|
</MyText>
|
|
<MyText className="text-[10px] font-bold uppercase text-slate-500">
|
|
N/A
|
|
</MyText>
|
|
</>
|
|
)}
|
|
</div>
|
|
{snippet.validTill && (
|
|
<div className="items-end">
|
|
<MyText className="text-[9px] font-bold uppercase text-slate-400">
|
|
Expires
|
|
</MyText>
|
|
<MyText
|
|
className="text-xs font-black"
|
|
style={{ color: isExpired ? '#E11D48' : '#64748B' }}
|
|
>
|
|
{dayjs(snippet.validTill).format('MMM DD')}
|
|
</MyText>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex-row flex items-center justify-between">
|
|
<div className="flex-row flex items-center">
|
|
<MyTouchableOpacity
|
|
onClick={() => onViewProducts(snippet.products)}
|
|
className="mr-4 flex-row items-center"
|
|
>
|
|
<ShoppingBag size={14} color="#94A3B8" />
|
|
<MyText className="ml-1.5 text-xs font-bold text-slate-500">
|
|
{snippet.skuIds.length} Items
|
|
</MyText>
|
|
<ChevronRight size={14} color="#94A3B8" />
|
|
</MyTouchableOpacity>
|
|
<MyTouchableOpacity
|
|
onClick={handleCopyLink}
|
|
className="flex-row items-center"
|
|
>
|
|
<Link2
|
|
size={16}
|
|
color={theme.colors.brand500}
|
|
/>
|
|
<MyText
|
|
className="ml-1 text-xs font-black"
|
|
style={{ color: theme.colors.brand600 }}
|
|
>
|
|
Share
|
|
</MyText>
|
|
</MyTouchableOpacity>
|
|
</div>
|
|
|
|
<MyTouchableOpacity
|
|
onClick={() => {}}
|
|
activeOpacity={0.7}
|
|
className="flex-row items-center"
|
|
>
|
|
<MyText
|
|
className="text-xs font-black uppercase tracking-widest text-slate-900"
|
|
>
|
|
View Slot
|
|
</MyText>
|
|
<ChevronRight size={16} color="#000" />
|
|
</MyTouchableOpacity>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function VendorSnippets() {
|
|
const {
|
|
data: snippets,
|
|
isLoading,
|
|
error,
|
|
refetch,
|
|
} = trpc.admin.vendorSnippets.getAll.useQuery()
|
|
|
|
const createSnippet = trpc.admin.vendorSnippets.create.useMutation()
|
|
const updateSnippet = trpc.admin.vendorSnippets.update.useMutation()
|
|
const deleteSnippet = trpc.admin.vendorSnippets.delete.useMutation()
|
|
|
|
void createSnippet
|
|
void updateSnippet
|
|
|
|
const [showCreateForm, setShowCreateForm] = useState(false)
|
|
const [editingSnippet, setEditingSnippet] =
|
|
useState<VendorSnippetFormType | null>(null)
|
|
const [showOrdersView, setShowOrdersView] = useState(false)
|
|
const [ordersData, setOrdersData] = useState<any>(null)
|
|
const [showProductList, setShowProductList] = useState(false)
|
|
const [selectedProducts, setSelectedProducts] = useState<VendorSnippetProduct[] | null>(null)
|
|
|
|
useManualRefresh(() => {
|
|
void refetch()
|
|
})
|
|
|
|
useMarkDataFetchers(() => {
|
|
refetch()
|
|
})
|
|
|
|
const handleCreate = () => {
|
|
setShowCreateForm(true)
|
|
setEditingSnippet(null)
|
|
}
|
|
|
|
const handleViewProducts = (products: VendorSnippetProduct[]) => {
|
|
setSelectedProducts(products)
|
|
setShowProductList(true)
|
|
}
|
|
|
|
const handleEdit = (snippet: VendorSnippet) => {
|
|
const formSnippet: VendorSnippetFormType = {
|
|
id: snippet.id,
|
|
snippetCode: snippet.snippetCode,
|
|
slotId: snippet.slotId || 0,
|
|
isPermanent: snippet.isPermanent,
|
|
skuIds: snippet.skuIds,
|
|
validTill: snippet.validTill,
|
|
createdAt: snippet.createdAt,
|
|
}
|
|
setEditingSnippet(formSnippet)
|
|
setShowCreateForm(true)
|
|
}
|
|
|
|
const handleDelete = (id: number) => {
|
|
deleteSnippet.mutate(
|
|
{ id },
|
|
{
|
|
onSuccess: () => {
|
|
refetch()
|
|
},
|
|
},
|
|
)
|
|
}
|
|
|
|
const handleViewOrders = async (snippetCode: string) => {
|
|
try {
|
|
const trpcClient = getTrpcClient()
|
|
const result =
|
|
await trpcClient.admin.vendorSnippets.getOrdersBySnippet.query({
|
|
snippetCode,
|
|
})
|
|
if (result.success) {
|
|
setOrdersData({
|
|
orders: result.data,
|
|
snippetCode: snippetCode,
|
|
})
|
|
setShowOrdersView(true)
|
|
} else {
|
|
window.alert('Error: Failed to fetch orders')
|
|
}
|
|
} catch (error: any) {
|
|
window.alert(`Error: ${error.message || 'Failed to fetch orders'}`)
|
|
}
|
|
}
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<AppContainer>
|
|
<div className="flex flex-1 items-center justify-center">
|
|
<MyText className="text-gray-600">Loading vendor snippets...</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
if (error) {
|
|
return (
|
|
<AppContainer>
|
|
<div className="flex flex-1 items-center justify-center">
|
|
<MyText className="text-red-600">Error loading snippets</MyText>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|
|
|
|
if (showOrdersView && ordersData) {
|
|
return (
|
|
<SnippetOrdersView
|
|
orders={ordersData.orders}
|
|
snippetCode={ordersData.snippetCode}
|
|
onClose={() => {
|
|
setShowOrdersView(false)
|
|
setOrdersData(null)
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
|
|
if (showCreateForm) {
|
|
return (
|
|
<VendorSnippetForm
|
|
snippet={editingSnippet}
|
|
onClose={() => {
|
|
setShowCreateForm(false)
|
|
setEditingSnippet(null)
|
|
}}
|
|
onSuccess={() => {
|
|
setShowCreateForm(false)
|
|
setEditingSnippet(null)
|
|
refetch()
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<AppContainer>
|
|
<div className="h-full flex-1 bg-white">
|
|
<div
|
|
className="flex-1 pb-32 pt-2"
|
|
>
|
|
{snippets && snippets.length === 0 ? (
|
|
<div className="flex flex-1 items-center justify-center py-20">
|
|
<div
|
|
className="mb-6 flex h-24 w-24 items-center justify-center rounded-full bg-slate-50"
|
|
>
|
|
<Code2 size={48} color="#94A3B8" />
|
|
</div>
|
|
<MyText
|
|
className="text-xl font-black tracking-tight text-slate-900"
|
|
>
|
|
No Snippets Yet
|
|
</MyText>
|
|
<MyText
|
|
className="mt-2 px-8 text-center font-medium text-slate-500"
|
|
>
|
|
Start by creating your first vendor identifier using the
|
|
button below.
|
|
</MyText>
|
|
</div>
|
|
) : (
|
|
snippets?.map((snippet, index) => (
|
|
<React.Fragment key={snippet.id}>
|
|
<SnippetItem
|
|
snippet={snippet}
|
|
index={index}
|
|
onEdit={handleEdit}
|
|
onDelete={handleDelete}
|
|
onViewOrders={handleViewOrders}
|
|
onViewProducts={handleViewProducts}
|
|
/>
|
|
{index < snippets.length - 1 && (
|
|
<div className="h-px w-full bg-slate-200" />
|
|
)}
|
|
</React.Fragment>
|
|
))
|
|
)}
|
|
</div>
|
|
|
|
</div>
|
|
</AppContainer>
|
|
<MyTouchableOpacity
|
|
onClick={handleCreate}
|
|
activeOpacity={0.95}
|
|
className="fixed bottom-8 right-6 z-50 shadow-2xl"
|
|
aria-label="Create vendor snippet"
|
|
>
|
|
<span
|
|
className="flex h-16 w-16 items-center justify-center shadow-lg"
|
|
style={{
|
|
borderRadius: 24,
|
|
background: 'linear-gradient(135deg, #1570EF, #194185)',
|
|
}}
|
|
>
|
|
<Plus size={32} color="white" />
|
|
</span>
|
|
</MyTouchableOpacity>
|
|
<ProductListDialog
|
|
open={showProductList}
|
|
onClose={() => setShowProductList(false)}
|
|
products={selectedProducts || []}
|
|
/>
|
|
</>
|
|
)
|
|
}
|