import { createFileRoute } from '@tanstack/react-router' import { useState } from 'react' import { AppContainer, MyButton, p as MyText, pInput as PInput } from 'web-components' import { ProtectedRoute } from '../components/ProtectedRoute' import { AppShell } from '../components/AppShell' import { trpc } from '../lib/trpc-client' export const Route = createFileRoute('/regions')({ component: () => ( ), }) const SETTING_KEYS = [ 'minRegularOrderValue', 'freeDeliveryThreshold', 'deliveryCharge', 'flashFreeDeliveryThreshold', 'flashDeliveryCharge', 'popularItems', 'allItemsOrder', 'isFlashDeliveryEnabled', 'supportMobile', 'supportEmail', 'welcomeMessage', ] as const const SETTING_LABELS: Record = { minRegularOrderValue: 'Minimum Regular Order Value', freeDeliveryThreshold: 'Free Delivery Threshold', deliveryCharge: 'Delivery Charge', flashFreeDeliveryThreshold: 'Flash Free Delivery Threshold', flashDeliveryCharge: 'Flash Delivery Charge', popularItems: 'Popular Items (JSON)', allItemsOrder: 'All Items Order (JSON)', isFlashDeliveryEnabled: 'Enable Flash Delivery', supportMobile: 'Support Mobile', supportEmail: 'Support Email', welcomeMessage: 'Welcome Message', } function RegionsPage() { const regionsQuery = trpc.superadmin.regions.list.useQuery() const createRegion = trpc.superadmin.regions.create.useMutation({ onSuccess: () => regionsQuery.refetch(), }) const [selectedRegionId, setSelectedRegionId] = useState(null) const [newRegionName, setNewRegionName] = useState('') const [newRegionDescription, setNewRegionDescription] = useState('') const regions = regionsQuery.data ?? [] return (
Regions Create regions and manage the delivery bounds and settings for each region.
Add a region
setNewRegionName(e.target.value)} placeholder="e.g. Mahabubnagar" /> setNewRegionDescription(e.target.value)} placeholder="Optional" />
{ const region = await createRegion.mutateAsync({ name: newRegionName.trim(), description: newRegionDescription.trim() || null, }) setNewRegionName('') setNewRegionDescription('') setSelectedRegionId(region.id) }} className="mt-4 rounded-lg bg-blue-500 px-4 py-2 text-white disabled:opacity-50" > {createRegion.isPending ? 'Creating...' : 'Create region'}
{regionsQuery.isLoading && Loading regions...}
{regions.map((region) => ( ))}
{selectedRegionId && }
) } function RegionDetail({ regionId }: { regionId: number }) { const regionQuery = trpc.superadmin.regions.get.useQuery({ id: regionId }) const [activeBoundId, setActiveBoundId] = useState(null) const [showAddBound, setShowAddBound] = useState(false) const bounds = regionQuery.data?.bounds ?? [] const effectiveBoundId = bounds.length > 0 ? bounds[0].id : null const activeBound = bounds.find((b) => b.id === activeBoundId) ?? bounds[0] ?? null return (
{regionQuery.data?.name} — bounds setShowAddBound((v) => !v)} className="rounded-lg border border-gray-200 px-3 py-1.5 text-sm text-gray-700" > {showAddBound ? 'Cancel' : 'Add bound'}
{showAddBound && ( { setShowAddBound(false) setActiveBoundId(id) regionQuery.refetch() }} /> )} {bounds.length === 0 && !showAddBound && ( No bounds yet. Add at least one bound so this region can take orders. )} {bounds.length > 0 && ( <>
{bounds.map((bound) => { const isActive = activeBound?.id === bound.id return ( ) })}
Only the lowest-priority bound's settings currently apply to checkout, regardless of address coordinates. {activeBound && } )}
) } function AddBoundForm({ regionId, onCreated }: { regionId: number; onCreated: (id: number) => void }) { const [description, setDescription] = useState('') const [priority, setPriority] = useState('') const [geojson, setGeojson] = useState('') const [error, setError] = useState(null) const createBound = trpc.superadmin.regions.createBound.useMutation() return (
setDescription(e.target.value)} placeholder="e.g. Mbnr-1" /> setPriority(e.target.value)} placeholder="1" />
GeoJSON