Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
423 lines
17 KiB
TypeScript
423 lines
17 KiB
TypeScript
"use client";
|
|
|
|
import { type ChangeEvent } from "react";
|
|
import { Plus, X } from "lucide-react";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { Switch } from "@/app/components/ui/switch";
|
|
import { QianyiSkuPicker } from "@/app/components/products/QianyiSkuPicker";
|
|
import {
|
|
addOption,
|
|
generateVariants,
|
|
isDefaultVariant,
|
|
MAX_OPTIONS,
|
|
prefillFromQianyi,
|
|
removeOption,
|
|
setHasVariants,
|
|
suggestVariantCode,
|
|
withOptionLabels,
|
|
type FieldErrors,
|
|
type SkuDraft,
|
|
type SkuVariantDraft,
|
|
} from "@/app/lib/catalogSetup";
|
|
import { FieldError } from "./FieldError";
|
|
import { SupplierSelect } from "./SupplierSelect";
|
|
import { BarcodeList } from "./BarcodeList";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
// The SKU fields and variants grid (redesign-product-setup-stepper): used by the SKU drawer and the new SKU
|
|
// panel. The caller owns the draft and the save. A SKU without variants is one flat form; with variants it
|
|
// has up to three options whose combinations become the variant rows (add-sku-option-axes).
|
|
|
|
// Draft dimension keys carry a "Cm" unit suffix; the note names them plainly.
|
|
const DIM_CODE: Record<string, string> = { lengthCm: "SkuDimLength", widthCm: "SkuDimWidth", heightCm: "SkuDimHeight", variant: "SkuDimOptions" };
|
|
|
|
export interface SkuEditorProps {
|
|
sku: SkuDraft;
|
|
onChange: (sku: SkuDraft) => void;
|
|
/** Uploads one variant image (SAME-REQUEST autocommit) and resolves with its URL, or "" on failure. */
|
|
onUploadImage: (file: File) => Promise<string>;
|
|
errors: FieldErrors;
|
|
saving: boolean;
|
|
}
|
|
|
|
export function SkuEditor({ sku, onChange, onUploadImage, errors, saving }: SkuEditorProps) {
|
|
const t = useT();
|
|
const saved = sku.id != null;
|
|
const hasVariants = sku.optionNames.length > 0;
|
|
|
|
function patch(next: Partial<SkuDraft>) {
|
|
onChange(withSuggestedCodes(sku, withOptionLabels({ ...sku, ...next })));
|
|
}
|
|
|
|
function patchVariant(index: number, next: Partial<SkuVariantDraft>) {
|
|
patch({ variants: sku.variants.map((v, i) => (i === index ? { ...v, ...next } : v)) });
|
|
}
|
|
|
|
function setList(list: string[], index: number, value: string): string[] {
|
|
return list.map((item, i) => (i === index ? value : item));
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="grid gap-4 sm:grid-cols-4">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="sku-code">{t("SkuFieldCode")}</Label>
|
|
<Input
|
|
id="sku-code"
|
|
readOnly={saved}
|
|
disabled={saving}
|
|
value={sku.code}
|
|
onChange={(e) => patch({ code: e.target.value })}
|
|
aria-invalid={!!errors["sku.code"]}
|
|
className={saved ? "opacity-70" : undefined}
|
|
/>
|
|
<FieldError message={errors["sku.code"]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="sku-name">{t("SkuFieldName")}</Label>
|
|
<Input
|
|
id="sku-name"
|
|
disabled={saving}
|
|
value={sku.name}
|
|
onChange={(e) => patch({ name: e.target.value })}
|
|
aria-invalid={!!errors["sku.name"]}
|
|
/>
|
|
<FieldError message={errors["sku.name"]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="sku-supplier">{t("SkuSupplier")}</Label>
|
|
<SupplierSelect
|
|
value={sku.supplierId}
|
|
onChange={(supplierId) => patch({ supplierId })}
|
|
disabled={saving}
|
|
invalid={!!errors["sku.supplierId"]}
|
|
/>
|
|
<FieldError message={errors["sku.supplierId"]} />
|
|
</div>
|
|
<div className="flex items-end gap-2 pb-2">
|
|
<Switch
|
|
id="sku-active"
|
|
checked={sku.status === "Y"}
|
|
onCheckedChange={(on) => patch({ status: on ? "Y" : "N" })}
|
|
disabled={saving}
|
|
/>
|
|
<Label htmlFor="sku-active">{t("CatActive")}</Label>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-1">
|
|
<div className="flex items-center gap-2">
|
|
<Switch
|
|
id="sku-has-variants"
|
|
checked={hasVariants}
|
|
onCheckedChange={(on) => onChange(withSuggestedCodes(sku, setHasVariants(sku, on)))}
|
|
disabled={saving || (hasVariants && sku.variants.length > 1)}
|
|
/>
|
|
<Label htmlFor="sku-has-variants">{t("SkuHasVariants")}</Label>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">
|
|
{!hasVariants
|
|
? t("SkuVariantsOffHint")
|
|
: sku.variants.length > 1
|
|
? t("SkuVariantsRemoveHint")
|
|
: t("SkuVariantsOnHint")}
|
|
</p>
|
|
</div>
|
|
|
|
<FieldError message={errors["sku.variations"]} />
|
|
{!hasVariants ? (
|
|
sku.variants.slice(0, 1).map((v) => (
|
|
<div key={v.id ?? "default"} data-testid="sku-variant-row" className="grid gap-3 rounded-md border p-3 md:grid-cols-6">
|
|
<VariantFields sku={sku} v={v} i={0} errors={errors} saving={saving} onChange={onChange} patchVariant={patchVariant} onUploadImage={onUploadImage} />
|
|
{/* The one variant's code is the SKU code, so a code error from the server shows here. */}
|
|
<div className="md:col-span-6">
|
|
<FieldError message={errors["sku.variations[0].code"]} />
|
|
</div>
|
|
</div>
|
|
))
|
|
) : (
|
|
<>
|
|
<div className="space-y-3 rounded-md border p-3" data-testid="sku-options">
|
|
{sku.optionNames.map((name, a) => (
|
|
<div key={a} className="grid gap-3 md:grid-cols-[1fr_2fr_auto]">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-option-name-${a}`}>{t("SkuOptionN", { n: a + 1 })}</Label>
|
|
<Input
|
|
id={`sku-option-name-${a}`}
|
|
aria-label={t("SkuOptionNName", { n: a + 1 })}
|
|
placeholder={t(`SkuOptionNamePlaceholder${a + 1}`)}
|
|
disabled={saving}
|
|
value={name}
|
|
onChange={(e) => patch({ optionNames: setList(sku.optionNames, a, e.target.value) })}
|
|
aria-invalid={!!errors[`sku.optionNames[${a}]`]}
|
|
/>
|
|
<FieldError message={errors[`sku.optionNames[${a}]`]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-option-values-${a}`}>{t("SkuOptionValues")}</Label>
|
|
<Input
|
|
id={`sku-option-values-${a}`}
|
|
aria-label={t("SkuOptionNValues", { n: a + 1 })}
|
|
placeholder={t(`SkuOptionValuesPlaceholder${a + 1}`)}
|
|
disabled={saving}
|
|
value={sku.optionValues[a] ?? ""}
|
|
onChange={(e) => patch({ optionValues: setList(sku.optionValues, a, e.target.value) })}
|
|
/>
|
|
</div>
|
|
<div className="flex items-end pb-0.5">
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
aria-label={t("SkuRemoveOptionN", { n: a + 1 })}
|
|
onClick={() => onChange(removeOption(sku, a))}
|
|
disabled={saving || sku.optionNames.length === 1}
|
|
>
|
|
<X aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
<FieldError message={errors["sku.optionNames"]} />
|
|
<div className="flex flex-wrap gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => onChange(addOption(sku))}
|
|
disabled={saving || sku.optionNames.length >= MAX_OPTIONS}
|
|
>
|
|
<Plus aria-hidden="true" className="size-4" />
|
|
{t("SkuAddOption")}
|
|
</Button>
|
|
<Button type="button" size="sm" onClick={() => onChange(generateVariants(sku))} disabled={saving}>
|
|
{t("SkuGenerateVariants")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-3">
|
|
{sku.variants.map((v, i) => {
|
|
const path = `sku.variations[${i}]`;
|
|
const n = i + 1;
|
|
return (
|
|
<div key={v.id ?? `new-${i}`} data-testid="sku-variant-row" className="grid gap-3 rounded-md border p-3 md:grid-cols-6">
|
|
{sku.optionNames.map((name, a) => (
|
|
<div key={a} className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-option-${i}-${a}`}>{name.trim() || t("SkuOptionN", { n: a + 1 })}</Label>
|
|
<Input
|
|
id={`sku-variant-option-${i}-${a}`}
|
|
aria-label={t("SkuVariantOption", { n, name: name.trim() || t("SkuOptionN", { n: a + 1 }) })}
|
|
disabled={saving}
|
|
value={v.options[a] ?? ""}
|
|
onChange={(e) => patchVariant(i, { options: setList(sku.optionNames.map((_, j) => v.options[j] ?? ""), a, e.target.value) })}
|
|
aria-invalid={!!errors[`${path}.options`]}
|
|
/>
|
|
</div>
|
|
))}
|
|
<div className="space-y-1.5 md:col-span-2">
|
|
<Label htmlFor={`sku-variant-code-${i}`}>{t("Code")}</Label>
|
|
<Input
|
|
id={`sku-variant-code-${i}`}
|
|
aria-label={t("SkuVariantNCode", { n })}
|
|
readOnly={v.id != null}
|
|
disabled={saving}
|
|
placeholder={suggestVariantCode(sku.code, v.variant, v.variantName) || t("SkuVariantCodePlaceholder")}
|
|
value={v.code}
|
|
onChange={(e) => patchVariant(i, { code: e.target.value })}
|
|
aria-invalid={!!errors[`${path}.code`]}
|
|
className={v.id != null ? "opacity-70" : undefined}
|
|
/>
|
|
<FieldError message={errors[`${path}.code`]} />
|
|
</div>
|
|
<div className="md:col-span-6">
|
|
<FieldError message={errors[`${path}.options`]} />
|
|
</div>
|
|
<VariantFields sku={sku} v={v} i={i} errors={errors} saving={saving} onChange={onChange} patchVariant={patchVariant} onUploadImage={onUploadImage} />
|
|
<div className="flex items-end justify-between gap-2 md:col-span-2">
|
|
<p className="text-xs text-muted-foreground">{v.usedBy.length > 0 ? t("SkuUsedByNames", { names: v.usedBy.join(", ") }) : ""}</p>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
aria-label={t("SkuRemoveVariantN", { n })}
|
|
onClick={() => patch({ variants: sku.variants.filter((_, j) => j !== i) })}
|
|
disabled={saving || sku.variants.length === 1}
|
|
>
|
|
<X aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
interface VariantFieldsProps {
|
|
sku: SkuDraft;
|
|
v: SkuVariantDraft;
|
|
i: number;
|
|
errors: FieldErrors;
|
|
saving: boolean;
|
|
onChange: (sku: SkuDraft) => void;
|
|
patchVariant: (index: number, next: Partial<SkuVariantDraft>) => void;
|
|
onUploadImage: (file: File) => Promise<string>;
|
|
}
|
|
|
|
/** The stock-unit fields every variant has, also the whole form of a SKU without variants. */
|
|
function VariantFields({ sku, v, i, errors, saving, onChange, patchVariant, onUploadImage }: VariantFieldsProps) {
|
|
const t = useT();
|
|
const path = `sku.variations[${i}]`;
|
|
const n = i + 1;
|
|
|
|
async function uploadImage(event: ChangeEvent<HTMLInputElement>) {
|
|
const file = event.target.files?.[0];
|
|
event.target.value = "";
|
|
if (!file) return;
|
|
const url = await onUploadImage(file);
|
|
if (url) patchVariant(i, { image: url });
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-weight-${i}`}>{t("SkuWeightKg")}</Label>
|
|
<Input
|
|
id={`sku-variant-weight-${i}`}
|
|
aria-label={t("SkuVariantNWeight", { n })}
|
|
type="number"
|
|
step="0.001"
|
|
min="0"
|
|
disabled={saving}
|
|
value={v.weight}
|
|
onChange={(e) => patchVariant(i, { weight: e.target.value })}
|
|
aria-invalid={!!errors[`${path}.weight`]}
|
|
/>
|
|
<FieldError message={errors[`${path}.weight`]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-length-${i}`}>{t("SkuLengthCm")}</Label>
|
|
<Input
|
|
id={`sku-variant-length-${i}`}
|
|
aria-label={t("SkuVariantNLengthCm", { n })}
|
|
type="number"
|
|
step="0.01"
|
|
min="0"
|
|
disabled={saving}
|
|
value={v.lengthCm}
|
|
onChange={(e) => patchVariant(i, { lengthCm: e.target.value })}
|
|
aria-invalid={!!errors[`${path}.lengthCm`]}
|
|
/>
|
|
<FieldError message={errors[`${path}.lengthCm`]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-width-${i}`}>{t("SkuWidthCm")}</Label>
|
|
<Input
|
|
id={`sku-variant-width-${i}`}
|
|
aria-label={t("SkuVariantNWidthCm", { n })}
|
|
type="number"
|
|
step="0.01"
|
|
min="0"
|
|
disabled={saving}
|
|
value={v.widthCm}
|
|
onChange={(e) => patchVariant(i, { widthCm: e.target.value })}
|
|
aria-invalid={!!errors[`${path}.widthCm`]}
|
|
/>
|
|
<FieldError message={errors[`${path}.widthCm`]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-height-${i}`}>{t("SkuHeightCm")}</Label>
|
|
<Input
|
|
id={`sku-variant-height-${i}`}
|
|
aria-label={t("SkuVariantNHeightCm", { n })}
|
|
type="number"
|
|
step="0.01"
|
|
min="0"
|
|
disabled={saving}
|
|
value={v.heightCm}
|
|
onChange={(e) => patchVariant(i, { heightCm: e.target.value })}
|
|
aria-invalid={!!errors[`${path}.heightCm`]}
|
|
/>
|
|
<FieldError message={errors[`${path}.heightCm`]} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={`sku-variant-image-${i}`}>{t("CatImage")}</Label>
|
|
<div className="flex items-center gap-2">
|
|
{v.image ? (
|
|
// eslint-disable-next-line @next/next/no-img-element -- remote drive thumbnail
|
|
<img src={v.image} alt={t("SkuVariantN", { n })} className="size-9 rounded object-cover" />
|
|
) : null}
|
|
<Input
|
|
id={`sku-variant-image-${i}`}
|
|
aria-label={t("SkuVariantNImage", { n })}
|
|
type="file"
|
|
accept="image/png,image/jpeg,image/webp"
|
|
disabled={saving}
|
|
onChange={uploadImage}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label>{t("SkuOnHand")}</Label>
|
|
<p className="text-sm font-medium tabular" data-testid="variant-on-hand">
|
|
{v.id == null || v.onHand == null ? "—" : v.onHand}
|
|
</p>
|
|
</div>
|
|
<div className="md:col-span-4">
|
|
<QianyiSkuPicker
|
|
value={v.qySku}
|
|
title={v.qyTitle}
|
|
status={v.qyStatus}
|
|
skuVariationId={v.id}
|
|
disabled={saving}
|
|
onChange={(qySku, option) =>
|
|
qySku && option
|
|
? onChange(prefillFromQianyi(sku, i, option))
|
|
: patchVariant(i, { qySku, qyTitle: option?.title ?? null, qyStatus: qySku ? "OK" : undefined, qyTouched: true })
|
|
}
|
|
/>
|
|
{v.qyFilled && Object.keys(v.qyFilled).length > 0 ? (
|
|
<p className="mt-1 text-xs text-muted-foreground">{t("SkuFilledFromQianyi", { fields: Object.keys(v.qyFilled).map((k) => t(DIM_CODE[k] ?? k)).join(", ") })}</p>
|
|
) : null}
|
|
{[v.weight, v.lengthCm, v.widthCm, v.heightCm].some((d) => !(Number(d?.trim() || NaN) > 0)) ? (
|
|
<p className="mt-1 text-xs text-warning">{t("SkuMissingDimensions")}</p>
|
|
) : null}
|
|
{v.qyLinkSource === "AUTO" && v.qySku ? <Badge variant="outline">{t("SkuAutoLinked")}</Badge> : null}
|
|
</div>
|
|
<BarcodeList
|
|
n={n}
|
|
barcodes={v.barcodes ?? []}
|
|
path={path}
|
|
errors={errors}
|
|
disabled={saving}
|
|
onChange={(barcodes) => patchVariant(i, { barcodes, barcodesTouched: true })}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* An unsaved variant's code follows the suggestion ({SKU code}-{VALUES}) while it is still empty or
|
|
* still the suggestion for the previous values; once the admin types their own code it is left alone.
|
|
*/
|
|
function withSuggestedCodes(prev: SkuDraft, next: SkuDraft): SkuDraft {
|
|
if (prev.variants.length !== next.variants.length) return next;
|
|
const variants = next.variants.map((v, i) => {
|
|
const before = prev.variants[i];
|
|
if (v.id != null || v.code !== before.code) return v;
|
|
const auto = !before.code.trim() || before.code === suggestVariantCode(prev.code, before.variant, before.variantName);
|
|
if (!auto) return v;
|
|
// A default variant's code is the SKU code, which the backend fills in: leave it blank.
|
|
const code =
|
|
next.code.trim() && !isDefaultVariant(v.variant, v.variantName) ? suggestVariantCode(next.code, v.variant, v.variantName) : "";
|
|
return code === v.code ? v : { ...v, code };
|
|
});
|
|
return { ...next, variants };
|
|
}
|