AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

settings.tsx 23KB

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