| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { router } from '@inertiajs/react';
- import { Link2 } from 'lucide-react';
- import { useState } from 'react';
- import FeaturedProductController from '@/actions/App/Http/Controllers/FeaturedProductController';
- import ProductController from '@/actions/App/Http/Controllers/ProductController';
- import { formatRinggit } from '@/lib/money';
- import type { FeaturedSection, ProductOption, Store } from '@/types';
-
- const fieldClass =
- '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 disabled:opacity-50 md:text-sm';
-
- /**
- * Lets the merchant choose which real product each "featured product" section shows,
- * so its name, price, photo and link always match the product list.
- */
- export default function FeaturedPicker({
- store,
- sections,
- products,
- busy,
- }: {
- store: Store;
- sections: FeaturedSection[];
- products: ProductOption[];
- busy: boolean;
- }) {
- const [saving, setSaving] = useState<string | null>(null);
-
- if (sections.length === 0) {
- return null;
- }
-
- function choose(section: FeaturedSection, slug: string) {
- router.put(
- FeaturedProductController.update.url({
- slug: store.slug,
- sectionId: section.section_id,
- }),
- { product_slug: slug || null },
- {
- preserveScroll: true,
- onStart: () => setSaving(section.section_id),
- onFinish: () => setSaving(null),
- },
- );
- }
-
- return (
- <div
- className="space-y-4 rounded-xl border p-4"
- data-test="featured-picker"
- >
- <div className="flex items-start gap-3">
- <Link2 className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">Featured product</h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- Choose which of your products this section shows. Its
- real name, price and photo are used, and the button
- opens its page.
- </p>
- </div>
- </div>
-
- {products.length === 0 ? (
- <p className="text-sm text-muted-foreground">
- You have no active products yet.{' '}
- <a
- href={ProductController.create.url(store.slug)}
- className="underline underline-offset-2"
- >
- Add a product
- </a>{' '}
- to show it here. Until then the section shows example text.
- </p>
- ) : (
- <ul className="space-y-3">
- {sections.map((section) => {
- const id = `featured-${section.section_id}`;
- const missing =
- section.product_slug !== null &&
- !products.some(
- (product) =>
- product.slug === section.product_slug,
- );
-
- return (
- <li
- key={section.section_id}
- className="grid gap-1.5"
- >
- <label htmlFor={id} className="text-sm">
- “{section.name}”
- {section.page_id !== 'home' && (
- <span className="text-muted-foreground">
- {' '}
- on {section.page_title}
- </span>
- )}
- </label>
- <select
- id={id}
- value={
- missing
- ? ''
- : (section.product_slug ?? '')
- }
- disabled={
- busy || saving === section.section_id
- }
- onChange={(event) =>
- choose(section, event.target.value)
- }
- className={fieldClass}
- >
- <option value="">
- No product (hide the price)
- </option>
- {products.map((product) => (
- <option
- key={product.slug}
- value={product.slug}
- >
- {product.name} ·{' '}
- {formatRinggit(product.price)}
- </option>
- ))}
- </select>
- {missing && (
- <p className="text-xs text-amber-700 dark:text-amber-400">
- The product this section showed is no
- longer active. Choose another one.
- </p>
- )}
- </li>
- );
- })}
- </ul>
- )}
- </div>
- );
- }
|