AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

easyparcel-setup.tsx 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { Deferred, router } from '@inertiajs/react';
  2. import { Check, ExternalLink, Link2, Unlink } from 'lucide-react';
  3. import { useState } from 'react';
  4. import EasyParcelController from '@/actions/App/Http/Controllers/EasyParcelController';
  5. import { Badge } from '@/components/ui/badge';
  6. import { Button } from '@/components/ui/button';
  7. import {
  8. Card,
  9. CardContent,
  10. CardDescription,
  11. CardHeader,
  12. CardTitle,
  13. } from '@/components/ui/card';
  14. import { Skeleton } from '@/components/ui/skeleton';
  15. import { Spinner } from '@/components/ui/spinner';
  16. import { formatRinggit } from '@/lib/money';
  17. import { cn } from '@/lib/utils';
  18. import type { StoreSummary } from '@/types';
  19. export type EasyParcelSettings = {
  20. /** False until the app's own EasyParcel keys are set. */
  21. available: boolean;
  22. connected: boolean;
  23. account_name: string | null;
  24. redirect_uri: string;
  25. };
  26. const SIGN_UP_URL = 'https://easyparcel.com/my/';
  27. /**
  28. * A guided setup for booking couriers through the merchant's own EasyParcel account.
  29. * It sits inside the Settings form, so its buttons never submit that form.
  30. */
  31. export function EasyParcelSetup({
  32. store,
  33. easyparcel,
  34. balance,
  35. hasPickup,
  36. }: {
  37. store: StoreSummary;
  38. easyparcel: EasyParcelSettings;
  39. balance?: { amount: number | null; error: string | null } | null;
  40. hasPickup: boolean;
  41. }) {
  42. const [busy, setBusy] = useState(false);
  43. const connect = () => {
  44. setBusy(true);
  45. // EasyParcel's sign-in page opens in this tab, then brings the merchant back here.
  46. router.post(
  47. EasyParcelController.connect.url(store.slug),
  48. {},
  49. { onFinish: () => setBusy(false) },
  50. );
  51. };
  52. const disconnect = () => {
  53. if (
  54. !window.confirm(
  55. 'Disconnect EasyParcel? You can connect again at any time. Parcels already booked are not affected.',
  56. )
  57. ) {
  58. return;
  59. }
  60. router.delete(EasyParcelController.disconnect.url(store.slug), {
  61. preserveScroll: true,
  62. });
  63. };
  64. return (
  65. <Card id="easyparcel" data-test="easyparcel-setup">
  66. <CardHeader>
  67. <CardTitle className="flex flex-wrap items-center gap-2">
  68. Couriers with EasyParcel
  69. {easyparcel.connected && (
  70. <Badge className="bg-emerald-600 text-white hover:bg-emerald-600">
  71. <Check className="size-3" />
  72. Connected
  73. </Badge>
  74. )}
  75. </CardTitle>
  76. <CardDescription>
  77. Compare J&amp;T, Pos Laju, Ninja Van, DHL and more on each
  78. order, book the cheapest and print the label. Parcels are
  79. paid from your own EasyParcel credit.
  80. </CardDescription>
  81. </CardHeader>
  82. <CardContent>
  83. {easyparcel.connected ? (
  84. <div className="space-y-4">
  85. <div className="rounded-lg border p-4 text-sm">
  86. <p>
  87. Connected to{' '}
  88. <span className="font-medium">
  89. {easyparcel.account_name ??
  90. 'your EasyParcel account'}
  91. </span>
  92. </p>
  93. <div className="mt-1 text-muted-foreground">
  94. <Deferred
  95. data="easyparcelBalance"
  96. fallback={<Skeleton className="h-4 w-40" />}
  97. >
  98. <BalanceLine balance={balance} />
  99. </Deferred>
  100. </div>
  101. </div>
  102. {!hasPickup && (
  103. <p className="text-sm text-amber-700 dark:text-amber-400">
  104. One more step: add your pickup address below,
  105. then save.
  106. </p>
  107. )}
  108. <div className="flex flex-wrap gap-2">
  109. <Button type="button" variant="outline" asChild>
  110. <a
  111. href={SIGN_UP_URL}
  112. target="_blank"
  113. rel="noreferrer"
  114. >
  115. <ExternalLink />
  116. Top up on EasyParcel
  117. </a>
  118. </Button>
  119. <Button
  120. type="button"
  121. variant="ghost"
  122. onClick={disconnect}
  123. data-test="easyparcel-disconnect"
  124. >
  125. <Unlink />
  126. Disconnect
  127. </Button>
  128. </div>
  129. </div>
  130. ) : (
  131. <ol className="space-y-4">
  132. <Step
  133. number={1}
  134. title="Create a free EasyParcel account"
  135. >
  136. Sign up on EasyParcel and finish their identity
  137. check. Already have an account? Skip this.
  138. <a
  139. href={SIGN_UP_URL}
  140. target="_blank"
  141. rel="noreferrer"
  142. className="mt-1 flex w-fit items-center gap-1 font-medium text-foreground underline underline-offset-2"
  143. >
  144. Open EasyParcel
  145. <ExternalLink className="size-3.5" />
  146. </a>
  147. </Step>
  148. <Step number={2} title="Top up some credit">
  149. In EasyParcel, top up a little credit (RM 20 is
  150. enough to start). Each courier you book is paid from
  151. it.
  152. </Step>
  153. <Step
  154. number={3}
  155. title="Add your pickup address"
  156. done={hasPickup}
  157. >
  158. Fill it in below and save, so couriers know where to
  159. collect.
  160. </Step>
  161. <Step number={4} title="Connect">
  162. Sign in to EasyParcel and click Allow. You come
  163. straight back here.
  164. <div className="mt-2">
  165. {easyparcel.available ? (
  166. <Button
  167. type="button"
  168. onClick={connect}
  169. disabled={busy}
  170. data-test="easyparcel-connect"
  171. >
  172. {busy ? <Spinner /> : <Link2 />}
  173. Connect EasyParcel
  174. </Button>
  175. ) : (
  176. <p
  177. className="rounded-md bg-muted p-3 text-xs"
  178. data-test="easyparcel-unavailable"
  179. >
  180. EasyParcel is not switched on for this
  181. app yet. The app owner adds
  182. EASYPARCEL_CLIENT_ID and
  183. EASYPARCEL_CLIENT_SECRET to .env.local,
  184. with {easyparcel.redirect_uri} as the
  185. redirect address.
  186. </p>
  187. )}
  188. </div>
  189. </Step>
  190. </ol>
  191. )}
  192. </CardContent>
  193. </Card>
  194. );
  195. }
  196. function BalanceLine({
  197. balance,
  198. }: {
  199. balance?: { amount: number | null; error: string | null } | null;
  200. }) {
  201. if (balance?.error) {
  202. return <span className="text-destructive">{balance.error}</span>;
  203. }
  204. return (
  205. <span data-test="easyparcel-balance">
  206. Credit:{' '}
  207. {balance?.amount != null ? formatRinggit(balance.amount) : '—'}
  208. </span>
  209. );
  210. }
  211. function Step({
  212. number,
  213. title,
  214. done = false,
  215. children,
  216. }: {
  217. number: number;
  218. title: string;
  219. done?: boolean;
  220. children: React.ReactNode;
  221. }) {
  222. return (
  223. <li className="flex gap-3">
  224. <span
  225. className={cn(
  226. 'flex size-7 shrink-0 items-center justify-center rounded-full border text-sm font-medium',
  227. done && 'border-emerald-600 bg-emerald-600 text-white',
  228. )}
  229. aria-hidden="true"
  230. >
  231. {done ? <Check className="size-4" /> : number}
  232. </span>
  233. <div className="text-sm text-muted-foreground">
  234. <p className="font-medium text-foreground">
  235. {title}
  236. {done && <span className="sr-only"> (done)</span>}
  237. </p>
  238. {children}
  239. </div>
  240. </li>
  241. );
  242. }