import { Deferred, router } from '@inertiajs/react'; import { Check, ExternalLink, Link2, Unlink } from 'lucide-react'; import { useState } from 'react'; import EasyParcelController from '@/actions/App/Http/Controllers/EasyParcelController'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Skeleton } from '@/components/ui/skeleton'; import { Spinner } from '@/components/ui/spinner'; import { formatRinggit } from '@/lib/money'; import { cn } from '@/lib/utils'; import type { StoreSummary } from '@/types'; export type EasyParcelSettings = { /** False until the app's own EasyParcel keys are set. */ available: boolean; connected: boolean; account_name: string | null; redirect_uri: string; }; const SIGN_UP_URL = 'https://easyparcel.com/my/'; /** * A guided setup for booking couriers through the merchant's own EasyParcel account. * It sits inside the Settings form, so its buttons never submit that form. */ export function EasyParcelSetup({ store, easyparcel, balance, hasPickup, }: { store: StoreSummary; easyparcel: EasyParcelSettings; balance?: { amount: number | null; error: string | null } | null; hasPickup: boolean; }) { const [busy, setBusy] = useState(false); const connect = () => { setBusy(true); // EasyParcel's sign-in page opens in this tab, then brings the merchant back here. router.post( EasyParcelController.connect.url(store.slug), {}, { onFinish: () => setBusy(false) }, ); }; const disconnect = () => { if ( !window.confirm( 'Disconnect EasyParcel? You can connect again at any time. Parcels already booked are not affected.', ) ) { return; } router.delete(EasyParcelController.disconnect.url(store.slug), { preserveScroll: true, }); }; return ( Couriers with EasyParcel {easyparcel.connected && ( Connected )} Compare J&T, Pos Laju, Ninja Van, DHL and more on each order, book the cheapest and print the label. Parcels are paid from your own EasyParcel credit. {easyparcel.connected ? (

Connected to{' '} {easyparcel.account_name ?? 'your EasyParcel account'}

} >
{!hasPickup && (

One more step: add your pickup address below, then save.

)}
) : (
    Sign up on EasyParcel and finish their identity check. Already have an account? Skip this. Open EasyParcel In EasyParcel, top up a little credit (RM 20 is enough to start). Each courier you book is paid from it. Fill it in below and save, so couriers know where to collect. Sign in to EasyParcel and click Allow. You come straight back here.
    {easyparcel.available ? ( ) : (

    EasyParcel is not switched on for this app yet. The app owner adds EASYPARCEL_CLIENT_ID and EASYPARCEL_CLIENT_SECRET to .env.local, with {easyparcel.redirect_uri} as the redirect address.

    )}
)}
); } function BalanceLine({ balance, }: { balance?: { amount: number | null; error: string | null } | null; }) { if (balance?.error) { return {balance.error}; } return ( Credit:{' '} {balance?.amount != null ? formatRinggit(balance.amount) : '—'} ); } function Step({ number, title, done = false, children, }: { number: number; title: string; done?: boolean; children: React.ReactNode; }) { return (
  • {title} {done && (done)}

    {children}
  • ); }