import React from 'react' import { RefreshControl, ScrollView, View } from 'react-native' import { useLocalSearchParams, useRouter } from 'expo-router' import { MaterialCommunityIcons } from '@expo/vector-icons' import { tw, MyText, MyTouchableOpacity, useMarkDataFetchers } from 'common-ui' import { trpc } from '@/src/trpc-client' const money = (value: string | number | null | undefined) => value == null || value === '' ? 'None' : `₹${Number(value)}` const formatDate = (value: string | Date | null | undefined) => value ? new Date(value).toLocaleDateString() : 'No expiry' const couponStatus = (coupon: any) => { if (coupon.isInvalidated) return 'Inactive' if (coupon.validTill && new Date(coupon.validTill) <= new Date()) return 'Expired' return 'Active' } const statusStyles = (status: string) => { if (status === 'Active') return { badge: 'bg-green-100', text: 'text-green-600' } if (status === 'Expired') return { badge: 'bg-yellow-100', text: 'text-yellow-600' } return { badge: 'bg-red-100', text: 'text-red-600' } } function DetailRow({ icon, label, value, }: { icon: React.ComponentProps['name'] label: string value: string }) { return ( {label} {value} ) } function Section({ title, children }: { title: string; children: React.ReactNode }) { return ( {title} {children} ) } export default function CouponDetails() { const router = useRouter() const { id } = useLocalSearchParams() const rawId = Array.isArray(id) ? id[0] : id const couponId = Number(rawId) const hasValidId = Number.isInteger(couponId) && couponId > 0 const { data: coupon, isLoading, isFetching, error, refetch } = trpc.admin.coupon.getById.useQuery( { id: couponId }, { enabled: hasValidId } ) useMarkDataFetchers(() => { refetch() }) const handleEdit = () => { router.push(`/(drawer)/dashboard/coupons/edit/${couponId}`) } if (isLoading) { return ( Loading coupon... ) } if (error || !coupon) { return ( Coupon not found router.back()} style={tw`mt-6 px-8 py-3 bg-gray-100 rounded-full`} > Go Back ) } const status = couponStatus(coupon) const colors = statusStyles(status) const isGlobal = coupon.regionId == null const users = coupon.applicableUsers ?? [] const skuCount = coupon.skuIds?.length ?? 0 const discount = coupon.discountPercent ? `${Number(coupon.discountPercent)}% off` : coupon.flatDiscount ? `${money(coupon.flatDiscount)} off` : 'None' return ( router.back()} style={tw`p-2 -ml-2 mr-1`} > Coupon Details ID: {coupon.id} {!isGlobal && ( Edit )} } > {coupon.couponCode} {isGlobal && ( Global )} {status} {discount} {isGlobal && ( Applies to all regions — created and managed by the superadmin. )}
Uses / User {coupon.maxLimitForUser ? String(coupon.maxLimitForUser) : 'Unlimited'}
{!coupon.isApplyForAll && users.length > 0 && ( {users.map((user: any) => ( {user.name || 'Unnamed user'} {user.mobile || 'No mobile'} ))} )}
Created On {coupon.createdAt ? new Date(coupon.createdAt).toLocaleDateString() : 'Unknown'}
) }