| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- import { Form, Head } from '@inertiajs/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 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;
- 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;
- };
-
- export default function StoreSettings({
- store,
- settings,
- automaticAnnouncements,
- states,
- easyparcel,
- easyparcelBalance,
- }: {
- store: StoreSummary;
- settings: Settings;
- automaticAnnouncements: string[];
- states: string[];
- easyparcel: EasyParcelSettings;
- /** Loaded after the page shows. Null when EasyParcel is not connected. */
- easyparcelBalance?: { amount: number | null; error: string | null } | null;
- }) {
- 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 }}
- className="max-w-2xl space-y-6"
- >
- {({ processing, errors }) => (
- <>
- <Card>
- <CardHeader>
- <CardTitle>WhatsApp</CardTitle>
- <CardDescription>
- Shoppers see a WhatsApp chat button on
- every page of your store, and on their
- order page. Leave empty to hide it.
- </CardDescription>
- </CardHeader>
- <CardContent 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"
- />
- <InputError
- message={errors.whatsapp_number}
- />
- </CardContent>
- </Card>
-
- <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>
-
- <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>
-
- <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>
-
- <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: '#' },
- ],
- };
|