import React, { useState, useEffect, useMemo } from 'react' import { createFileRoute, useNavigate } from '@tanstack/react-router' import { Save, Pencil, EllipsisVertical, Shuffle } from 'lucide-react' import { AppContainer, p as MyText, pInput as MyTextInput, BottomDialog, Checkbox, MyFlatList, } from 'web-components' import MultiSelect from '@/components/MultiSelect' import { trpc } from '@/lib/trpc-client' export const Route = createFileRoute('/dashboard/prices')({ component: PricesOverview, }) interface SkuItemProps { sku: any productName: string hasChanges: (skuId: number) => boolean pendingChanges: Record setPendingChanges: React.Dispatch>> openEditDialog: (sku: any, productName: string) => void } const SkuItemComponent: React.FC = ({ sku, productName, hasChanges, pendingChanges, setPendingChanges, openEditDialog, }) => { const changed = hasChanges(sku.id) const change = pendingChanges[sku.id] || {} const displayPrice = change.price !== undefined ? change.price : sku.price const displayMarketPrice = change.marketPrice !== undefined ? change.marketPrice : sku.marketPrice const displayFlashPrice = change.flashPrice !== undefined ? change.flashPrice : sku.flashPrice const displayUnit = (sku.features || []).map((f: any) => f.featureValue).join(' ') || '—' return (
{changed && ( )}
{productName.length > 20 ? productName.substring(0, 20) + '...' : productName} {sku.displayName || sku.name || ''}
{ const currentValue = change.isFlashAvailable ?? sku.isFlashAvailable ?? false setPendingChanges((prev) => ({ ...prev, [sku.id]: { ...prev[sku.id], isFlashAvailable: !currentValue, }, })) }} /> Flash
Our Price
₹{displayPrice}
Market Price
{displayMarketPrice ? `₹${displayMarketPrice}` : 'N/A'}
Flash Price
{displayFlashPrice ? `₹${displayFlashPrice}` : 'N/A'}
Size
{displayUnit}
) } interface PendingChange { price?: number marketPrice?: number | null flashPrice?: number | null isFlashAvailable?: boolean } interface EditDialogState { open: boolean sku: any productName: string tempPrice: string tempMarketPrice: string tempFlashPrice: string } function PricesOverview() { const navigate = useNavigate() const [selectedStores, setSelectedStores] = useState([]) const [pendingChanges, setPendingChanges] = useState>({}) const [editDialog, setEditDialog] = useState({ open: false, sku: null, productName: '', tempPrice: '', tempMarketPrice: '', tempFlashPrice: '', }) const [showMenu, setShowMenu] = useState(false) const { data: productsData, isLoading: productsLoading, refetch: refetchProducts } = trpc.admin.product.getProducts.useQuery() const { data: storesData, isLoading: storesLoading } = trpc.admin.store.getStores.useQuery() const updatePricesMutation = trpc.admin.product.updateProductPrices.useMutation() const stores = storesData?.stores || [] const allProducts = productsData?.products || [] const sortedStores = useMemo(() => [...stores].sort((a, b) => a.name.localeCompare(b.name)), [stores] ) const storeOptions = useMemo(() => sortedStores.map((store) => ({ label: store.name, value: store.id.toString(), })), [sortedStores] ) useEffect(() => { if (stores.length > 0 && selectedStores.length === 0) { setSelectedStores(stores.map((s) => s.id.toString())) } }, [stores, selectedStores]) const allSkus = useMemo(() => { const skus: any[] = [] for (const product of allProducts) { if (product.skus && product.skus.length > 0) { for (const sku of product.skus) { skus.push({ ...sku, _productName: product.name, _storeId: product.storeId }) } } } return skus }, [allProducts]) const filteredSkus = useMemo(() => { if (selectedStores.length === 0) return allSkus return allSkus.filter((sku) => sku._storeId && selectedStores.includes(sku._storeId.toString()) ) }, [allSkus, selectedStores]) const hasChanges = (skuId: number) => !!pendingChanges[skuId] const openEditDialog = (sku: any, productName: string) => { const change = pendingChanges[sku.id] || {} setEditDialog({ open: true, sku, productName, tempPrice: (change.price ?? sku.price)?.toString() || '', tempMarketPrice: (change.marketPrice ?? sku.marketPrice)?.toString() || '', tempFlashPrice: (change.flashPrice ?? sku.flashPrice)?.toString() || '', }) } const saveEditDialog = () => { const price = parseFloat(editDialog.tempPrice) const marketPrice = editDialog.tempMarketPrice ? parseFloat(editDialog.tempMarketPrice) : null const flashPrice = editDialog.tempFlashPrice ? parseFloat(editDialog.tempFlashPrice) : null if (isNaN(price) || price <= 0) { window.alert('Please enter a valid price') return } if (editDialog.tempMarketPrice && (isNaN(marketPrice!) || marketPrice! <= 0)) { window.alert('Please enter a valid market price') return } if (editDialog.tempFlashPrice && (isNaN(flashPrice!) || flashPrice! <= 0)) { window.alert('Please enter a valid flash price') return } setPendingChanges((prev) => ({ ...prev, [editDialog.sku.id]: { price: price !== parseFloat(editDialog.sku.price) ? price : undefined, marketPrice: marketPrice !== parseFloat(editDialog.sku.marketPrice || '0') ? marketPrice : undefined, flashPrice: flashPrice !== parseFloat(editDialog.sku.flashPrice || '0') ? flashPrice : undefined, }, })) } const handleSave = () => { const updates = Object.entries(pendingChanges).map(([skuId, change]) => { const sku = allSkus.find((s) => s.id === parseInt(skuId)) const update: any = { skuId: sku?.id } if (change.price !== undefined) update.price = change.price if (change.marketPrice !== undefined) update.marketPrice = change.marketPrice if (change.flashPrice !== undefined) update.flashPrice = change.flashPrice if (change.isFlashAvailable !== undefined) update.isFlashAvailable = change.isFlashAvailable return update }) updatePricesMutation.mutate( { updates }, { onSuccess: () => { setPendingChanges({}) refetchProducts() window.alert('Prices updated successfully') }, onError: (error: any) => { window.alert(`Failed to update prices: ${error.message || 'Unknown error'}`) }, } ) } const changeCount = Object.keys(pendingChanges).length return (
setSelectedStores(value as string[])} placeholder="Select stores" />
{productsLoading || storesLoading ? (
Loading...
) : ( ( )} keyExtractor={(item) => item.id.toString()} className="p-4" /> )} setEditDialog({ ...editDialog, open: false, tempFlashPrice: '', tempMarketPrice: '', tempPrice: '' })}>
{editDialog.productName} {editDialog.sku?.displayName || editDialog.sku?.name || ''}
Our Price setEditDialog({ ...editDialog, tempPrice: e.target.value })} type="number" placeholder="Enter price" />
Market Price (Optional) setEditDialog({ ...editDialog, tempMarketPrice: e.target.value })} type="number" placeholder="Enter market price" />
Flash Price (Optional) setEditDialog({ ...editDialog, tempFlashPrice: e.target.value })} type="number" placeholder="Enter flash price" />
setShowMenu(false)}>
Options
) }