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
142 lines
6.5 KiB
TypeScript
142 lines
6.5 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import type { SortingState } from "@tanstack/react-table";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { SortableHead, sortRows } from "@/app/components/ui/sortable-head";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { DateTimePicker } from "@/app/components/ui/date-time-picker";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
|
|
import { listRateCards, saveRateCard, listZones, type RateCard } from "@/app/lib/services/adminShipping";
|
|
import { RATE_COLUMNS, templateRows } from "@/app/lib/shipping/rateCardWorkbook";
|
|
import { downloadXlsx, toXlsx } from "@/app/lib/xlsx";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { RateCardEditor } from "./RateCardEditor";
|
|
|
|
type CardForm = { name: string; effectiveFrom: string; freeShippingThreshold: string; warehouseId: string };
|
|
const EMPTY: CardForm = { name: "", effectiveFrom: "", freeShippingThreshold: "", warehouseId: "" };
|
|
|
|
// Rate cards tab (add-shipping-rates): cards are created as drafts; the editor holds the rates and the activation.
|
|
export function RateCardsTab() {
|
|
const t = useT();
|
|
const [cards, setCards] = useState<RateCard[]>([]);
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
const [open, setOpen] = useState<number | null>(null);
|
|
const [form, setForm] = useState<CardForm | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const load = useCallback(() => {
|
|
listRateCards().then(setCards).catch((e: unknown) => setError(e instanceof Error ? e.message : "ShipRateLoadFailed"));
|
|
}, []);
|
|
useEffect(load, [load]);
|
|
|
|
const sorted = useMemo(
|
|
() =>
|
|
sortRows(cards, sorting, {
|
|
name: (c) => c.name,
|
|
from: (c) => c.effectiveFrom,
|
|
status: (c) => c.status,
|
|
warehouse: (c) => c.warehouseId ?? t("CatAllWarehouses"),
|
|
free: (c) => c.freeShippingThreshold,
|
|
}),
|
|
[cards, sorting, t],
|
|
);
|
|
|
|
const create = async () => {
|
|
if (!form) return;
|
|
setError(null);
|
|
try {
|
|
const saved = await saveRateCard({
|
|
name: form.name,
|
|
effectiveFrom: form.effectiveFrom.length === 16 ? `${form.effectiveFrom}:00` : form.effectiveFrom,
|
|
freeShippingThreshold: form.freeShippingThreshold ? Number(form.freeShippingThreshold) : null,
|
|
warehouseId: form.warehouseId ? Number(form.warehouseId) : null,
|
|
});
|
|
setForm(null);
|
|
load();
|
|
if (saved.id) setOpen(saved.id);
|
|
} catch (e) {
|
|
setError(e instanceof Error ? e.message : "ShipRateCreateFailed");
|
|
}
|
|
};
|
|
|
|
if (open !== null) {
|
|
return <RateCardEditor cardId={open} onBack={() => { setOpen(null); load(); }} />;
|
|
}
|
|
|
|
const text = (key: keyof CardForm, label: string, type = "text") => (
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`card-${key}`}>{label}</Label>
|
|
<Input id={`card-${key}`} type={type} value={form?.[key] ?? ""} onChange={(e) => form && setForm({ ...form, [key]: e.target.value })} />
|
|
</div>
|
|
);
|
|
|
|
return (
|
|
<div className="space-y-3">
|
|
<p className="text-sm text-muted-foreground">{t("ShipRateTemplateHelp")}</p>
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="outline" onClick={async () => {
|
|
try {
|
|
const zones = await listZones();
|
|
const codes = zones.filter((z) => z.status === "Y").map((z) => z.code);
|
|
downloadXlsx("shipping-rates-template.xlsx", await toXlsx(templateRows(codes), RATE_COLUMNS, "Rates"));
|
|
} catch {
|
|
setError("ShipRateTemplateFailed");
|
|
}
|
|
}}>{t("CatDownloadTemplate")}</Button>
|
|
<Button onClick={() => { setError(null); setForm({ ...EMPTY }); }}>{t("ShipRateNewCard")}</Button>
|
|
</div>
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<SortableHead label={t("CatName")} sortKey="name" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("CatEffectiveFrom")} sortKey="from" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("Status")} sortKey="status" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("Warehouse")} sortKey="warehouse" sorting={sorting} onSortingChange={setSorting} />
|
|
<SortableHead label={t("ShipRateFreeOver")} sortKey="free" sorting={sorting} onSortingChange={setSorting} />
|
|
<TableHead />
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{sorted.map((c) => (
|
|
<TableRow key={c.id}>
|
|
<TableCell>{c.name}</TableCell>
|
|
<TableCell>{c.effectiveFrom.replace("T", " ").slice(0, 16)}</TableCell>
|
|
<TableCell>{c.status}</TableCell>
|
|
<TableCell>{c.warehouseId ?? t("CatAllWarehouses")}</TableCell>
|
|
<TableCell>{c.freeShippingThreshold ?? "—"}</TableCell>
|
|
<TableCell className="text-right">
|
|
<Button variant="outline" size="sm" onClick={() => c.id && setOpen(c.id)}>{t("ShipRateOpen")}</Button>
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
{!form && error && <p role="alert" className="text-sm text-destructive">{t(error)}</p>}
|
|
|
|
<Dialog open={form !== null} onOpenChange={(o) => { if (!o) setForm(null); }}>
|
|
<DialogContent>
|
|
<DialogHeader><DialogTitle>{t("ShipRateNewTitle")}</DialogTitle></DialogHeader>
|
|
<div className="space-y-3">
|
|
{text("name", t("CatName"))}
|
|
<div className="space-y-1">
|
|
<Label htmlFor="card-effectiveFrom">{t("EffectiveFrom")}</Label>
|
|
<DateTimePicker id="card-effectiveFrom" timeLabel={t("EffectiveFromTime")} value={form?.effectiveFrom ?? ""}
|
|
onChange={(v) => form && setForm({ ...form, effectiveFrom: v })} />
|
|
</div>
|
|
{text("freeShippingThreshold", t("ShipRateFreeShippingOver"), "number")}
|
|
{text("warehouseId", t("ShipRateWarehouseId"), "number")}
|
|
{error && <p role="alert" className="text-sm text-destructive">{t(error)}</p>}
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => setForm(null)}>{t("Cancel")}</Button>
|
|
<Button onClick={create}>{t("CatCreate")}</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|