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.

checkout.tsx 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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({ states }: { states: string[] }) {
  36. const { cart, language, baseUrl } = useStorefront();
  37. const t = storefrontText(language);
  38. const form = useForm({
  39. name: '',
  40. email: '',
  41. phone: '',
  42. line1: '',
  43. line2: '',
  44. city: '',
  45. postcode: '',
  46. state: '',
  47. });
  48. // "cart" errors come from the server: an empty cart, or the payment page could not be opened.
  49. const errors = form.errors as typeof form.errors & { cart?: string };
  50. function submit(event: FormEvent) {
  51. event.preventDefault();
  52. form.post(`${baseUrl}/checkout`, { preserveScroll: true });
  53. }
  54. function text(
  55. name: keyof typeof form.data,
  56. label: string,
  57. props: React.InputHTMLAttributes<HTMLInputElement> = {},
  58. ) {
  59. return (
  60. <Field id={name} label={label} error={form.errors[name]}>
  61. <input
  62. id={name}
  63. name={name}
  64. value={form.data[name]}
  65. onChange={(event) => form.setData(name, event.target.value)}
  66. aria-invalid={form.errors[name] ? true : undefined}
  67. aria-describedby={
  68. form.errors[name] ? `${name}-error` : undefined
  69. }
  70. className={inputClass}
  71. {...props}
  72. />
  73. </Field>
  74. );
  75. }
  76. return (
  77. <main className="py-10 md:py-14">
  78. <Container>
  79. <SfLink
  80. href="/cart"
  81. className="text-sm text-(--sf-muted) transition-colors hover:text-(--sf-fg)"
  82. >
  83. ← {t.backToCart}
  84. </SfLink>
  85. <h1 className="sf-heading mt-4 text-4xl font-semibold sm:text-5xl">
  86. {t.checkout}
  87. </h1>
  88. <div className="mt-10 grid gap-10 lg:grid-cols-[1fr_360px] lg:gap-14">
  89. <form onSubmit={submit} className="space-y-10" noValidate>
  90. {errors.cart && (
  91. <p
  92. role="alert"
  93. className="rounded-(--sf-radius-card) border border-red-300 bg-red-50 p-4 text-sm text-red-800"
  94. >
  95. {errors.cart}
  96. </p>
  97. )}
  98. <fieldset className="space-y-5">
  99. <legend className="sf-heading text-xl font-semibold">
  100. {t.contactDetails}
  101. </legend>
  102. {text('name', t.fullName, {
  103. autoComplete: 'name',
  104. required: true,
  105. })}
  106. <div className="grid gap-5 sm:grid-cols-2">
  107. {text('email', t.email, {
  108. type: 'email',
  109. autoComplete: 'email',
  110. required: true,
  111. })}
  112. {text('phone', t.phone, {
  113. type: 'tel',
  114. autoComplete: 'tel',
  115. required: true,
  116. })}
  117. </div>
  118. </fieldset>
  119. <fieldset className="space-y-5">
  120. <legend className="sf-heading text-xl font-semibold">
  121. {t.deliveryAddress}
  122. </legend>
  123. {text('line1', t.addressLine1, {
  124. autoComplete: 'address-line1',
  125. required: true,
  126. })}
  127. {text('line2', t.addressLine2, {
  128. autoComplete: 'address-line2',
  129. })}
  130. <div className="grid gap-5 sm:grid-cols-3">
  131. {text('postcode', t.postcode, {
  132. inputMode: 'numeric',
  133. autoComplete: 'postal-code',
  134. maxLength: 5,
  135. required: true,
  136. })}
  137. {text('city', t.city, {
  138. autoComplete: 'address-level2',
  139. required: true,
  140. })}
  141. <Field
  142. id="state"
  143. label={t.state}
  144. error={form.errors.state}
  145. >
  146. <select
  147. id="state"
  148. name="state"
  149. value={form.data.state}
  150. onChange={(event) =>
  151. form.setData(
  152. 'state',
  153. event.target.value,
  154. )
  155. }
  156. autoComplete="address-level1"
  157. required
  158. aria-invalid={
  159. form.errors.state ? true : undefined
  160. }
  161. className={inputClass}
  162. >
  163. <option value="" disabled>
  164. {t.chooseState}
  165. </option>
  166. {states.map((state) => (
  167. <option key={state} value={state}>
  168. {state}
  169. </option>
  170. ))}
  171. </select>
  172. </Field>
  173. </div>
  174. </fieldset>
  175. <div>
  176. <button
  177. type="submit"
  178. disabled={form.processing}
  179. 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"
  180. >
  181. {t.payNow}
  182. </button>
  183. <p className="mt-2 text-xs text-(--sf-muted)">
  184. {t.paySecure}
  185. </p>
  186. </div>
  187. </form>
  188. <aside className="h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
  189. <h2 className="font-medium">{t.orderSummary}</h2>
  190. <ul className="mt-4 space-y-3 text-sm">
  191. {cart.lines.map((line) => (
  192. <li
  193. key={line.product.id}
  194. className="flex justify-between gap-4"
  195. >
  196. <span>
  197. {line.product.name}{' '}
  198. <span className="text-(--sf-muted)">
  199. × {line.quantity}
  200. </span>
  201. </span>
  202. <span className="tabular-nums">
  203. {formatPrice(line.line_total)}
  204. </span>
  205. </li>
  206. ))}
  207. </ul>
  208. <div className="mt-5 flex items-baseline justify-between gap-4 border-t border-(--sf-border) pt-4">
  209. <span className="font-medium">{t.total}</span>
  210. <span className="text-xl font-semibold tabular-nums">
  211. {formatPrice(cart.subtotal)}
  212. </span>
  213. </div>
  214. </aside>
  215. </div>
  216. </Container>
  217. </main>
  218. );
  219. }