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
125 lines
5.4 KiB
TypeScript
125 lines
5.4 KiB
TypeScript
"use client";
|
|
|
|
import { useId, useState } from "react";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Checkbox } from "@/app/components/ui/checkbox";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import type { ProductListRow } from "@/app/lib/types";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
/** The campaign's eligible products, chosen by category; mounted only while open, so Cancel just drops the draft. */
|
|
export function ProductSelectDialog({
|
|
products,
|
|
selected,
|
|
onDone,
|
|
onClose,
|
|
}: {
|
|
products: ProductListRow[];
|
|
selected: string[];
|
|
onDone: (codes: string[]) => void;
|
|
onClose: () => void;
|
|
}) {
|
|
const t = useT();
|
|
const id = useId();
|
|
const [draft, setDraft] = useState(() => new Set(selected.map((c) => c.toUpperCase())));
|
|
const [query, setQuery] = useState("");
|
|
|
|
const q = query.trim().toLowerCase();
|
|
const visible = q
|
|
? products.filter((p) => p.productName.toLowerCase().includes(q) || p.productCode.toLowerCase().includes(q))
|
|
: products;
|
|
const groups = new Map<string, ProductListRow[]>();
|
|
for (const p of visible) {
|
|
const category = p.productCategory?.trim() || t("PromoUncategorised");
|
|
groups.set(category, [...(groups.get(category) ?? []), p]);
|
|
}
|
|
|
|
function toggle(codes: string[], on: boolean) {
|
|
setDraft((prev) => {
|
|
const next = new Set(prev);
|
|
for (const c of codes) {
|
|
if (on) next.add(c.toUpperCase());
|
|
else next.delete(c.toUpperCase());
|
|
}
|
|
return next;
|
|
});
|
|
}
|
|
|
|
function done() {
|
|
// Catalogue order first, then any chosen code the catalogue no longer lists, so nothing is lost.
|
|
const known = products.filter((p) => draft.has(p.productCode.toUpperCase())).map((p) => p.productCode);
|
|
const knownUpper = new Set(known.map((c) => c.toUpperCase()));
|
|
onDone([...known, ...selected.filter((c) => draft.has(c.toUpperCase()) && !knownUpper.has(c.toUpperCase()))]);
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
<DialogContent aria-describedby={undefined} className="flex max-h-[90vh] flex-col gap-0 p-0 sm:max-w-3xl">
|
|
<DialogHeader className="border-b p-4">
|
|
<DialogTitle>{t("PromoSelectEligibleProducts")}</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="border-b p-4">
|
|
<Input
|
|
type="search"
|
|
aria-label={t("PromoSearchNameOrCode")}
|
|
placeholder={t("PromoSearchNameOrCode")}
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="min-h-0 flex-1 overflow-y-auto">
|
|
{groups.size === 0 && <p className="p-4 text-sm text-muted-foreground">{t("PromoNoProductsMatch")}</p>}
|
|
{[...groups].map(([category, rows], gi) => {
|
|
const chosen = rows.filter((p) => draft.has(p.productCode.toUpperCase())).length;
|
|
const headId = `${id}-g${gi}`;
|
|
return (
|
|
<div key={category} role="group" aria-labelledby={headId}>
|
|
<div className="flex items-center gap-3 border-y bg-muted/40 px-4 py-2">
|
|
<Checkbox
|
|
aria-label={t("PromoSelectAllCategory", { category })}
|
|
checked={chosen === rows.length ? true : chosen > 0 ? "indeterminate" : false}
|
|
onCheckedChange={() => toggle(rows.map((p) => p.productCode), chosen < rows.length)}
|
|
/>
|
|
<span id={headId} className="text-sm font-semibold">{category}</span>
|
|
<span className="text-xs text-muted-foreground">{t("PromoCategoryCount", { selected: chosen, total: rows.length })}</span>
|
|
</div>
|
|
<div className="grid gap-x-6 gap-y-3 p-4 sm:grid-cols-2">
|
|
{rows.map((p, pi) => {
|
|
const nameId = `${headId}-p${pi}`;
|
|
return (
|
|
<label key={p.productCode} className="flex cursor-pointer items-start gap-3">
|
|
<Checkbox
|
|
className="mt-0.5"
|
|
aria-labelledby={nameId}
|
|
checked={draft.has(p.productCode.toUpperCase())}
|
|
onCheckedChange={(v) => toggle([p.productCode], v === true)}
|
|
/>
|
|
<span className="min-w-0">
|
|
<span id={nameId} className="block text-sm">{p.productName}</span>
|
|
<span className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
{p.productCode}
|
|
{p.productStatus !== "Y" && <Badge variant="neutral">{t("CatInactive")}</Badge>}
|
|
</span>
|
|
</span>
|
|
</label>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
<DialogFooter className="items-center border-t p-4 sm:justify-between">
|
|
<p className="text-sm">{t("PromoNSelected", { count: draft.size })}</p>
|
|
<div className="flex gap-2">
|
|
<Button type="button" variant="outline" onClick={onClose}>{t("Cancel")}</Button>
|
|
<Button type="button" onClick={done}>{t("PromoPickerDone")}</Button>
|
|
</div>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|