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
503 lines
17 KiB
TypeScript
503 lines
17 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState, type FormEvent } from "react";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/app/components/ui/select";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/app/components/ui/dialog";
|
|
import { DateTimePicker } from "@/app/components/ui/date-time-picker";
|
|
import { ErrorState } from "@/app/components/ui/states";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { LINK_KIND_OPTIONS, ZONE_OPTIONS, saveBanner, uploadBannerImage } from "@/app/lib/services/banners";
|
|
import type { AdminBanner, BannerLinkKind, BannerStatus, BannerZone } from "@/app/lib/types";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import type { MessageParams } from "@/app/lib/i18n/t";
|
|
|
|
// The `/banners` create/edit dialog (spec "add-homepage-banners", task 13). Mirrors
|
|
// app/bulletins/page.tsx's BulletinFormDialog: upload-on-submit images so an abandoned form
|
|
// uploads nothing (both banner image uploads are SAME-REQUEST/file_autocommit=Y — see
|
|
// app/api/banners/image/route.ts — so the returned URL can be persisted immediately).
|
|
//
|
|
// No product/category picker component exists anywhere in the admin portal (checked
|
|
// app/components/ui, app/components/products, and grepped for Picker/Combobox — the only
|
|
// typeahead is MemberSearchCombobox, which is member-specific). PRODUCT/CATEGORY link
|
|
// references are therefore plain validated id/slug text fields, not a typeahead.
|
|
|
|
interface FormState {
|
|
id?: number;
|
|
zone: BannerZone;
|
|
desktopImageUrl: string;
|
|
mobileImageUrl: string;
|
|
altText: string;
|
|
headline: string;
|
|
subHeadline: string;
|
|
ctaLabel: string;
|
|
linkKind: BannerLinkKind;
|
|
linkReference: string;
|
|
displayOrder: string;
|
|
dateFrom: string;
|
|
dateTo: string;
|
|
status: BannerStatus;
|
|
}
|
|
|
|
function emptyForm(zone: BannerZone): FormState {
|
|
return {
|
|
zone,
|
|
desktopImageUrl: "",
|
|
mobileImageUrl: "",
|
|
altText: "",
|
|
headline: "",
|
|
subHeadline: "",
|
|
ctaLabel: "",
|
|
linkKind: "NONE",
|
|
linkReference: "",
|
|
displayOrder: "",
|
|
dateFrom: "",
|
|
dateTo: "",
|
|
status: "Y",
|
|
};
|
|
}
|
|
|
|
function toFormState(banner: AdminBanner): FormState {
|
|
return {
|
|
id: banner.id,
|
|
zone: banner.zone,
|
|
desktopImageUrl: banner.desktopImageUrl,
|
|
mobileImageUrl: banner.mobileImageUrl ?? "",
|
|
altText: banner.altText,
|
|
headline: banner.headline ?? "",
|
|
subHeadline: banner.subHeadline ?? "",
|
|
ctaLabel: banner.ctaLabel ?? "",
|
|
linkKind: banner.linkKind ?? "NONE",
|
|
linkReference: banner.linkReference ?? "",
|
|
displayOrder: banner.displayOrder != null ? String(banner.displayOrder) : "",
|
|
dateFrom: banner.dateFrom ?? "",
|
|
dateTo: banner.dateTo ?? "",
|
|
status: banner.status,
|
|
};
|
|
}
|
|
|
|
/** Reads a File as base64 with the `data:...;base64,` prefix stripped. */
|
|
function fileToBase64(file: File): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onload = () => {
|
|
const dataUrl = String(reader.result ?? "");
|
|
const comma = dataUrl.indexOf(",");
|
|
resolve(comma >= 0 ? dataUrl.slice(comma + 1) : dataUrl);
|
|
};
|
|
reader.onerror = () => reject(new Error("UiImageReadFailed"));
|
|
reader.readAsDataURL(file);
|
|
});
|
|
}
|
|
|
|
function toPayload(form: FormState, desktopImageUrl: string, mobileImageUrl: string): AdminBanner {
|
|
return {
|
|
id: form.id,
|
|
zone: form.zone,
|
|
desktopImageUrl,
|
|
mobileImageUrl: mobileImageUrl || undefined,
|
|
altText: form.altText.trim(),
|
|
headline: form.headline.trim() || undefined,
|
|
subHeadline: form.subHeadline.trim() || undefined,
|
|
ctaLabel: form.ctaLabel.trim() || undefined,
|
|
// Always sent explicitly (default "NONE") -- never omitted -- and the reference is dropped
|
|
// whenever the kind is NONE, matching the route handler's validation.
|
|
linkKind: form.linkKind,
|
|
linkReference: form.linkKind === "NONE" ? undefined : form.linkReference.trim(),
|
|
displayOrder: form.displayOrder === "" ? undefined : Number(form.displayOrder),
|
|
dateFrom: form.dateFrom || undefined,
|
|
dateTo: form.dateTo || undefined,
|
|
status: form.status,
|
|
};
|
|
}
|
|
|
|
/** Mirrors app/api/banners/route.ts's POST/PUT validation, plus the displayOrder >= 0 rule. */
|
|
function validate(form: FormState, hasDesktopImage: boolean): string | null {
|
|
if (!hasDesktopImage) return "BnrDesktopImageRequired";
|
|
if (!form.altText.trim()) return "BnrAltTextRequired";
|
|
if (form.linkKind !== "NONE" && !form.linkReference.trim()) {
|
|
return "BnrLinkReferenceRequired";
|
|
}
|
|
if (form.dateFrom && form.dateTo && new Date(form.dateFrom) >= new Date(form.dateTo)) {
|
|
return "UiDateOrder";
|
|
}
|
|
if (form.displayOrder !== "" && Number(form.displayOrder) < 0) {
|
|
return "BnrDisplayOrderMin";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** Placeholder text for the link-reference field, per link kind. */
|
|
function referencePlaceholder(linkKind: BannerLinkKind): string {
|
|
switch (linkKind) {
|
|
case "PRODUCT":
|
|
return "BnrRefPlaceholderProduct";
|
|
case "CATEGORY":
|
|
return "BnrRefPlaceholderCategory";
|
|
case "URL":
|
|
return "BnrRefPlaceholderUrl";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export function BannerFormDialog({
|
|
mode,
|
|
banner,
|
|
defaultZone,
|
|
onClose,
|
|
onSaved,
|
|
}: {
|
|
mode: "add" | "edit";
|
|
/** Required in edit mode; ignored (defaultZone is used instead) in add mode. */
|
|
banner?: AdminBanner;
|
|
/** The zone a new banner starts with -- normally the page's current zone filter. */
|
|
defaultZone: BannerZone;
|
|
onClose: () => void;
|
|
onSaved: () => void;
|
|
}) {
|
|
const t = useT();
|
|
const [form, setForm] = useState<FormState>(() =>
|
|
mode === "edit" && banner ? toFormState(banner) : emptyForm(defaultZone),
|
|
);
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState<{ code: string; params?: MessageParams } | null>(null);
|
|
|
|
// Upload-on-submit (mirrors BulletinFormDialog): a picked file is held locally and shown as a
|
|
// blob: preview; nothing is uploaded until Save runs, so an abandoned form uploads nothing.
|
|
const [desktopFile, setDesktopFile] = useState<File | null>(null);
|
|
const [desktopPreviewUrl, setDesktopPreviewUrl] = useState<string | null>(null);
|
|
const [mobileFile, setMobileFile] = useState<File | null>(null);
|
|
const [mobilePreviewUrl, setMobilePreviewUrl] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (desktopPreviewUrl && typeof URL.revokeObjectURL === "function") {
|
|
URL.revokeObjectURL(desktopPreviewUrl);
|
|
}
|
|
if (mobilePreviewUrl && typeof URL.revokeObjectURL === "function") {
|
|
URL.revokeObjectURL(mobilePreviewUrl);
|
|
}
|
|
};
|
|
}, [desktopPreviewUrl, mobilePreviewUrl]);
|
|
|
|
function pickDesktopImage(file: File | undefined | null) {
|
|
if (!file) return;
|
|
setDesktopPreviewUrl(URL.createObjectURL(file));
|
|
setDesktopFile(file);
|
|
}
|
|
|
|
function removeDesktopImage() {
|
|
setDesktopFile(null);
|
|
setDesktopPreviewUrl(null);
|
|
setForm((f) => ({ ...f, desktopImageUrl: "" }));
|
|
}
|
|
|
|
function pickMobileImage(file: File | undefined | null) {
|
|
if (!file) return;
|
|
setMobilePreviewUrl(URL.createObjectURL(file));
|
|
setMobileFile(file);
|
|
}
|
|
|
|
function removeMobileImage() {
|
|
setMobileFile(null);
|
|
setMobilePreviewUrl(null);
|
|
setForm((f) => ({ ...f, mobileImageUrl: "" }));
|
|
}
|
|
|
|
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
setError(null);
|
|
|
|
const hasDesktopImage = Boolean(desktopFile) || Boolean(form.desktopImageUrl);
|
|
const validationError = validate(form, hasDesktopImage);
|
|
if (validationError) {
|
|
setError({ code: validationError });
|
|
return;
|
|
}
|
|
|
|
setSaving(true);
|
|
try {
|
|
let desktopImageUrl = form.desktopImageUrl;
|
|
if (desktopFile) {
|
|
const base = await fileToBase64(desktopFile);
|
|
try {
|
|
const res = await uploadBannerImage({
|
|
fileName: desktopFile.name,
|
|
fileType: desktopFile.type,
|
|
base,
|
|
zone: form.zone,
|
|
});
|
|
desktopImageUrl = res.url;
|
|
} catch (err) {
|
|
// Upload failure BLOCKS the save: a banner must never be persisted referencing an
|
|
// image that failed to upload. The finally below resets saving.
|
|
setError(
|
|
err instanceof ApiError
|
|
? { code: "BnrDesktopUploadFailedWith", params: { message: t(err.message) } }
|
|
: { code: "BnrDesktopUploadFailed" },
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
let mobileImageUrl = form.mobileImageUrl;
|
|
if (mobileFile) {
|
|
const base = await fileToBase64(mobileFile);
|
|
try {
|
|
const res = await uploadBannerImage({
|
|
fileName: mobileFile.name,
|
|
fileType: mobileFile.type,
|
|
base,
|
|
zone: form.zone,
|
|
});
|
|
mobileImageUrl = res.url;
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof ApiError
|
|
? { code: "BnrMobileUploadFailedWith", params: { message: t(err.message) } }
|
|
: { code: "BnrMobileUploadFailed" },
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
await saveBanner(toPayload(form, desktopImageUrl, mobileImageUrl));
|
|
onSaved();
|
|
onClose();
|
|
} catch (err) {
|
|
setError({ code: err instanceof Error ? err.message : "BnrSaveFailed" });
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
<DialogContent className="max-h-[90vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<DialogTitle>{t(mode === "add" ? "BnrNewBanner" : "BnrEditBanner")}</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
{error && <ErrorState message={t(error.code, error.params)} />}
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4" noValidate>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-zone">{t("BnrZone")}</Label>
|
|
<Select value={form.zone} onValueChange={(v) => setForm((f) => ({ ...f, zone: v as BannerZone }))}>
|
|
<SelectTrigger id="banner-zone" disabled={saving}>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{ZONE_OPTIONS.map((opt) => (
|
|
<SelectItem key={opt.value} value={opt.value}>
|
|
{t(opt.label)}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
{form.zone === "CAMPAIGN" && (
|
|
<p className="text-sm text-muted-foreground">
|
|
{t("BnrCampaignNotShown")}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-desktop-image">{t("BnrDesktopImage")}</Label>
|
|
<div className="space-y-2">
|
|
{desktopPreviewUrl ?? form.desktopImageUrl ? (
|
|
<img
|
|
src={desktopPreviewUrl ?? form.desktopImageUrl}
|
|
alt={t("BnrDesktopImagePreview")}
|
|
className="max-h-40 w-full rounded border object-contain"
|
|
/>
|
|
) : (
|
|
<p className="text-sm text-muted-foreground">{t("CatNoImageSelected")}</p>
|
|
)}
|
|
<Input
|
|
id="banner-desktop-image"
|
|
type="file"
|
|
accept="image/*"
|
|
onChange={(e) => {
|
|
pickDesktopImage(e.target.files?.[0]);
|
|
e.target.value = ""; // allow re-picking the same file
|
|
}}
|
|
disabled={saving}
|
|
/>
|
|
{(desktopPreviewUrl ?? form.desktopImageUrl) && (
|
|
<Button type="button" variant="outline" size="sm" onClick={removeDesktopImage} disabled={saving}>
|
|
{t("CatRemoveImage")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-mobile-image">{t("BnrMobileImage")}</Label>
|
|
<div className="space-y-2">
|
|
{mobilePreviewUrl ?? form.mobileImageUrl ? (
|
|
<img
|
|
src={mobilePreviewUrl ?? form.mobileImageUrl}
|
|
alt={t("BnrMobileImagePreview")}
|
|
className="max-h-40 w-full rounded border object-contain"
|
|
/>
|
|
) : (
|
|
<p className="text-sm text-muted-foreground">{t("CatNoImageSelected")}</p>
|
|
)}
|
|
<Input
|
|
id="banner-mobile-image"
|
|
type="file"
|
|
accept="image/*"
|
|
onChange={(e) => {
|
|
pickMobileImage(e.target.files?.[0]);
|
|
e.target.value = "";
|
|
}}
|
|
disabled={saving}
|
|
/>
|
|
{(mobilePreviewUrl ?? form.mobileImageUrl) && (
|
|
<Button type="button" variant="outline" size="sm" onClick={removeMobileImage} disabled={saving}>
|
|
{t("CatRemoveImage")}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-alt-text">{t("BnrAltText")}</Label>
|
|
<Input
|
|
id="banner-alt-text"
|
|
required
|
|
value={form.altText}
|
|
onChange={(e) => setForm((f) => ({ ...f, altText: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-headline">{t("BnrHeadline")}</Label>
|
|
<Input
|
|
id="banner-headline"
|
|
value={form.headline}
|
|
onChange={(e) => setForm((f) => ({ ...f, headline: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-sub-headline">{t("BnrSubHeadline")}</Label>
|
|
<Input
|
|
id="banner-sub-headline"
|
|
value={form.subHeadline}
|
|
onChange={(e) => setForm((f) => ({ ...f, subHeadline: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-cta-label">{t("BnrCtaLabel")}</Label>
|
|
<Input
|
|
id="banner-cta-label"
|
|
value={form.ctaLabel}
|
|
onChange={(e) => setForm((f) => ({ ...f, ctaLabel: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-link-kind">{t("BnrLinkKind")}</Label>
|
|
<Select
|
|
value={form.linkKind}
|
|
onValueChange={(v) =>
|
|
setForm((f) => ({ ...f, linkKind: v as BannerLinkKind, linkReference: v === "NONE" ? "" : f.linkReference }))
|
|
}
|
|
>
|
|
<SelectTrigger id="banner-link-kind" disabled={saving}>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{LINK_KIND_OPTIONS.map((opt) => (
|
|
<SelectItem key={opt.value} value={opt.value}>
|
|
{t(opt.label)}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
|
|
{form.linkKind !== "NONE" && (
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-link-reference">{t("BnrLinkReference")}</Label>
|
|
<Input
|
|
id="banner-link-reference"
|
|
placeholder={t(referencePlaceholder(form.linkKind))}
|
|
value={form.linkReference}
|
|
onChange={(e) => setForm((f) => ({ ...f, linkReference: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-display-order">{t("BnrDisplayOrder")}</Label>
|
|
<Input
|
|
id="banner-display-order"
|
|
type="number"
|
|
min={0}
|
|
value={form.displayOrder}
|
|
onChange={(e) => setForm((f) => ({ ...f, displayOrder: e.target.value }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-date-from">{t("CatFrom")}</Label>
|
|
<DateTimePicker
|
|
id="banner-date-from"
|
|
timeLabel={t("UiFromTime")}
|
|
value={form.dateFrom}
|
|
onChange={(v) => setForm((f) => ({ ...f, dateFrom: v }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="banner-date-to">{t("To")}</Label>
|
|
<DateTimePicker
|
|
id="banner-date-to"
|
|
timeLabel={t("UiToTime")}
|
|
value={form.dateTo}
|
|
onChange={(v) => setForm((f) => ({ ...f, dateTo: v }))}
|
|
disabled={saving}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button type="button" variant="outline" onClick={onClose} disabled={saving}>
|
|
{t("Cancel")}
|
|
</Button>
|
|
<Button type="submit" disabled={saving}>
|
|
{t("Save")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|