freshyo/apps/admin-ui/app/(drawer)/dashboard/coupons/detail/[id].tsx
2026-11-18 09:40:33 +05:30

232 lines
9.2 KiB
TypeScript

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<typeof MaterialCommunityIcons>['name']
label: string
value: string
}) {
return (
<View style={tw`flex-row items-center justify-between py-3 border-b border-gray-100`}>
<View style={tw`flex-row items-center flex-1 mr-3`}>
<MaterialCommunityIcons name={icon} size={18} color="#6b7280" />
<MyText style={tw`text-sm text-gray-500 ml-2`}>{label}</MyText>
</View>
<MyText style={tw`text-sm font-semibold text-gray-800 text-right`}>{value}</MyText>
</View>
)
}
function Section({ title, children }: { title: string; children: React.ReactNode }) {
return (
<View style={tw`bg-white rounded-2xl p-4 mb-4 shadow-sm`}>
<MyText style={tw`text-xs font-bold uppercase tracking-wider text-gray-400 mb-1`}>
{title}
</MyText>
{children}
</View>
)
}
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 (
<View style={tw`flex-1 justify-center items-center bg-white`}>
<MaterialCommunityIcons name="loading" size={32} color="#3b82f6" />
<MyText style={tw`text-gray-500 mt-4 font-medium`}>Loading coupon...</MyText>
</View>
)
}
if (error || !coupon) {
return (
<View style={tw`flex-1 justify-center items-center bg-white p-6`}>
<MaterialCommunityIcons name="ticket-percent-outline" size={56} color="#ef4444" />
<MyText style={tw`text-xl font-bold text-gray-900 mt-4`}>Coupon not found</MyText>
<MyTouchableOpacity
testID="coupon-detail-back"
onPress={() => router.back()}
style={tw`mt-6 px-8 py-3 bg-gray-100 rounded-full`}
>
<MyText style={tw`text-gray-800 font-semibold`}>Go Back</MyText>
</MyTouchableOpacity>
</View>
)
}
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 (
<View style={tw`flex-1 bg-gray-50`}>
<View style={tw`flex-row items-center px-4 py-3 bg-white border-b border-gray-100`}>
<MyTouchableOpacity
testID="coupon-detail-back"
onPress={() => router.back()}
style={tw`p-2 -ml-2 mr-1`}
>
<MaterialCommunityIcons name="arrow-left" size={24} color="#111827" />
</MyTouchableOpacity>
<View style={tw`flex-1`}>
<MyText style={tw`text-lg font-bold text-gray-900`}>Coupon Details</MyText>
<MyText style={tw`text-xs text-gray-500`}>ID: {coupon.id}</MyText>
</View>
{!isGlobal && (
<MyTouchableOpacity
testID="coupon-detail-edit"
onPress={handleEdit}
style={tw`flex-row items-center bg-blue-500 px-3 py-2 rounded-lg`}
>
<MaterialCommunityIcons name="pencil" size={16} color="white" />
<MyText style={tw`text-white font-semibold ml-1`}>Edit</MyText>
</MyTouchableOpacity>
)}
</View>
<ScrollView
style={tw`flex-1`}
contentContainerStyle={tw`p-4 pb-10`}
refreshControl={<RefreshControl refreshing={isFetching} onRefresh={() => { void refetch() }} />}
>
<View style={tw`bg-white rounded-2xl p-5 mb-4 shadow-sm`}>
<View style={tw`flex-row items-center flex-wrap`}>
<MyText style={tw`text-2xl font-extrabold text-gray-900 mr-2`}>
{coupon.couponCode}
</MyText>
{isGlobal && (
<View style={tw`px-2 py-0.5 rounded-full bg-indigo-100 border border-indigo-200 mr-2`}>
<MyText style={tw`text-[10px] font-bold text-indigo-700`}>Global</MyText>
</View>
)}
<View style={tw`px-2 py-1 rounded-full ${colors.badge}`}>
<MyText style={tw`text-xs font-semibold ${colors.text}`}>{status}</MyText>
</View>
</View>
<MyText style={tw`text-base font-semibold text-gray-700 mt-3`}>{discount}</MyText>
{isGlobal && (
<View style={tw`mt-3 bg-indigo-50 border border-indigo-100 rounded-lg p-3`}>
<MyText style={tw`text-xs text-indigo-700`}>
Applies to all regions — created and managed by the superadmin.
</MyText>
</View>
)}
</View>
<Section title="Discount">
<DetailRow icon="percent" label="Type" value={coupon.discountPercent ? 'Percentage' : 'Flat amount'} />
<DetailRow icon="cart-outline" label="Min Order" value={money(coupon.minOrder)} />
<DetailRow icon="cash-multiple" label="Max Discount" value={money(coupon.maxValue)} />
<View style={tw`flex-row items-center justify-between py-3`}>
<View style={tw`flex-row items-center flex-1 mr-3`}>
<MaterialCommunityIcons name="repeat" size={18} color="#6b7280" />
<MyText style={tw`text-sm text-gray-500 ml-2`}>Uses / User</MyText>
</View>
<MyText style={tw`text-sm font-semibold text-gray-800 text-right`}>
{coupon.maxLimitForUser ? String(coupon.maxLimitForUser) : 'Unlimited'}
</MyText>
</View>
</Section>
<Section title="Validity & rules">
<DetailRow icon="calendar-blank-outline" label="Valid Till" value={formatDate(coupon.validTill)} />
<DetailRow icon="numeric-1-circle-outline" label="First Order Only" value={coupon.isFirstOrderOnly ? 'Yes' : 'No'} />
<DetailRow icon="lock-outline" label="Exclusive" value={coupon.exclusiveApply ? 'Yes' : 'No'} />
<DetailRow icon="map-marker-outline" label="Region" value={isGlobal ? 'All regions' : `Region #${coupon.regionId}`} />
</Section>
<Section title="Targeting">
<DetailRow
icon="account-group-outline"
label="Audience"
value={coupon.isApplyForAll ? 'All users' : `${users.length} users`}
/>
<DetailRow
icon="package-variant-closed"
label="Products"
value={skuCount === 0 ? 'Whole cart' : `${skuCount} selected`}
/>
{!coupon.isApplyForAll && users.length > 0 && (
<View style={tw`mt-2`}>
{users.map((user: any) => (
<View key={user.id} style={tw`flex-row items-center py-2 border-b border-gray-50`}>
<MaterialCommunityIcons name="account-circle-outline" size={18} color="#9ca3af" />
<MyText style={tw`text-sm text-gray-700 ml-2 flex-1`}>
{user.name || 'Unnamed user'}
</MyText>
<MyText style={tw`text-xs text-gray-500`}>{user.mobile || 'No mobile'}</MyText>
</View>
))}
</View>
)}
</Section>
<Section title="Created">
<DetailRow icon="account-outline" label="Created By" value={coupon.creator?.name || 'Unknown'} />
<View style={tw`flex-row items-center justify-between py-3`}>
<View style={tw`flex-row items-center flex-1 mr-3`}>
<MaterialCommunityIcons name="clock-outline" size={18} color="#6b7280" />
<MyText style={tw`text-sm text-gray-500 ml-2`}>Created On</MyText>
</View>
<MyText style={tw`text-sm font-semibold text-gray-800 text-right`}>
{coupon.createdAt ? new Date(coupon.createdAt).toLocaleDateString() : 'Unknown'}
</MyText>
</View>
</Section>
</ScrollView>
</View>
)
}