AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings.tsx 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. import { Form, Head } from '@inertiajs/react';
  2. import {
  3. CreditCard,
  4. Megaphone,
  5. MessageCircle,
  6. Package,
  7. Truck,
  8. } from 'lucide-react';
  9. import { useState } from 'react';
  10. import StoreController from '@/actions/App/Http/Controllers/StoreController';
  11. import { BackButton } from '@/components/back-button';
  12. import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
  13. import InputError from '@/components/input-error';
  14. import type { EasyParcelSettings } from '@/components/stores/easyparcel-setup';
  15. import { EasyParcelSetup } from '@/components/stores/easyparcel-setup';
  16. import type { PaymentSettings } from '@/components/stores/get-paid-setup';
  17. import { GetPaidSetup } from '@/components/stores/get-paid-setup';
  18. import type { HitPaySettings } from '@/components/stores/hitpay-setup';
  19. import { HitPaySetup } from '@/components/stores/hitpay-setup';
  20. import SectionTabs from '@/components/stores/section-tabs';
  21. import StoreNav from '@/components/stores/store-nav';
  22. import { Button } from '@/components/ui/button';
  23. import { Checkbox } from '@/components/ui/checkbox';
  24. import {
  25. Card,
  26. CardContent,
  27. CardDescription,
  28. CardHeader,
  29. CardTitle,
  30. } from '@/components/ui/card';
  31. import { Input } from '@/components/ui/input';
  32. import { Label } from '@/components/ui/label';
  33. import { dashboard } from '@/routes';
  34. import type { StoreSummary } from '@/types';
  35. type Settings = {
  36. whatsapp_number: string | null;
  37. contact_email: string | null;
  38. shipping_west: string;
  39. shipping_east: string;
  40. free_shipping_over: string | null;
  41. announcement_enabled: boolean;
  42. /** One message per line. */
  43. announcement_messages: string;
  44. pickup: {
  45. name: string;
  46. phone: string;
  47. line1: string;
  48. line2: string;
  49. city: string;
  50. postcode: string;
  51. state: string;
  52. };
  53. parcel_weight: string;
  54. parcel_length: string;
  55. parcel_width: string;
  56. parcel_height: string;
  57. };
  58. /** Settings cards sit flat inside a tab, split by a line, instead of as boxes inside a box. */
  59. const flatCards =
  60. 'divide-y p-0 [&>[data-slot=card]]:rounded-none [&>[data-slot=card]]:border-0 [&>[data-slot=card]]:bg-transparent [&>[data-slot=card]]:shadow-none';
  61. /** True when the save refused a field starting with one of these names (pickup_ covers every pickup field). */
  62. function hasErrorIn(errors: Record<string, string>, names: string[]): boolean {
  63. return Object.keys(errors).some((field) =>
  64. names.some(
  65. (name) =>
  66. field === name ||
  67. (name.endsWith('_') && field.startsWith(name)),
  68. ),
  69. );
  70. }
  71. export default function StoreSettings({
  72. store,
  73. settings,
  74. automaticAnnouncements,
  75. states,
  76. accountEmail,
  77. payments,
  78. hitpay,
  79. easyparcel,
  80. easyparcelBalance,
  81. }: {
  82. store: StoreSummary;
  83. settings: Settings;
  84. automaticAnnouncements: string[];
  85. states: string[];
  86. /** The merchant's sign-in email, offered as a quick fill for the contact email. */
  87. accountEmail: string;
  88. payments: PaymentSettings;
  89. hitpay: HitPaySettings;
  90. easyparcel: EasyParcelSettings;
  91. /** Loaded after the page shows. Null when EasyParcel is not connected. */
  92. easyparcelBalance?: { amount: number | null; error: string | null } | null;
  93. }) {
  94. const [openTab, setOpenTab] = useState('');
  95. return (
  96. <>
  97. <Head title={`Settings - ${store.name}`} />
  98. <div className="mx-auto w-full max-w-6xl space-y-6 p-4">
  99. <div className="space-y-4">
  100. <BackButton href={StoreController.show.url(store.slug)}>
  101. Back to store
  102. </BackButton>
  103. <h1 className="text-xl font-semibold">{store.name}</h1>
  104. </div>
  105. <StoreNav slug={store.slug} current="settings" />
  106. <Form
  107. {...StoreSettingsController.update.form(store.slug)}
  108. options={{ preserveScroll: true }}
  109. // Fields sit in tabs, and the browser cannot show its own warning on a hidden one, so it would
  110. // block the save silently. The server checks the same rules, and its errors open the right tab.
  111. noValidate
  112. className="max-w-2xl space-y-6"
  113. >
  114. {({ processing, errors }) => (
  115. <>
  116. <SectionTabs
  117. name="settings"
  118. label="Store settings"
  119. onChange={setOpenTab}
  120. panelClassName={flatCards}
  121. tabs={[
  122. {
  123. key: 'payments',
  124. label: 'Payments',
  125. icon: CreditCard,
  126. content: (
  127. <>
  128. <GetPaidSetup
  129. store={store}
  130. payments={payments}
  131. />
  132. <HitPaySetup
  133. store={store}
  134. hitpay={hitpay}
  135. />
  136. </>
  137. ),
  138. },
  139. {
  140. key: 'contact',
  141. label: 'Contact',
  142. icon: MessageCircle,
  143. hasError: hasErrorIn(errors, [
  144. 'whatsapp_number',
  145. 'contact_email',
  146. ]),
  147. content: (
  148. <>
  149. <ContactCard
  150. settings={settings}
  151. accountEmail={accountEmail}
  152. errors={errors}
  153. />
  154. </>
  155. ),
  156. },
  157. {
  158. key: 'shipping',
  159. label: 'Shipping fees',
  160. icon: Truck,
  161. hasError: hasErrorIn(errors, [
  162. 'shipping_',
  163. 'free_shipping_over',
  164. ]),
  165. content: (
  166. <>
  167. <Card>
  168. <CardHeader>
  169. <CardTitle>
  170. Shipping fees
  171. </CardTitle>
  172. <CardDescription>
  173. Added at checkout,
  174. based on the
  175. shopper&rsquo;s
  176. state.
  177. </CardDescription>
  178. </CardHeader>
  179. <CardContent className="space-y-5">
  180. <div className="grid gap-5 sm:grid-cols-2">
  181. <div className="grid gap-2">
  182. <Label htmlFor="shipping_west">
  183. Peninsular
  184. Malaysia
  185. (RM)
  186. </Label>
  187. <Input
  188. id="shipping_west"
  189. name="shipping_west"
  190. inputMode="decimal"
  191. required
  192. defaultValue={
  193. settings.shipping_west
  194. }
  195. />
  196. <InputError
  197. message={
  198. errors.shipping_west
  199. }
  200. />
  201. </div>
  202. <div className="grid gap-2">
  203. <Label htmlFor="shipping_east">
  204. Sabah,
  205. Sarawak and
  206. Labuan (RM)
  207. </Label>
  208. <Input
  209. id="shipping_east"
  210. name="shipping_east"
  211. inputMode="decimal"
  212. required
  213. defaultValue={
  214. settings.shipping_east
  215. }
  216. />
  217. <InputError
  218. message={
  219. errors.shipping_east
  220. }
  221. />
  222. </div>
  223. </div>
  224. <div className="grid gap-2 sm:max-w-[calc(50%-0.625rem)]">
  225. <Label htmlFor="free_shipping_over">
  226. Free shipping on
  227. orders over (RM,
  228. optional)
  229. </Label>
  230. <Input
  231. id="free_shipping_over"
  232. name="free_shipping_over"
  233. inputMode="decimal"
  234. defaultValue={
  235. settings.free_shipping_over ??
  236. ''
  237. }
  238. placeholder="100.00"
  239. aria-describedby="free-help"
  240. />
  241. <p
  242. id="free-help"
  243. className="text-xs text-muted-foreground"
  244. >
  245. Leave empty to
  246. always charge
  247. shipping. Enter
  248. 0 to always ship
  249. free.
  250. </p>
  251. <InputError
  252. message={
  253. errors.free_shipping_over
  254. }
  255. />
  256. </div>
  257. </CardContent>
  258. </Card>
  259. </>
  260. ),
  261. },
  262. {
  263. key: 'couriers',
  264. label: 'Couriers',
  265. icon: Package,
  266. hasError: hasErrorIn(errors, [
  267. 'pickup_',
  268. 'parcel_',
  269. ]),
  270. content: (
  271. <>
  272. <EasyParcelSetup
  273. store={store}
  274. easyparcel={easyparcel}
  275. balance={easyparcelBalance}
  276. hasPickup={
  277. settings.pickup
  278. .line1 !== ''
  279. }
  280. />
  281. <Card id="pickup">
  282. <CardHeader>
  283. <CardTitle>
  284. Pickup address
  285. </CardTitle>
  286. <CardDescription>
  287. Where couriers
  288. collect your
  289. parcels, printed on
  290. every label as the
  291. sender.
  292. </CardDescription>
  293. </CardHeader>
  294. <CardContent className="space-y-5">
  295. <div className="grid gap-5 sm:grid-cols-2">
  296. <TextField
  297. name="pickup_name"
  298. label="Sender name"
  299. value={
  300. settings
  301. .pickup
  302. .name
  303. }
  304. placeholder={
  305. store.name
  306. }
  307. error={
  308. errors.pickup_name
  309. }
  310. maxLength={60}
  311. />
  312. <TextField
  313. name="pickup_phone"
  314. label="Phone for the courier"
  315. value={
  316. settings
  317. .pickup
  318. .phone
  319. }
  320. placeholder="012-345 6789"
  321. error={
  322. errors.pickup_phone
  323. }
  324. maxLength={30}
  325. type="tel"
  326. />
  327. </div>
  328. <TextField
  329. name="pickup_line1"
  330. label="Address"
  331. value={
  332. settings.pickup
  333. .line1
  334. }
  335. placeholder="No. 12, Jalan Mawar 3"
  336. error={
  337. errors.pickup_line1
  338. }
  339. maxLength={100}
  340. />
  341. <TextField
  342. name="pickup_line2"
  343. label="Address line 2 (optional)"
  344. value={
  345. settings.pickup
  346. .line2
  347. }
  348. placeholder="Taman Melati"
  349. error={
  350. errors.pickup_line2
  351. }
  352. maxLength={100}
  353. />
  354. <div className="grid gap-5 sm:grid-cols-3">
  355. <TextField
  356. name="pickup_postcode"
  357. label="Postcode"
  358. value={
  359. settings
  360. .pickup
  361. .postcode
  362. }
  363. placeholder="53100"
  364. error={
  365. errors.pickup_postcode
  366. }
  367. maxLength={5}
  368. inputMode="numeric"
  369. />
  370. <TextField
  371. name="pickup_city"
  372. label="Town or city"
  373. value={
  374. settings
  375. .pickup
  376. .city
  377. }
  378. placeholder="Kuala Lumpur"
  379. error={
  380. errors.pickup_city
  381. }
  382. maxLength={60}
  383. />
  384. <div className="grid gap-2">
  385. <Label htmlFor="pickup_state">
  386. State
  387. </Label>
  388. <select
  389. id="pickup_state"
  390. name="pickup_state"
  391. defaultValue={
  392. settings
  393. .pickup
  394. .state
  395. }
  396. className="h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm"
  397. >
  398. <option value="">
  399. Choose
  400. </option>
  401. {states.map(
  402. (
  403. state,
  404. ) => (
  405. <option
  406. key={
  407. state
  408. }
  409. value={
  410. state
  411. }
  412. >
  413. {
  414. state
  415. }
  416. </option>
  417. ),
  418. )}
  419. </select>
  420. <InputError
  421. message={
  422. errors.pickup_state
  423. }
  424. />
  425. </div>
  426. </div>
  427. <div className="space-y-2 border-t pt-5">
  428. <p className="text-sm font-medium">
  429. Your usual
  430. parcel
  431. </p>
  432. <p className="text-xs text-muted-foreground">
  433. Filled in for
  434. you when booking
  435. a courier. You
  436. can change it
  437. for each order.
  438. </p>
  439. <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
  440. <TextField
  441. name="parcel_weight"
  442. label="Weight (kg)"
  443. value={
  444. settings.parcel_weight
  445. }
  446. error={
  447. errors.parcel_weight
  448. }
  449. inputMode="decimal"
  450. />
  451. <TextField
  452. name="parcel_length"
  453. label="Length (cm)"
  454. value={
  455. settings.parcel_length
  456. }
  457. error={
  458. errors.parcel_length
  459. }
  460. inputMode="numeric"
  461. />
  462. <TextField
  463. name="parcel_width"
  464. label="Width (cm)"
  465. value={
  466. settings.parcel_width
  467. }
  468. error={
  469. errors.parcel_width
  470. }
  471. inputMode="numeric"
  472. />
  473. <TextField
  474. name="parcel_height"
  475. label="Height (cm)"
  476. value={
  477. settings.parcel_height
  478. }
  479. error={
  480. errors.parcel_height
  481. }
  482. inputMode="numeric"
  483. />
  484. </div>
  485. </div>
  486. </CardContent>
  487. </Card>
  488. </>
  489. ),
  490. },
  491. {
  492. key: 'announcement',
  493. label: 'Announcement bar',
  494. icon: Megaphone,
  495. hasError: hasErrorIn(errors, [
  496. 'announcement_',
  497. ]),
  498. content: (
  499. <>
  500. <Card>
  501. <CardHeader>
  502. <CardTitle>
  503. Announcement bar
  504. </CardTitle>
  505. <CardDescription>
  506. A slim bar above
  507. your store's header.
  508. Several messages
  509. take turns.
  510. </CardDescription>
  511. </CardHeader>
  512. <CardContent className="space-y-5">
  513. <div className="flex items-center gap-3">
  514. <input
  515. type="hidden"
  516. name="announcement_enabled"
  517. value="0"
  518. />
  519. <Checkbox
  520. id="announcement_enabled"
  521. name="announcement_enabled"
  522. value="1"
  523. defaultChecked={
  524. settings.announcement_enabled
  525. }
  526. />
  527. <Label htmlFor="announcement_enabled">
  528. Show the
  529. announcement bar
  530. </Label>
  531. </div>
  532. <div className="grid gap-2">
  533. <Label htmlFor="announcement_messages">
  534. Your own
  535. messages
  536. (optional, one
  537. per line, up to
  538. 3)
  539. </Label>
  540. <textarea
  541. id="announcement_messages"
  542. name="announcement_messages"
  543. rows={3}
  544. maxLength={400}
  545. defaultValue={
  546. settings.announcement_messages
  547. }
  548. placeholder={
  549. 'Raya orders close 25 March\nNew: Sambal Bilis is here'
  550. }
  551. className="w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm"
  552. />
  553. <InputError
  554. message={
  555. errors.announcement_messages
  556. }
  557. />
  558. </div>
  559. <div className="space-y-1.5 rounded-md bg-muted/50 p-3 text-sm">
  560. <p className="font-medium">
  561. Added by
  562. themselves
  563. </p>
  564. {automaticAnnouncements.length >
  565. 0 ? (
  566. <ul
  567. className="list-disc space-y-0.5 pl-5 text-muted-foreground"
  568. data-test="automatic-announcements"
  569. >
  570. {automaticAnnouncements.map(
  571. (
  572. line,
  573. ) => (
  574. <li
  575. key={
  576. line
  577. }
  578. >
  579. {
  580. line
  581. }
  582. </li>
  583. ),
  584. )}
  585. </ul>
  586. ) : (
  587. <p className="text-muted-foreground">
  588. Nothing
  589. right now.
  590. Free
  591. shipping and
  592. running
  593. automatic
  594. discounts
  595. show here by
  596. themselves.
  597. </p>
  598. )}
  599. </div>
  600. </CardContent>
  601. </Card>
  602. </>
  603. ),
  604. },
  605. ]}
  606. />
  607. {/* Payments are saved by their own buttons; this saves the other tabs. */}
  608. {openTab !== 'payments' && (
  609. <Button
  610. disabled={processing}
  611. data-test="save-settings-button"
  612. >
  613. Save settings
  614. </Button>
  615. )}
  616. </>
  617. )}
  618. </Form>
  619. </div>
  620. </>
  621. );
  622. }
  623. function TextField({
  624. name,
  625. label,
  626. value,
  627. error,
  628. placeholder,
  629. maxLength,
  630. type = 'text',
  631. inputMode,
  632. }: {
  633. name: string;
  634. label: string;
  635. value: string;
  636. error?: string;
  637. placeholder?: string;
  638. maxLength?: number;
  639. type?: string;
  640. inputMode?: 'text' | 'numeric' | 'decimal' | 'tel';
  641. }) {
  642. return (
  643. <div className="grid gap-2">
  644. <Label htmlFor={name}>{label}</Label>
  645. <Input
  646. id={name}
  647. name={name}
  648. type={type}
  649. inputMode={inputMode}
  650. maxLength={maxLength}
  651. defaultValue={value}
  652. placeholder={placeholder}
  653. />
  654. <InputError message={error} />
  655. </div>
  656. );
  657. }
  658. StoreSettings.layout = {
  659. breadcrumbs: [
  660. { title: 'Overview', href: dashboard() },
  661. { title: 'Settings', href: '#' },
  662. ],
  663. };
  664. /**
  665. * How shoppers reach the store: the WhatsApp button and the email address their replies go to.
  666. */
  667. function ContactCard({
  668. settings,
  669. accountEmail,
  670. errors,
  671. }: {
  672. settings: Settings;
  673. accountEmail: string;
  674. errors: Record<string, string | undefined>;
  675. }) {
  676. const [email, setEmail] = useState(settings.contact_email ?? '');
  677. return (
  678. <Card id="contact">
  679. <CardHeader>
  680. <CardTitle>How shoppers reach you</CardTitle>
  681. <CardDescription>
  682. How shoppers contact you about their orders.
  683. </CardDescription>
  684. </CardHeader>
  685. <CardContent className="space-y-5">
  686. <div className="grid gap-2">
  687. <Label htmlFor="whatsapp_number">WhatsApp number</Label>
  688. <Input
  689. id="whatsapp_number"
  690. name="whatsapp_number"
  691. type="tel"
  692. inputMode="tel"
  693. maxLength={30}
  694. defaultValue={settings.whatsapp_number ?? ''}
  695. placeholder="012-345 6789"
  696. />
  697. <p className="text-xs text-muted-foreground">
  698. Adds a WhatsApp chat button to every page, the order
  699. page and the order emails. Leave empty to hide it.
  700. </p>
  701. <InputError message={errors.whatsapp_number} />
  702. </div>
  703. <div className="grid gap-2">
  704. <Label htmlFor="contact_email">Email for shoppers</Label>
  705. <Input
  706. id="contact_email"
  707. name="contact_email"
  708. type="email"
  709. maxLength={160}
  710. value={email}
  711. onChange={(event) => setEmail(event.target.value)}
  712. placeholder="hello@yourstore.com"
  713. />
  714. <p className="text-xs text-muted-foreground">
  715. When a shopper replies to an order email, it comes here.
  716. It is also shown at the bottom of those emails. Leave
  717. empty and shoppers cannot reply by email.
  718. </p>
  719. {email === '' && accountEmail !== '' && (
  720. <button
  721. type="button"
  722. onClick={() => setEmail(accountEmail)}
  723. className="w-fit text-xs font-medium text-foreground underline underline-offset-2"
  724. data-test="use-account-email"
  725. >
  726. Use {accountEmail}
  727. </button>
  728. )}
  729. <InputError message={errors.contact_email} />
  730. </div>
  731. </CardContent>
  732. </Card>
  733. );
  734. }