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 (
Connected to{' '}
{easyparcel.account_name ??
'your EasyParcel account'}
One more step: add your pickup address below,
then save.
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.
)}
{title} {done && (done)}
{children}