| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- import { Form, Head } from '@inertiajs/react';
- import {
- CreditCard,
- Megaphone,
- MessageCircle,
- Package,
- Truck,
- } from 'lucide-react';
- import { useState } from 'react';
- import StoreController from '@/actions/App/Http/Controllers/StoreController';
- import { BackButton } from '@/components/back-button';
- import StoreSettingsController from '@/actions/App/Http/Controllers/StoreSettingsController';
- import InputError from '@/components/input-error';
- import type { EasyParcelSettings } from '@/components/stores/easyparcel-setup';
- import { EasyParcelSetup } from '@/components/stores/easyparcel-setup';
- import type { PaymentSettings } from '@/components/stores/get-paid-setup';
- import { GetPaidSetup } from '@/components/stores/get-paid-setup';
- import type { HitPaySettings } from '@/components/stores/hitpay-setup';
- import { HitPaySetup } from '@/components/stores/hitpay-setup';
- import SectionTabs from '@/components/stores/section-tabs';
- import StoreNav from '@/components/stores/store-nav';
- import { Button } from '@/components/ui/button';
- import { Checkbox } from '@/components/ui/checkbox';
- import {
- Card,
- CardContent,
- CardDescription,
- CardHeader,
- CardTitle,
- } from '@/components/ui/card';
- import { Input } from '@/components/ui/input';
- import { Label } from '@/components/ui/label';
- import { dashboard } from '@/routes';
- import type { StoreSummary } from '@/types';
-
- type Settings = {
- whatsapp_number: string | null;
- contact_email: string | null;
- shipping_west: string;
- shipping_east: string;
- free_shipping_over: string | null;
- announcement_enabled: boolean;
- /** One message per line. */
- announcement_messages: string;
- pickup: {
- name: string;
- phone: string;
- line1: string;
- line2: string;
- city: string;
- postcode: string;
- state: string;
- };
- parcel_weight: string;
- parcel_length: string;
- parcel_width: string;
- parcel_height: string;
- };
-
- /** Settings cards sit flat inside a tab, split by a line, instead of as boxes inside a box. */
- const flatCards =
- 'divide-y p-0 [&>[data-slot=card]]:rounded-none [&>[data-slot=card]]:border-0 [&>[data-slot=card]]:bg-transparent [&>[data-slot=card]]:shadow-none';
-
- /** True when the save refused a field starting with one of these names (pickup_ covers every pickup field). */
- function hasErrorIn(errors: Record<string, string>, names: string[]): boolean {
- return Object.keys(errors).some((field) =>
- names.some(
- (name) =>
- field === name ||
- (name.endsWith('_') && field.startsWith(name)),
- ),
- );
- }
-
- export default function StoreSettings({
- store,
- settings,
- automaticAnnouncements,
- states,
- accountEmail,
- payments,
- hitpay,
- easyparcel,
- easyparcelBalance,
- }: {
- store: StoreSummary;
- settings: Settings;
- automaticAnnouncements: string[];
- states: string[];
- /** The merchant's sign-in email, offered as a quick fill for the contact email. */
- accountEmail: string;
- payments: PaymentSettings;
- hitpay: HitPaySettings;
- easyparcel: EasyParcelSettings;
- /** Loaded after the page shows. Null when EasyParcel is not connected. */
- easyparcelBalance?: { amount: number | null; error: string | null } | null;
- }) {
- const [openTab, setOpenTab] = useState('');
-
- return (
- <>
- <Head title={`Settings - ${store.name}`} />
-
- <div className="mx-auto w-full max-w-6xl space-y-6 p-4">
- <div className="space-y-4">
- <BackButton href={StoreController.show.url(store.slug)}>
- Back to store
- </BackButton>
- <h1 className="text-xl font-semibold">{store.name}</h1>
- </div>
-
- <StoreNav slug={store.slug} current="settings" />
-
- <Form
- {...StoreSettingsController.update.form(store.slug)}
- options={{ preserveScroll: true }}
- // Fields sit in tabs, and the browser cannot show its own warning on a hidden one, so it would
- // block the save silently. The server checks the same rules, and its errors open the right tab.
- noValidate
- className="max-w-2xl space-y-6"
- >
- {({ processing, errors }) => (
- <>
- <SectionTabs
- name="settings"
- label="Store settings"
- onChange={setOpenTab}
- panelClassName={flatCards}
- tabs={[
- {
- key: 'payments',
- label: 'Payments',
- icon: CreditCard,
- content: (
- <>
- <GetPaidSetup
- store={store}
- payments={payments}
- />
-
- <HitPaySetup
- store={store}
- hitpay={hitpay}
- />
- </>
- ),
- },
- {
- key: 'contact',
- label: 'Contact',
- icon: MessageCircle,
- hasError: hasErrorIn(errors, [
- 'whatsapp_number',
- 'contact_email',
- ]),
- content: (
- <>
- <ContactCard
- settings={settings}
- accountEmail={accountEmail}
- errors={errors}
- />
- </>
- ),
- },
- {
- key: 'shipping',
- label: 'Shipping fees',
- icon: Truck,
- hasError: hasErrorIn(errors, [
- 'shipping_',
- 'free_shipping_over',
- ]),
- content: (
- <>
- <Card>
- <CardHeader>
- <CardTitle>
- Shipping fees
- </CardTitle>
- <CardDescription>
- Added at checkout,
- based on the
- shopper’s
- state.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-5">
- <div className="grid gap-5 sm:grid-cols-2">
- <div className="grid gap-2">
- <Label htmlFor="shipping_west">
- Peninsular
- Malaysia
- (RM)
- </Label>
- <Input
- id="shipping_west"
- name="shipping_west"
- inputMode="decimal"
- required
- defaultValue={
- settings.shipping_west
- }
- />
- <InputError
- message={
- errors.shipping_west
- }
- />
- </div>
- <div className="grid gap-2">
- <Label htmlFor="shipping_east">
- Sabah,
- Sarawak and
- Labuan (RM)
- </Label>
- <Input
- id="shipping_east"
- name="shipping_east"
- inputMode="decimal"
- required
- defaultValue={
- settings.shipping_east
- }
- />
- <InputError
- message={
- errors.shipping_east
- }
- />
- </div>
- </div>
-
- <div className="grid gap-2 sm:max-w-[calc(50%-0.625rem)]">
- <Label htmlFor="free_shipping_over">
- Free shipping on
- orders over (RM,
- optional)
- </Label>
- <Input
- id="free_shipping_over"
- name="free_shipping_over"
- inputMode="decimal"
- defaultValue={
- settings.free_shipping_over ??
- ''
- }
- placeholder="100.00"
- aria-describedby="free-help"
- />
- <p
- id="free-help"
- className="text-xs text-muted-foreground"
- >
- Leave empty to
- always charge
- shipping. Enter
- 0 to always ship
- free.
- </p>
- <InputError
- message={
- errors.free_shipping_over
- }
- />
- </div>
- </CardContent>
- </Card>
- </>
- ),
- },
- {
- key: 'couriers',
- label: 'Couriers',
- icon: Package,
- hasError: hasErrorIn(errors, [
- 'pickup_',
- 'parcel_',
- ]),
- content: (
- <>
- <EasyParcelSetup
- store={store}
- easyparcel={easyparcel}
- balance={easyparcelBalance}
- hasPickup={
- settings.pickup
- .line1 !== ''
- }
- />
-
- <Card id="pickup">
- <CardHeader>
- <CardTitle>
- Pickup address
- </CardTitle>
- <CardDescription>
- Where couriers
- collect your
- parcels, printed on
- every label as the
- sender.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-5">
- <div className="grid gap-5 sm:grid-cols-2">
- <TextField
- name="pickup_name"
- label="Sender name"
- value={
- settings
- .pickup
- .name
- }
- placeholder={
- store.name
- }
- error={
- errors.pickup_name
- }
- maxLength={60}
- />
- <TextField
- name="pickup_phone"
- label="Phone for the courier"
- value={
- settings
- .pickup
- .phone
- }
- placeholder="012-345 6789"
- error={
- errors.pickup_phone
- }
- maxLength={30}
- type="tel"
- />
- </div>
- <TextField
- name="pickup_line1"
- label="Address"
- value={
- settings.pickup
- .line1
- }
- placeholder="No. 12, Jalan Mawar 3"
- error={
- errors.pickup_line1
- }
- maxLength={100}
- />
- <TextField
- name="pickup_line2"
- label="Address line 2 (optional)"
- value={
- settings.pickup
- .line2
- }
- placeholder="Taman Melati"
- error={
- errors.pickup_line2
- }
- maxLength={100}
- />
- <div className="grid gap-5 sm:grid-cols-3">
- <TextField
- name="pickup_postcode"
- label="Postcode"
- value={
- settings
- .pickup
- .postcode
- }
- placeholder="53100"
- error={
- errors.pickup_postcode
- }
- maxLength={5}
- inputMode="numeric"
- />
- <TextField
- name="pickup_city"
- label="Town or city"
- value={
- settings
- .pickup
- .city
- }
- placeholder="Kuala Lumpur"
- error={
- errors.pickup_city
- }
- maxLength={60}
- />
- <div className="grid gap-2">
- <Label htmlFor="pickup_state">
- State
- </Label>
- <select
- id="pickup_state"
- name="pickup_state"
- defaultValue={
- settings
- .pickup
- .state
- }
- 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"
- >
- <option value="">
- Choose
- </option>
- {states.map(
- (
- state,
- ) => (
- <option
- key={
- state
- }
- value={
- state
- }
- >
- {
- state
- }
- </option>
- ),
- )}
- </select>
- <InputError
- message={
- errors.pickup_state
- }
- />
- </div>
- </div>
-
- <div className="space-y-2 border-t pt-5">
- <p className="text-sm font-medium">
- Your usual
- parcel
- </p>
- <p className="text-xs text-muted-foreground">
- Filled in for
- you when booking
- a courier. You
- can change it
- for each order.
- </p>
- <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
- <TextField
- name="parcel_weight"
- label="Weight (kg)"
- value={
- settings.parcel_weight
- }
- error={
- errors.parcel_weight
- }
- inputMode="decimal"
- />
- <TextField
- name="parcel_length"
- label="Length (cm)"
- value={
- settings.parcel_length
- }
- error={
- errors.parcel_length
- }
- inputMode="numeric"
- />
- <TextField
- name="parcel_width"
- label="Width (cm)"
- value={
- settings.parcel_width
- }
- error={
- errors.parcel_width
- }
- inputMode="numeric"
- />
- <TextField
- name="parcel_height"
- label="Height (cm)"
- value={
- settings.parcel_height
- }
- error={
- errors.parcel_height
- }
- inputMode="numeric"
- />
- </div>
- </div>
- </CardContent>
- </Card>
- </>
- ),
- },
- {
- key: 'announcement',
- label: 'Announcement bar',
- icon: Megaphone,
- hasError: hasErrorIn(errors, [
- 'announcement_',
- ]),
- content: (
- <>
- <Card>
- <CardHeader>
- <CardTitle>
- Announcement bar
- </CardTitle>
- <CardDescription>
- A slim bar above
- your store's header.
- Several messages
- take turns.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-5">
- <div className="flex items-center gap-3">
- <input
- type="hidden"
- name="announcement_enabled"
- value="0"
- />
- <Checkbox
- id="announcement_enabled"
- name="announcement_enabled"
- value="1"
- defaultChecked={
- settings.announcement_enabled
- }
- />
- <Label htmlFor="announcement_enabled">
- Show the
- announcement bar
- </Label>
- </div>
-
- <div className="grid gap-2">
- <Label htmlFor="announcement_messages">
- Your own
- messages
- (optional, one
- per line, up to
- 3)
- </Label>
- <textarea
- id="announcement_messages"
- name="announcement_messages"
- rows={3}
- maxLength={400}
- defaultValue={
- settings.announcement_messages
- }
- placeholder={
- 'Raya orders close 25 March\nNew: Sambal Bilis is here'
- }
- 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"
- />
- <InputError
- message={
- errors.announcement_messages
- }
- />
- </div>
-
- <div className="space-y-1.5 rounded-md bg-muted/50 p-3 text-sm">
- <p className="font-medium">
- Added by
- themselves
- </p>
- {automaticAnnouncements.length >
- 0 ? (
- <ul
- className="list-disc space-y-0.5 pl-5 text-muted-foreground"
- data-test="automatic-announcements"
- >
- {automaticAnnouncements.map(
- (
- line,
- ) => (
- <li
- key={
- line
- }
- >
- {
- line
- }
- </li>
- ),
- )}
- </ul>
- ) : (
- <p className="text-muted-foreground">
- Nothing
- right now.
- Free
- shipping and
- running
- automatic
- discounts
- show here by
- themselves.
- </p>
- )}
- </div>
- </CardContent>
- </Card>
- </>
- ),
- },
- ]}
- />
-
- {/* Payments are saved by their own buttons; this saves the other tabs. */}
- {openTab !== 'payments' && (
- <Button
- disabled={processing}
- data-test="save-settings-button"
- >
- Save settings
- </Button>
- )}
- </>
- )}
- </Form>
- </div>
- </>
- );
- }
-
- function TextField({
- name,
- label,
- value,
- error,
- placeholder,
- maxLength,
- type = 'text',
- inputMode,
- }: {
- name: string;
- label: string;
- value: string;
- error?: string;
- placeholder?: string;
- maxLength?: number;
- type?: string;
- inputMode?: 'text' | 'numeric' | 'decimal' | 'tel';
- }) {
- return (
- <div className="grid gap-2">
- <Label htmlFor={name}>{label}</Label>
- <Input
- id={name}
- name={name}
- type={type}
- inputMode={inputMode}
- maxLength={maxLength}
- defaultValue={value}
- placeholder={placeholder}
- />
- <InputError message={error} />
- </div>
- );
- }
-
- StoreSettings.layout = {
- breadcrumbs: [
- { title: 'Overview', href: dashboard() },
- { title: 'Settings', href: '#' },
- ],
- };
-
- /**
- * How shoppers reach the store: the WhatsApp button and the email address their replies go to.
- */
- function ContactCard({
- settings,
- accountEmail,
- errors,
- }: {
- settings: Settings;
- accountEmail: string;
- errors: Record<string, string | undefined>;
- }) {
- const [email, setEmail] = useState(settings.contact_email ?? '');
-
- return (
- <Card id="contact">
- <CardHeader>
- <CardTitle>How shoppers reach you</CardTitle>
- <CardDescription>
- How shoppers contact you about their orders.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-5">
- <div className="grid gap-2">
- <Label htmlFor="whatsapp_number">WhatsApp number</Label>
- <Input
- id="whatsapp_number"
- name="whatsapp_number"
- type="tel"
- inputMode="tel"
- maxLength={30}
- defaultValue={settings.whatsapp_number ?? ''}
- placeholder="012-345 6789"
- />
- <p className="text-xs text-muted-foreground">
- Adds a WhatsApp chat button to every page, the order
- page and the order emails. Leave empty to hide it.
- </p>
- <InputError message={errors.whatsapp_number} />
- </div>
-
- <div className="grid gap-2">
- <Label htmlFor="contact_email">Email for shoppers</Label>
- <Input
- id="contact_email"
- name="contact_email"
- type="email"
- maxLength={160}
- value={email}
- onChange={(event) => setEmail(event.target.value)}
- placeholder="hello@yourstore.com"
- />
- <p className="text-xs text-muted-foreground">
- When a shopper replies to an order email, it comes here.
- It is also shown at the bottom of those emails. Leave
- empty and shoppers cannot reply by email.
- </p>
- {email === '' && accountEmail !== '' && (
- <button
- type="button"
- onClick={() => setEmail(accountEmail)}
- className="w-fit text-xs font-medium text-foreground underline underline-offset-2"
- data-test="use-account-email"
- >
- Use {accountEmail}
- </button>
- )}
- <InputError message={errors.contact_email} />
- </div>
- </CardContent>
- </Card>
- );
- }
|