AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

checkout.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { useForm } from '@inertiajs/react';
  2. import type { FormEvent, ReactNode } from 'react';
  3. import { useStorefront } from '../context';
  4. import { storefrontText } from '../text';
  5. import { formatPrice } from '../theme';
  6. import { Container, SfLink } from '../ui';
  7. const inputClass =
  8. 'w-full rounded-(--sf-radius) border border-(--sf-border) bg-(--sf-bg) px-3 py-2.5 text-base text-(--sf-fg) outline-none focus-visible:border-(--sf-primary) focus-visible:ring-2 focus-visible:ring-(--sf-primary)/30';
  9. function Field({
  10. id,
  11. label,
  12. error,
  13. children,
  14. }: {
  15. id: string;
  16. label: string;
  17. error?: string;
  18. children: ReactNode;
  19. }) {
  20. return (
  21. <div className="grid gap-1.5">
  22. <label htmlFor={id} className="text-sm font-medium">
  23. {label}
  24. </label>
  25. {children}
  26. {error && (
  27. <p id={`${id}-error`} className="text-sm text-red-700">
  28. {error}
  29. </p>
  30. )}
  31. </div>
  32. );
  33. }
  34. /** The shopper's details and address, before going to the payment page. */
  35. export function CheckoutPage({
  36. states,
  37. shippingByState,
  38. }: {
  39. states: string[];
  40. shippingByState: Record<string, number>;
  41. }) {
  42. const { cart, language, baseUrl } = useStorefront();
  43. const t = storefrontText(language);
  44. const form = useForm({
  45. name: '',
  46. email: '',
  47. phone: '',
  48. line1: '',
  49. line2: '',
  50. city: '',
  51. postcode: '',
  52. state: '',
  53. });
  54. // "cart" errors come from the server: an empty cart, or the payment page could not be opened.
  55. const errors = form.errors as typeof form.errors & { cart?: string };
  56. const shipping = form.data.state
  57. ? (shippingByState[form.data.state] ?? null)
  58. : null;
  59. function submit(event: FormEvent) {
  60. event.preventDefault();
  61. form.post(`${baseUrl}/checkout`, { preserveScroll: true });
  62. }
  63. function text(
  64. name: keyof typeof form.data,
  65. label: string,
  66. props: React.InputHTMLAttributes<HTMLInputElement> = {},
  67. ) {
  68. return (
  69. <Field id={name} label={label} error={form.errors[name]}>
  70. <input
  71. id={name}
  72. name={name}
  73. value={form.data[name]}
  74. onChange={(event) => form.setData(name, event.target.value)}
  75. aria-invalid={form.errors[name] ? true : undefined}
  76. aria-describedby={
  77. form.errors[name] ? `${name}-error` : undefined
  78. }
  79. className={inputClass}
  80. {...props}
  81. />
  82. </Field>
  83. );
  84. }
  85. return (
  86. <main className="py-10 md:py-14">
  87. <Container>
  88. <SfLink
  89. href="/cart"
  90. className="text-sm text-(--sf-muted) transition-colors hover:text-(--sf-fg)"
  91. >
  92. ← {t.backToCart}
  93. </SfLink>
  94. <h1 className="sf-heading mt-4 text-4xl font-semibold sm:text-5xl">
  95. {t.checkout}
  96. </h1>
  97. <div className="mt-10 grid gap-10 lg:grid-cols-[1fr_360px] lg:gap-14">
  98. <form onSubmit={submit} className="space-y-10" noValidate>
  99. {errors.cart && (
  100. <p
  101. role="alert"
  102. className="rounded-(--sf-radius-card) border border-red-300 bg-red-50 p-4 text-sm text-red-800"
  103. >
  104. {errors.cart}
  105. </p>
  106. )}
  107. <fieldset className="space-y-5">
  108. <legend className="sf-heading text-xl font-semibold">
  109. {t.contactDetails}
  110. </legend>
  111. {text('name', t.fullName, {
  112. autoComplete: 'name',
  113. required: true,
  114. })}
  115. <div className="grid gap-5 sm:grid-cols-2">
  116. {text('email', t.email, {
  117. type: 'email',
  118. autoComplete: 'email',
  119. required: true,
  120. })}
  121. {text('phone', t.phone, {
  122. type: 'tel',
  123. autoComplete: 'tel',
  124. required: true,
  125. })}
  126. </div>
  127. </fieldset>
  128. <fieldset className="space-y-5">
  129. <legend className="sf-heading text-xl font-semibold">
  130. {t.deliveryAddress}
  131. </legend>
  132. {text('line1', t.addressLine1, {
  133. autoComplete: 'address-line1',
  134. required: true,
  135. })}
  136. {text('line2', t.addressLine2, {
  137. autoComplete: 'address-line2',
  138. })}
  139. <div className="grid gap-5 sm:grid-cols-3">
  140. {text('postcode', t.postcode, {
  141. inputMode: 'numeric',
  142. autoComplete: 'postal-code',
  143. maxLength: 5,
  144. required: true,
  145. })}
  146. {text('city', t.city, {
  147. autoComplete: 'address-level2',
  148. required: true,
  149. })}
  150. <Field
  151. id="state"
  152. label={t.state}
  153. error={form.errors.state}
  154. >
  155. <select
  156. id="state"
  157. name="state"
  158. value={form.data.state}
  159. onChange={(event) =>
  160. form.setData(
  161. 'state',
  162. event.target.value,
  163. )
  164. }
  165. autoComplete="address-level1"
  166. required
  167. aria-invalid={
  168. form.errors.state ? true : undefined
  169. }
  170. className={inputClass}
  171. >
  172. <option value="" disabled>
  173. {t.chooseState}
  174. </option>
  175. {states.map((state) => (
  176. <option key={state} value={state}>
  177. {state}
  178. </option>
  179. ))}
  180. </select>
  181. </Field>
  182. </div>
  183. </fieldset>
  184. <div>
  185. <button
  186. type="submit"
  187. disabled={form.processing}
  188. className="inline-flex w-full items-center justify-center rounded-(--sf-radius) bg-(--sf-primary) px-6 py-3 text-sm font-semibold text-(--sf-primary-fg) transition-opacity hover:opacity-85 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--sf-primary) disabled:cursor-not-allowed disabled:opacity-50 sm:w-auto"
  189. >
  190. {t.payNow}
  191. </button>
  192. <p className="mt-2 text-xs text-(--sf-muted)">
  193. {t.paySecure}
  194. </p>
  195. </div>
  196. </form>
  197. <aside className="h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
  198. <h2 className="font-medium">{t.orderSummary}</h2>
  199. <ul className="mt-4 space-y-3 text-sm">
  200. {cart.lines.map((line) => (
  201. <li
  202. key={line.key}
  203. className="flex justify-between gap-4"
  204. >
  205. <span>
  206. {line.product.name}
  207. {line.variant &&
  208. ` (${line.variant.name})`}{' '}
  209. <span className="text-(--sf-muted)">
  210. × {line.quantity}
  211. </span>
  212. </span>
  213. <span className="tabular-nums">
  214. {formatPrice(line.line_total)}
  215. </span>
  216. </li>
  217. ))}
  218. </ul>
  219. <div className="mt-5 space-y-2 border-t border-(--sf-border) pt-4 text-sm">
  220. <div className="flex justify-between gap-4">
  221. <span>{t.subtotal}</span>
  222. <span className="tabular-nums">
  223. {formatPrice(cart.subtotal)}
  224. </span>
  225. </div>
  226. {cart.applied && (
  227. <div className="flex justify-between gap-4">
  228. <span>
  229. {t.discount} ({cart.applied.name})
  230. </span>
  231. <span className="tabular-nums">
  232. −{formatPrice(cart.applied.amount)}
  233. </span>
  234. </div>
  235. )}
  236. <div className="flex justify-between gap-4">
  237. <span>{t.shipping}</span>
  238. <span
  239. className="tabular-nums"
  240. aria-live="polite"
  241. >
  242. {shipping === null
  243. ? '—'
  244. : shipping === 0
  245. ? t.shippingFree
  246. : formatPrice(shipping)}
  247. </span>
  248. </div>
  249. {shipping === null && (
  250. <p className="text-xs text-(--sf-muted)">
  251. {t.chooseStateForShipping}
  252. </p>
  253. )}
  254. </div>
  255. <div className="mt-4 flex items-baseline justify-between gap-4 border-t border-(--sf-border) pt-4">
  256. <span className="font-medium">{t.total}</span>
  257. <span className="text-xl font-semibold tabular-nums">
  258. {formatPrice(cart.total + (shipping ?? 0))}
  259. </span>
  260. </div>
  261. </aside>
  262. </div>
  263. </Container>
  264. </main>
  265. );
  266. }