Files
co-lite/solvway-admin-portal/app/components/warehouses/WarehouseFormDialog.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

256 lines
9.7 KiB
TypeScript

"use client";
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogFooter,
} from "@/app/components/ui/dialog";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/app/components/ui/select";
import { Input } from "@/app/components/ui/input";
import { Textarea } from "@/app/components/ui/textarea";
import { Label } from "@/app/components/ui/label";
import { STATUS_OPTIONS, TYPE_OPTIONS, FULFILMENT_MODE_OPTIONS } from "./warehouseLabels";
import { Button } from "@/app/components/ui/button";
import { createAdminWarehouse, updateAdminWarehouse } from "@/app/lib/services/adminWarehouses";
import type { AdminWarehouse } from "@/app/lib/types";
import { useT } from "@/app/lib/i18n/MessagesProvider";
// "— None —" sentinel: Radix Select disallows an empty-string item value, mirroring
// WarehouseTab.tsx's NONE sentinel for the parent picker.
const NONE = "none";
/**
* Create/edit dialog for a warehouse/store row (service-inventory `warehouse` table).
* `code` is only writable on create — it is immutable once a warehouse exists, so the
* edit-mode form renders it disabled. Mirrors AddLocationDialog's form/error/success shape.
*/
export function WarehouseFormDialog({
mode,
warehouse,
parentOptions,
onSuccess,
}: {
mode: "create" | "edit";
/** Required for mode="edit"; ignored for mode="create". */
warehouse?: AdminWarehouse;
/** Existing warehouses offered as the parent picker (label "code — name"). */
parentOptions: AdminWarehouse[];
onSuccess: () => void;
}) {
const [open, setOpen] = useState(false);
const t = useT();
const [code, setCode] = useState(warehouse?.code ?? "");
const [name, setName] = useState(warehouse?.name ?? "");
const [type, setType] = useState(warehouse?.type ?? "");
const [status, setStatus] = useState(warehouse?.status ?? "Y");
const [parentId, setParentId] = useState<string>(
warehouse?.parentId != null ? String(warehouse.parentId) : NONE,
);
const [address, setAddress] = useState(warehouse?.address ?? "");
const [fulfilmentMode, setFulfilmentMode] = useState<"QIANYI" | "MANUAL">(warehouse?.fulfilmentMode ?? "MANUAL");
const [onlinePriority, setOnlinePriority] = useState<string>(String(warehouse?.onlinePriority ?? 100));
const [qyWarehouseCode, setQyWarehouseCode] = useState(warehouse?.qyWarehouseCode ?? "");
const [error, setError] = useState<string | null>(null);
const [pending, setPending] = useState(false);
// Reseed every field from `warehouse` on each open, so a reused instance never shows the previously
// selected warehouse or abandoned edits (create mode reseeds to empty).
function handleOpenChange(next: boolean) {
if (next) {
setCode(warehouse?.code ?? "");
setName(warehouse?.name ?? "");
setType(warehouse?.type ?? "");
setStatus(warehouse?.status ?? "Y");
setParentId(warehouse?.parentId != null ? String(warehouse.parentId) : NONE);
setAddress(warehouse?.address ?? "");
setFulfilmentMode(warehouse?.fulfilmentMode ?? "MANUAL");
setOnlinePriority(String(warehouse?.onlinePriority ?? 100));
setQyWarehouseCode(warehouse?.qyWarehouseCode ?? "");
setError(null);
}
setOpen(next);
}
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!code.trim() || !name.trim() || !type.trim()) {
setError("CatCodeNameTypeRequired");
return;
}
const priority = Number(onlinePriority);
if (type === "ONLINE" && (onlinePriority.trim() === "" || !Number.isInteger(priority) || priority < 1)) {
setError("WhErrPriority");
return;
}
setError(null);
setPending(true);
try {
const fulfilment =
type === "ONLINE"
? { fulfilmentMode, onlinePriority: priority, ...(fulfilmentMode === "QIANYI" ? { qyWarehouseCode: qyWarehouseCode.trim() } : {}) }
: {};
const body = {
name,
type,
status,
parentId: parentId === NONE ? null : Number(parentId),
address: address.trim() ? address : null,
...fulfilment,
};
if (mode === "create") await createAdminWarehouse({ code, ...body });
else await updateAdminWarehouse((warehouse as AdminWarehouse).id, body);
setOpen(false);
onSuccess();
} catch (err) {
setError(err instanceof Error ? err.message : "WhSaveFailed");
} finally {
setPending(false);
}
}
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogTrigger asChild>
<Button type="button">{mode === "create" ? t("WhAdd") : t("Edit")}</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{mode === "create" ? t("WhAdd") : t("WhEditTitle", { name: warehouse?.name ?? "" })}</DialogTitle>
</DialogHeader>
<form onSubmit={handleSubmit} noValidate className="space-y-3">
<div className="space-y-1">
<Label htmlFor="wh-code">{t("Code")}</Label>
<Input
id="wh-code"
value={code}
onChange={(e) => setCode(e.target.value)}
disabled={mode === "edit"}
/>
</div>
<div className="space-y-1">
<Label htmlFor="wh-name">{t("CatName")}</Label>
<Input id="wh-name" value={name} onChange={(e) => setName(e.target.value)} />
</div>
<div className="space-y-1">
<Label htmlFor="wh-type">{t("Type")}</Label>
<Select value={type} onValueChange={setType}>
<SelectTrigger id="wh-type" className="w-full">
<SelectValue placeholder={t("CatSelectType")} />
</SelectTrigger>
<SelectContent>
{TYPE_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{t(opt.label)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="wh-status">{t("Status")}</Label>
<Select value={status} onValueChange={setStatus}>
<SelectTrigger id="wh-status" className="w-full">
<SelectValue placeholder={t("WhSelectStatus")} />
</SelectTrigger>
<SelectContent>
{STATUS_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{t(opt.label)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="wh-parent">{t("WhParent")}</Label>
<Select value={parentId} onValueChange={setParentId}>
<SelectTrigger id="wh-parent" className="w-full">
<SelectValue placeholder={t("WhSelectParent")} />
</SelectTrigger>
<SelectContent>
<SelectItem value={NONE}>{t("CatNoneOption")}</SelectItem>
{parentOptions
.filter((p) => !warehouse || p.id !== warehouse.id)
.map((p) => (
<SelectItem key={p.id} value={String(p.id)}>
{p.code} — {p.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="wh-address">{t("Address")}</Label>
<Textarea
id="wh-address"
value={address}
onChange={(e) => setAddress(e.target.value)}
maxLength={255}
/>
</div>
{type === "ONLINE" && (
<>
<div className="space-y-1">
<Label htmlFor="wh-fulfilment-mode">{t("WhFulfilmentMode")}</Label>
<Select value={fulfilmentMode} onValueChange={(v) => setFulfilmentMode(v as "QIANYI" | "MANUAL")}>
<SelectTrigger id="wh-fulfilment-mode" className="w-full">
<SelectValue placeholder={t("WhSelectFulfilmentMode")} />
</SelectTrigger>
<SelectContent>
{FULFILMENT_MODE_OPTIONS.map((opt) => (
<SelectItem key={opt.value} value={opt.value}>
{t(opt.label)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label htmlFor="wh-online-priority">{t("WhRoutingPriority")}</Label>
<Input
id="wh-online-priority"
type="number"
min={1}
step={1}
value={onlinePriority}
onChange={(e) => setOnlinePriority(e.target.value)}
/>
</div>
{fulfilmentMode === "QIANYI" && (
<div className="space-y-1">
<Label htmlFor="wh-qy-warehouse">{t("WhQianyiWarehouseName")}</Label>
<Input
id="wh-qy-warehouse"
value={qyWarehouseCode}
onChange={(e) => setQyWarehouseCode(e.target.value)}
/>
</div>
)}
</>
)}
{error && (
<p role="alert" className="text-sm text-destructive">
{error ? t(error) : null}
</p>
)}
<DialogFooter>
<Button type="submit" disabled={pending}>
{mode === "create" ? t("CatCreate") : t("Save")}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}