| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- 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 (
- <Card id="easyparcel" data-test="easyparcel-setup">
- <CardHeader>
- <CardTitle className="flex flex-wrap items-center gap-2">
- Couriers with EasyParcel
- {easyparcel.connected && (
- <Badge className="bg-emerald-600 text-white hover:bg-emerald-600">
- <Check className="size-3" />
- Connected
- </Badge>
- )}
- </CardTitle>
- <CardDescription>
- 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.
- </CardDescription>
- </CardHeader>
- <CardContent>
- {easyparcel.connected ? (
- <div className="space-y-4">
- <div className="rounded-lg border p-4 text-sm">
- <p>
- Connected to{' '}
- <span className="font-medium">
- {easyparcel.account_name ??
- 'your EasyParcel account'}
- </span>
- </p>
- <div className="mt-1 text-muted-foreground">
- <Deferred
- data="easyparcelBalance"
- fallback={<Skeleton className="h-4 w-40" />}
- >
- <BalanceLine balance={balance} />
- </Deferred>
- </div>
- </div>
- {!hasPickup && (
- <p className="text-sm text-amber-700 dark:text-amber-400">
- One more step: add your pickup address below,
- then save.
- </p>
- )}
- <div className="flex flex-wrap gap-2">
- <Button type="button" variant="outline" asChild>
- <a
- href={SIGN_UP_URL}
- target="_blank"
- rel="noreferrer"
- >
- <ExternalLink />
- Top up on EasyParcel
- </a>
- </Button>
- <Button
- type="button"
- variant="ghost"
- onClick={disconnect}
- data-test="easyparcel-disconnect"
- >
- <Unlink />
- Disconnect
- </Button>
- </div>
- </div>
- ) : (
- <ol className="space-y-4">
- <Step
- number={1}
- title="Create a free EasyParcel account"
- >
- Sign up on EasyParcel and finish their identity
- check. Already have an account? Skip this.
- <a
- href={SIGN_UP_URL}
- target="_blank"
- rel="noreferrer"
- className="mt-1 flex w-fit items-center gap-1 font-medium text-foreground underline underline-offset-2"
- >
- Open EasyParcel
- <ExternalLink className="size-3.5" />
- </a>
- </Step>
- <Step number={2} title="Top up some credit">
- In EasyParcel, top up a little credit (RM 20 is
- enough to start). Each courier you book is paid from
- it.
- </Step>
- <Step
- number={3}
- title="Add your pickup address"
- done={hasPickup}
- >
- Fill it in below and save, so couriers know where to
- collect.
- </Step>
- <Step number={4} title="Connect">
- Sign in to EasyParcel and click Allow. You come
- straight back here.
- <div className="mt-2">
- {easyparcel.available ? (
- <Button
- type="button"
- onClick={connect}
- disabled={busy}
- data-test="easyparcel-connect"
- >
- {busy ? <Spinner /> : <Link2 />}
- Connect EasyParcel
- </Button>
- ) : (
- <p
- className="rounded-md bg-muted p-3 text-xs"
- data-test="easyparcel-unavailable"
- >
- 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.
- </p>
- )}
- </div>
- </Step>
- </ol>
- )}
- </CardContent>
- </Card>
- );
- }
-
- function BalanceLine({
- balance,
- }: {
- balance?: { amount: number | null; error: string | null } | null;
- }) {
- if (balance?.error) {
- return <span className="text-destructive">{balance.error}</span>;
- }
-
- return (
- <span data-test="easyparcel-balance">
- Credit:{' '}
- {balance?.amount != null ? formatRinggit(balance.amount) : '—'}
- </span>
- );
- }
-
- function Step({
- number,
- title,
- done = false,
- children,
- }: {
- number: number;
- title: string;
- done?: boolean;
- children: React.ReactNode;
- }) {
- return (
- <li className="flex gap-3">
- <span
- className={cn(
- 'flex size-7 shrink-0 items-center justify-center rounded-full border text-sm font-medium',
- done && 'border-emerald-600 bg-emerald-600 text-white',
- )}
- aria-hidden="true"
- >
- {done ? <Check className="size-4" /> : number}
- </span>
- <div className="text-sm text-muted-foreground">
- <p className="font-medium text-foreground">
- {title}
- {done && <span className="sr-only"> (done)</span>}
- </p>
- {children}
- </div>
- </li>
- );
- }
|