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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. import { Form, Head } from '@inertiajs/react';
  2. import { useState } from 'react';
  3. import StoreController from '@/actions/App/Http/Controllers/StoreController';
  4. import { BackButton } from '@/components/back-button';
  5. import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
  6. import InputError from '@/components/input-error';
  7. import type { EasyParcelSettings } from '@/components/stores/easyparcel-setup';
  8. import { EasyParcelSetup } from '@/components/stores/easyparcel-setup';
  9. import type { PaymentSettings } from '@/components/stores/get-paid-setup';
  10. import { GetPaidSetup } from '@/components/stores/get-paid-setup';
  11. import type { HitPaySettings } from '@/components/stores/hitpay-setup';
  12. import { HitPaySetup } from '@/components/stores/hitpay-setup';
  13. import StoreNav from '@/components/stores/store-nav';
  14. import { Button } from '@/components/ui/button';
  15. import { Checkbox } from '@/components/ui/checkbox';
  16. import {
  17. Card,
  18. CardContent,
  19. CardDescription,
  20. CardHeader,
  21. CardTitle,
  22. } from '@/components/ui/card';
  23. import { Input } from '@/components/ui/input';
  24. import { Label } from '@/components/ui/label';
  25. import { dashboard } from '@/routes';
  26. import type { StoreSummary } from '@/types';
  27. type Settings = {
  28. whatsapp_number: string | null;
  29. contact_email: string | null;
  30. shipping_west: string;
  31. shipping_east: string;
  32. free_shipping_over: string | null;
  33. announcement_enabled: boolean;
  34. /** One message per line. */
  35. announcement_messages: string;
  36. pickup: {
  37. name: string;
  38. phone: string;
  39. line1: string;
  40. line2: string;
  41. city: string;
  42. postcode: string;
  43. state: string;
  44. };
  45. parcel_weight: string;
  46. parcel_length: string;
  47. parcel_width: string;
  48. parcel_height: string;
  49. };
  50. export default function StoreSettings({
  51. store,
  52. settings,
  53. automaticAnnouncements,
  54. states,
  55. accountEmail,
  56. payments,
  57. hitpay,
  58. easyparcel,
  59. easyparcelBalance,
  60. }: {
  61. store: StoreSummary;
  62. settings: Settings;
  63. automaticAnnouncements: string[];
  64. states: string[];
  65. /** The merchant's sign-in email, offered as a quick fill for the contact email. */
  66. accountEmail: string;
  67. payments: PaymentSettings;
  68. hitpay: HitPaySettings;
  69. easyparcel: EasyParcelSettings;
  70. /** Loaded after the page shows. Null when EasyParcel is not connected. */
  71. easyparcelBalance?: { amount: number | null; error: string | null } | null;
  72. }) {
  73. return (
  74. <>
  75. <Head title={`Settings - ${store.name}`} />
  76. <div className="mx-auto w-full max-w-6xl space-y-6 p-4">
  77. <div className="space-y-4">
  78. <BackButton href={StoreController.show.url(store.slug)}>
  79. Back to store
  80. </BackButton>
  81. <h1 className="text-xl font-semibold">{store.name}</h1>
  82. </div>
  83. <StoreNav slug={store.slug} current="settings" />
  84. <Form
  85. {...StoreSettingsController.update.form(store.slug)}
  86. options={{ preserveScroll: true }}
  87. className="max-w-2xl space-y-6"
  88. >
  89. {({ processing, errors }) => (
  90. <>
  91. <GetPaidSetup store={store} payments={payments} />
  92. <HitPaySetup store={store} hitpay={hitpay} />
  93. <ContactCard
  94. settings={settings}
  95. accountEmail={accountEmail}
  96. errors={errors}
  97. />
  98. <Card>
  99. <CardHeader>
  100. <CardTitle>Shipping fees</CardTitle>
  101. <CardDescription>
  102. Added at checkout, based on the
  103. shopper&rsquo;s state.
  104. </CardDescription>
  105. </CardHeader>
  106. <CardContent className="space-y-5">
  107. <div className="grid gap-5 sm:grid-cols-2">
  108. <div className="grid gap-2">
  109. <Label htmlFor="shipping_west">
  110. Peninsular Malaysia (RM)
  111. </Label>
  112. <Input
  113. id="shipping_west"
  114. name="shipping_west"
  115. inputMode="decimal"
  116. required
  117. defaultValue={
  118. settings.shipping_west
  119. }
  120. />
  121. <InputError
  122. message={errors.shipping_west}
  123. />
  124. </div>
  125. <div className="grid gap-2">
  126. <Label htmlFor="shipping_east">
  127. Sabah, Sarawak and Labuan (RM)
  128. </Label>
  129. <Input
  130. id="shipping_east"
  131. name="shipping_east"
  132. inputMode="decimal"
  133. required
  134. defaultValue={
  135. settings.shipping_east
  136. }
  137. />
  138. <InputError
  139. message={errors.shipping_east}
  140. />
  141. </div>
  142. </div>
  143. <div className="grid gap-2 sm:max-w-[calc(50%-0.625rem)]">
  144. <Label htmlFor="free_shipping_over">
  145. Free shipping on orders over (RM,
  146. optional)
  147. </Label>
  148. <Input
  149. id="free_shipping_over"
  150. name="free_shipping_over"
  151. inputMode="decimal"
  152. defaultValue={
  153. settings.free_shipping_over ??
  154. ''
  155. }
  156. placeholder="100.00"
  157. aria-describedby="free-help"
  158. />
  159. <p
  160. id="free-help"
  161. className="text-xs text-muted-foreground"
  162. >
  163. Leave empty to always charge
  164. shipping. Enter 0 to always ship
  165. free.
  166. </p>
  167. <InputError
  168. message={errors.free_shipping_over}
  169. />
  170. </div>
  171. </CardContent>
  172. </Card>
  173. <EasyParcelSetup
  174. store={store}
  175. easyparcel={easyparcel}
  176. balance={easyparcelBalance}
  177. hasPickup={settings.pickup.line1 !== ''}
  178. />
  179. <Card id="pickup">
  180. <CardHeader>
  181. <CardTitle>Pickup address</CardTitle>
  182. <CardDescription>
  183. Where couriers collect your parcels,
  184. printed on every label as the sender.
  185. </CardDescription>
  186. </CardHeader>
  187. <CardContent className="space-y-5">
  188. <div className="grid gap-5 sm:grid-cols-2">
  189. <TextField
  190. name="pickup_name"
  191. label="Sender name"
  192. value={settings.pickup.name}
  193. placeholder={store.name}
  194. error={errors.pickup_name}
  195. maxLength={60}
  196. />
  197. <TextField
  198. name="pickup_phone"
  199. label="Phone for the courier"
  200. value={settings.pickup.phone}
  201. placeholder="012-345 6789"
  202. error={errors.pickup_phone}
  203. maxLength={30}
  204. type="tel"
  205. />
  206. </div>
  207. <TextField
  208. name="pickup_line1"
  209. label="Address"
  210. value={settings.pickup.line1}
  211. placeholder="No. 12, Jalan Mawar 3"
  212. error={errors.pickup_line1}
  213. maxLength={100}
  214. />
  215. <TextField
  216. name="pickup_line2"
  217. label="Address line 2 (optional)"
  218. value={settings.pickup.line2}
  219. placeholder="Taman Melati"
  220. error={errors.pickup_line2}
  221. maxLength={100}
  222. />
  223. <div className="grid gap-5 sm:grid-cols-3">
  224. <TextField
  225. name="pickup_postcode"
  226. label="Postcode"
  227. value={settings.pickup.postcode}
  228. placeholder="53100"
  229. error={errors.pickup_postcode}
  230. maxLength={5}
  231. inputMode="numeric"
  232. />
  233. <TextField
  234. name="pickup_city"
  235. label="Town or city"
  236. value={settings.pickup.city}
  237. placeholder="Kuala Lumpur"
  238. error={errors.pickup_city}
  239. maxLength={60}
  240. />
  241. <div className="grid gap-2">
  242. <Label htmlFor="pickup_state">
  243. State
  244. </Label>
  245. <select
  246. id="pickup_state"
  247. name="pickup_state"
  248. defaultValue={
  249. settings.pickup.state
  250. }
  251. 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"
  252. >
  253. <option value="">Choose</option>
  254. {states.map((state) => (
  255. <option
  256. key={state}
  257. value={state}
  258. >
  259. {state}
  260. </option>
  261. ))}
  262. </select>
  263. <InputError
  264. message={errors.pickup_state}
  265. />
  266. </div>
  267. </div>
  268. <div className="space-y-2 border-t pt-5">
  269. <p className="text-sm font-medium">
  270. Your usual parcel
  271. </p>
  272. <p className="text-xs text-muted-foreground">
  273. Filled in for you when booking a
  274. courier. You can change it for each
  275. order.
  276. </p>
  277. <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
  278. <TextField
  279. name="parcel_weight"
  280. label="Weight (kg)"
  281. value={settings.parcel_weight}
  282. error={errors.parcel_weight}
  283. inputMode="decimal"
  284. />
  285. <TextField
  286. name="parcel_length"
  287. label="Length (cm)"
  288. value={settings.parcel_length}
  289. error={errors.parcel_length}
  290. inputMode="numeric"
  291. />
  292. <TextField
  293. name="parcel_width"
  294. label="Width (cm)"
  295. value={settings.parcel_width}
  296. error={errors.parcel_width}
  297. inputMode="numeric"
  298. />
  299. <TextField
  300. name="parcel_height"
  301. label="Height (cm)"
  302. value={settings.parcel_height}
  303. error={errors.parcel_height}
  304. inputMode="numeric"
  305. />
  306. </div>
  307. </div>
  308. </CardContent>
  309. </Card>
  310. <Card>
  311. <CardHeader>
  312. <CardTitle>Announcement bar</CardTitle>
  313. <CardDescription>
  314. A slim bar above your store's header.
  315. Several messages take turns.
  316. </CardDescription>
  317. </CardHeader>
  318. <CardContent className="space-y-5">
  319. <div className="flex items-center gap-3">
  320. <input
  321. type="hidden"
  322. name="announcement_enabled"
  323. value="0"
  324. />
  325. <Checkbox
  326. id="announcement_enabled"
  327. name="announcement_enabled"
  328. value="1"
  329. defaultChecked={
  330. settings.announcement_enabled
  331. }
  332. />
  333. <Label htmlFor="announcement_enabled">
  334. Show the announcement bar
  335. </Label>
  336. </div>
  337. <div className="grid gap-2">
  338. <Label htmlFor="announcement_messages">
  339. Your own messages (optional, one per
  340. line, up to 3)
  341. </Label>
  342. <textarea
  343. id="announcement_messages"
  344. name="announcement_messages"
  345. rows={3}
  346. maxLength={400}
  347. defaultValue={
  348. settings.announcement_messages
  349. }
  350. placeholder={
  351. 'Raya orders close 25 March\nNew: Sambal Bilis is here'
  352. }
  353. 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"
  354. />
  355. <InputError
  356. message={
  357. errors.announcement_messages
  358. }
  359. />
  360. </div>
  361. <div className="space-y-1.5 rounded-md bg-muted/50 p-3 text-sm">
  362. <p className="font-medium">
  363. Added by themselves
  364. </p>
  365. {automaticAnnouncements.length > 0 ? (
  366. <ul
  367. className="list-disc space-y-0.5 pl-5 text-muted-foreground"
  368. data-test="automatic-announcements"
  369. >
  370. {automaticAnnouncements.map(
  371. (line) => (
  372. <li key={line}>
  373. {line}
  374. </li>
  375. ),
  376. )}
  377. </ul>
  378. ) : (
  379. <p className="text-muted-foreground">
  380. Nothing right now. Free shipping
  381. and running automatic discounts
  382. show here by themselves.
  383. </p>
  384. )}
  385. </div>
  386. </CardContent>
  387. </Card>
  388. <Button
  389. disabled={processing}
  390. data-test="save-settings-button"
  391. >
  392. Save settings
  393. </Button>
  394. </>
  395. )}
  396. </Form>
  397. </div>
  398. </>
  399. );
  400. }
  401. function TextField({
  402. name,
  403. label,
  404. value,
  405. error,
  406. placeholder,
  407. maxLength,
  408. type = 'text',
  409. inputMode,
  410. }: {
  411. name: string;
  412. label: string;
  413. value: string;
  414. error?: string;
  415. placeholder?: string;
  416. maxLength?: number;
  417. type?: string;
  418. inputMode?: 'text' | 'numeric' | 'decimal' | 'tel';
  419. }) {
  420. return (
  421. <div className="grid gap-2">
  422. <Label htmlFor={name}>{label}</Label>
  423. <Input
  424. id={name}
  425. name={name}
  426. type={type}
  427. inputMode={inputMode}
  428. maxLength={maxLength}
  429. defaultValue={value}
  430. placeholder={placeholder}
  431. />
  432. <InputError message={error} />
  433. </div>
  434. );
  435. }
  436. StoreSettings.layout = {
  437. breadcrumbs: [
  438. { title: 'Overview', href: dashboard() },
  439. { title: 'Settings', href: '#' },
  440. ],
  441. };
  442. /**
  443. * How shoppers reach the store: the WhatsApp button and the email address their replies go to.
  444. */
  445. function ContactCard({
  446. settings,
  447. accountEmail,
  448. errors,
  449. }: {
  450. settings: Settings;
  451. accountEmail: string;
  452. errors: Record<string, string | undefined>;
  453. }) {
  454. const [email, setEmail] = useState(settings.contact_email ?? '');
  455. return (
  456. <Card id="contact">
  457. <CardHeader>
  458. <CardTitle>How shoppers reach you</CardTitle>
  459. <CardDescription>
  460. How shoppers contact you about their orders.
  461. </CardDescription>
  462. </CardHeader>
  463. <CardContent className="space-y-5">
  464. <div className="grid gap-2">
  465. <Label htmlFor="whatsapp_number">WhatsApp number</Label>
  466. <Input
  467. id="whatsapp_number"
  468. name="whatsapp_number"
  469. type="tel"
  470. inputMode="tel"
  471. maxLength={30}
  472. defaultValue={settings.whatsapp_number ?? ''}
  473. placeholder="012-345 6789"
  474. />
  475. <p className="text-xs text-muted-foreground">
  476. Adds a WhatsApp chat button to every page, the order
  477. page and the order emails. Leave empty to hide it.
  478. </p>
  479. <InputError message={errors.whatsapp_number} />
  480. </div>
  481. <div className="grid gap-2">
  482. <Label htmlFor="contact_email">Email for shoppers</Label>
  483. <Input
  484. id="contact_email"
  485. name="contact_email"
  486. type="email"
  487. maxLength={160}
  488. value={email}
  489. onChange={(event) => setEmail(event.target.value)}
  490. placeholder="hello@yourstore.com"
  491. />
  492. <p className="text-xs text-muted-foreground">
  493. When a shopper replies to an order email, it comes here.
  494. It is also shown at the bottom of those emails. Leave
  495. empty and shoppers cannot reply by email.
  496. </p>
  497. {email === '' && accountEmail !== '' && (
  498. <button
  499. type="button"
  500. onClick={() => setEmail(accountEmail)}
  501. className="w-fit text-xs font-medium text-foreground underline underline-offset-2"
  502. data-test="use-account-email"
  503. >
  504. Use {accountEmail}
  505. </button>
  506. )}
  507. <InputError message={errors.contact_email} />
  508. </div>
  509. </CardContent>
  510. </Card>
  511. );
  512. }