309 lines
11 KiB
TypeScript
309 lines
11 KiB
TypeScript
import { createFileRoute, useNavigate, Outlet, useLocation } from '@tanstack/react-router'
|
|
import { useAuth } from '../lib/auth-context'
|
|
import { MyButton } from 'web-components'
|
|
import { AppLayout } from '../components/AppLayout'
|
|
import { NextOrderGlimpse } from '../components/me/NextOrderGlimpse'
|
|
import { Dialog } from '../components/Dialog'
|
|
import { RegionSelector } from '../components/RegionSelector'
|
|
import { useRegionStore } from '../lib/stores/region-store'
|
|
import {
|
|
Package,
|
|
ShoppingCart,
|
|
Ticket,
|
|
MapPin,
|
|
Globe,
|
|
User,
|
|
MessageSquare,
|
|
MessageCircle,
|
|
FileText,
|
|
LogOut,
|
|
ChevronRight,
|
|
Settings,
|
|
} from 'lucide-react'
|
|
import React, { useState } from 'react'
|
|
import { useGetEssentialConsts } from '../hooks/prominent-api-hooks'
|
|
import { isInNextOrderGlimpseWindow } from '@packages/shared'
|
|
import { trpc } from '../lib/trpc-client'
|
|
|
|
export const Route = createFileRoute('/me')({ component: MePage })
|
|
|
|
interface MenuItem {
|
|
title: string
|
|
icon: React.ComponentType<{ size?: number | string; color?: string }>
|
|
color: string
|
|
to?: string
|
|
href?: string
|
|
subtitle: string
|
|
}
|
|
|
|
interface MenuSection {
|
|
title: string
|
|
items: MenuItem[]
|
|
}
|
|
|
|
function MenuIcon({ icon: Icon, color }: { icon: MenuItem['icon']; color: string }) {
|
|
return (
|
|
<div
|
|
className="mr-4 flex h-12 w-12 shrink-0 items-center justify-center rounded-2xl"
|
|
style={{ backgroundColor: `${color}15` }}
|
|
>
|
|
<Icon size={24} color={color} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function MePage() {
|
|
const navigate = useNavigate()
|
|
const location = useLocation()
|
|
const { user, logout } = useAuth()
|
|
const [regionDialogOpen, setRegionDialogOpen] = useState(false)
|
|
const selectedRegion = useRegionStore((state) => state.selectedRegion)
|
|
const selectedRegionId = useRegionStore((state) => state.selectedRegionId)
|
|
// Same number the NextOrderGlimpse dials via tel: — Live Support opens it in WhatsApp.
|
|
const { data: essentialConsts } = useGetEssentialConsts()
|
|
const supportMobile = (essentialConsts as any)?.supportMobile ?? null
|
|
const waDigits = String(supportMobile ?? '').replace(/\D/g, '')
|
|
const waPhone = waDigits.length === 10 ? `91${waDigits}` : waDigits
|
|
// Same upcoming orders the glimpse above shows; their ids prefill the message.
|
|
const { data: ordersData } = trpc.user.order.getOrders.useQuery(
|
|
{ page: 1, pageSize: 50 },
|
|
{ enabled: !!user }
|
|
)
|
|
const upcomingIds: string[] = ((ordersData as any)?.data ?? [])
|
|
.filter((order: any) => {
|
|
if (String(order.orderStatus).toLowerCase() === 'cancelled') return false
|
|
if (String(order.deliveryStatus).toLowerCase() === 'success') return false
|
|
return isInNextOrderGlimpseWindow(order)
|
|
})
|
|
.map((order: any) => order.orderId)
|
|
.filter(Boolean)
|
|
const waText = upcomingIds.length > 0 ? `Hi, my orders are ${upcomingIds.join(', ')}` : null
|
|
const waHref = waPhone
|
|
? waText
|
|
? `https://wa.me/${waPhone}?text=${encodeURIComponent(waText)}`
|
|
: `https://wa.me/${waPhone}`
|
|
: undefined
|
|
|
|
// Check if we're on the exact /me path (not a child route like /me/orders)
|
|
const isExactMePath = location.pathname === '/me'
|
|
|
|
if (!user) {
|
|
return (
|
|
<AppLayout>
|
|
<div className="flex flex-col items-center gap-4 py-20">
|
|
<p className="text-gray-600">Please sign in</p>
|
|
<MyButton
|
|
textContent="Sign In"
|
|
onClick={() => navigate({ to: '/login' })}
|
|
className="bg-brand-600 text-white"
|
|
/>
|
|
</div>
|
|
</AppLayout>
|
|
)
|
|
}
|
|
|
|
const menuSections: MenuSection[] = [
|
|
{
|
|
title: 'Shopping & Activity',
|
|
items: [
|
|
{
|
|
title: 'My Orders',
|
|
icon: Package,
|
|
color: '#1570EF',
|
|
to: '/me/orders',
|
|
subtitle: 'Orders history & tracking',
|
|
},
|
|
{
|
|
title: 'My Cart',
|
|
icon: ShoppingCart,
|
|
color: '#10B981',
|
|
to: '/cart',
|
|
subtitle: 'Items ready for checkout',
|
|
},
|
|
{
|
|
title: 'Coupons',
|
|
icon: Ticket,
|
|
color: '#8B5CF6',
|
|
to: '/me/coupons',
|
|
subtitle: 'View active offers & rewards',
|
|
},
|
|
...(waHref
|
|
? [
|
|
{
|
|
title: 'Live Support',
|
|
icon: MessageCircle,
|
|
color: '#25D366',
|
|
href: waHref,
|
|
subtitle: supportMobile
|
|
? `Chat on WhatsApp • ${supportMobile}`
|
|
: 'Chat with us on WhatsApp',
|
|
} as MenuItem,
|
|
]
|
|
: []),
|
|
],
|
|
},
|
|
{
|
|
title: 'Saved Information',
|
|
items: [
|
|
{
|
|
title: 'Addresses',
|
|
icon: MapPin,
|
|
color: '#F59E0B',
|
|
to: '/me/addresses',
|
|
subtitle: 'Manage delivery locations',
|
|
},
|
|
{
|
|
title: 'Profile Settings',
|
|
icon: User,
|
|
color: '#3B82F6',
|
|
to: '/me/edit-profile',
|
|
subtitle: 'Update your personal details',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: 'Support',
|
|
items: [
|
|
{
|
|
title: 'Help & Complaints',
|
|
icon: MessageSquare,
|
|
color: '#EF4444',
|
|
to: '/me/complaints',
|
|
subtitle: 'Talk to our customer care',
|
|
},
|
|
{
|
|
title: 'Terms & Conditions',
|
|
icon: FileText,
|
|
color: '#6B7280',
|
|
to: '/me/terms',
|
|
subtitle: 'Read our legal policies',
|
|
},
|
|
],
|
|
},
|
|
]
|
|
|
|
return (
|
|
<AppLayout>
|
|
{isExactMePath ? (
|
|
<div className="min-h-screen bg-gray-50">
|
|
<div className="mx-auto w-full max-w-2xl">
|
|
<div className="flex flex-row items-center justify-between px-6 pb-10 pt-8">
|
|
<div>
|
|
<p className="text-3xl font-extrabold tracking-tight text-gray-900">My Account</p>
|
|
<p className="mt-1 text-sm text-gray-400">Manage your freshyo experience</p>
|
|
</div>
|
|
<button
|
|
onClick={() => navigate({ to: '/me/edit-profile' })}
|
|
aria-label="Settings"
|
|
className="rounded-2xl bg-gray-100 p-3 transition-colors hover:bg-gray-200"
|
|
>
|
|
<Settings size={22} color="#1F2937" />
|
|
</button>
|
|
</div>
|
|
|
|
<NextOrderGlimpse />
|
|
|
|
<div className="px-6">
|
|
{menuSections.map((section, sIndex) => (
|
|
<div key={sIndex} className="mb-8">
|
|
<p className="mb-4 px-2 text-sm font-bold uppercase tracking-[2px] text-gray-400">
|
|
{section.title}
|
|
</p>
|
|
<div className="overflow-hidden rounded-[32px] border border-gray-100 bg-white shadow-sm">
|
|
{section.items.map((item, iIndex) => {
|
|
const rowClass = `flex w-full flex-row items-center p-4 text-left transition-colors hover:bg-gray-50 ${
|
|
iIndex !== section.items.length - 1 ? 'border-b border-gray-50' : ''
|
|
}`
|
|
const rowContent = (
|
|
<>
|
|
<MenuIcon icon={item.icon} color={item.color} />
|
|
<div className="min-w-0 flex-1">
|
|
<p className="text-base font-semibold text-gray-800">{item.title}</p>
|
|
<p className="mt-0.5 text-xs text-gray-400">{item.subtitle}</p>
|
|
</div>
|
|
<div className="ml-2 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-gray-50">
|
|
<ChevronRight size={16} color="#94A3B8" />
|
|
</div>
|
|
</>
|
|
)
|
|
return item.href ? (
|
|
<a
|
|
key={iIndex}
|
|
href={item.href}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className={rowClass}
|
|
>
|
|
{rowContent}
|
|
</a>
|
|
) : (
|
|
<button
|
|
key={iIndex}
|
|
onClick={() => navigate({ to: item.to as any })}
|
|
className={rowClass}
|
|
>
|
|
{rowContent}
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
</div>
|
|
))}
|
|
|
|
{/* Logout row */}
|
|
<button
|
|
onClick={logout}
|
|
className="mt-2 flex w-full flex-row items-center justify-center rounded-[24px] border border-red-50 bg-white p-5 shadow-sm transition-colors hover:bg-red-50"
|
|
>
|
|
<LogOut size={22} color="#F43F5E" className="mr-2" />
|
|
<p className="text-base font-bold text-rose-500">Log Out from Freshyo</p>
|
|
</button>
|
|
|
|
{/* Delivery region row */}
|
|
<button
|
|
onClick={() => setRegionDialogOpen(true)}
|
|
className="mt-3 flex w-full flex-row items-center p-4 text-left transition-colors hover:bg-gray-50"
|
|
>
|
|
<div
|
|
className="mr-4 flex h-12 w-12 shrink-0 items-center justify-center rounded-2xl"
|
|
style={{ backgroundColor: '#1570EF15' }}
|
|
>
|
|
<Globe size={24} color="#1570EF" />
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<p className="text-base font-semibold text-gray-800">Delivery Region</p>
|
|
<p className="mt-0.5 text-xs text-gray-400">
|
|
{selectedRegion?.name ? `Browsing ${selectedRegion.name}` : 'Choose your region'}
|
|
</p>
|
|
</div>
|
|
<div className="ml-2 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-gray-50">
|
|
<ChevronRight size={16} color="#94A3B8" />
|
|
</div>
|
|
</button>
|
|
|
|
<p className="mt-12 text-center text-[10px] uppercase tracking-[3px] text-gray-300">
|
|
Version 1.2.0 • Freshyo App
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
/* Render child routes when not on exact /me path */
|
|
<Outlet />
|
|
)}
|
|
|
|
<Dialog
|
|
open={regionDialogOpen}
|
|
onClose={() => setRegionDialogOpen(false)}
|
|
title="Delivery Region"
|
|
>
|
|
<RegionSelector
|
|
mode="switch"
|
|
selectedRegionId={selectedRegionId}
|
|
onComplete={() => setRegionDialogOpen(false)}
|
|
onCancel={() => setRegionDialogOpen(false)}
|
|
/>
|
|
</Dialog>
|
|
</AppLayout>
|
|
)
|
|
}
|