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
312 lines
13 KiB
TypeScript
312 lines
13 KiB
TypeScript
"use client";
|
|
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import type { ReactNode } from "react";
|
|
import { ArrowDown, ArrowUp, Plus, Trash2 } from "lucide-react";
|
|
import { BLOCK_LABELS, type CmsBlock } from "@/app/lib/cmsPages";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { Textarea } from "@/app/components/ui/textarea";
|
|
import { RadioGroup, RadioGroupItem } from "@/app/components/ui/radio-group";
|
|
import { FieldError } from "@/app/components/catalog-setup/FieldError";
|
|
import { RichTextEditor } from "@/app/components/products/RichTextEditor";
|
|
|
|
// One block of the page editor (add-cms-pages): a fieldset with move/remove and the fields of its type.
|
|
|
|
/** Field paths (relative to the block) each type renders inline; any other error shows at the block's top. */
|
|
const INLINE: Record<string, string[]> = {
|
|
HERO: ["imageUrl", "heading", "text", "buttonLabel", "buttonUrl"],
|
|
RICH_TEXT: ["html"],
|
|
IMAGE_TEXT: ["imageUrl", "imagePosition", "heading", "html", "buttonLabel", "buttonUrl"],
|
|
FEATURED_PRODUCTS: ["heading", "collectionId", "limit"],
|
|
COLLECTION_LIST: ["heading", "collectionIds"],
|
|
FAQ: ["heading", "items"],
|
|
FORM: ["heading", "formId"],
|
|
};
|
|
const LISTED = /^(items\[\d+\]\.(question|answer)|collectionIds\[\d+\])$/;
|
|
|
|
const SELECT_CLASS =
|
|
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm disabled:cursor-not-allowed disabled:opacity-50";
|
|
|
|
export interface BlockFieldsProps {
|
|
block: CmsBlock;
|
|
/** Stable per-block key, used for element ids. */
|
|
id: number;
|
|
index: number;
|
|
count: number;
|
|
/** Server errors for this block, keyed by path relative to it ("buttonUrl", "items[1].answer"). */
|
|
errors: Record<string, string>;
|
|
saving: boolean;
|
|
collections: { id: number; title: string }[];
|
|
/** Every form, for the Form block's picker (add-cms-forms). */
|
|
forms: { id: number; name: string; status: "Y" | "N" }[];
|
|
/** A chosen image that uploads on save: its file name and, once read, a data-URL preview. */
|
|
pendingImage?: { name: string; preview?: string };
|
|
trustedImageHosts: string[];
|
|
onChange: (update: (block: CmsBlock) => CmsBlock) => void;
|
|
onMove: (delta: -1 | 1) => void;
|
|
onRemove: () => void;
|
|
onChooseImage: (file: File) => void;
|
|
uploadInlineImage: (file: File) => Promise<string>;
|
|
}
|
|
|
|
function Field({ id, label, error, children }: { id: string; label: string; error?: string; children: ReactNode }) {
|
|
return (
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor={id}>{label}</Label>
|
|
{children}
|
|
<FieldError message={error} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function BlockFields(props: BlockFieldsProps) {
|
|
const { block, id, index, count, errors, saving, collections, forms, pendingImage, onChange, onMove, onRemove } = props;
|
|
const t = useT();
|
|
const set = (patch: Partial<CmsBlock>) => onChange((b) => ({ ...b, ...patch }));
|
|
const fid = (name: string) => `block-${id}-${name}`;
|
|
const topErrors = Object.entries(errors).filter(([path]) => !INLINE[block.type]?.includes(path) && !LISTED.test(path));
|
|
|
|
const text = (name: "heading" | "text" | "buttonLabel" | "buttonUrl", labelCode: string) => (
|
|
<Field id={fid(name)} label={t(labelCode)} error={errors[name]}>
|
|
<Input id={fid(name)} value={block[name] ?? ""} disabled={saving} onChange={(e) => set({ [name]: e.target.value })} />
|
|
</Field>
|
|
);
|
|
|
|
const richText = (labelCode: string) => (
|
|
<Field id={fid("html")} label={t(labelCode)} error={errors.html}>
|
|
{/* Inline images upload on insert: SAME-REQUEST, file_autocommit=Y. An image later removed
|
|
from the text stays stored and unreferenced; it is never reaped. */}
|
|
<RichTextEditor
|
|
id={fid("html")}
|
|
value={block.html ?? ""}
|
|
disabled={saving}
|
|
onChange={(html) => set({ html })}
|
|
onUploadImage={props.uploadInlineImage}
|
|
trustedImageHosts={props.trustedImageHosts}
|
|
/>
|
|
</Field>
|
|
);
|
|
|
|
const image = (
|
|
<Field id={fid("image")} label={t("CmsBlockImage")} error={errors.imageUrl}>
|
|
<Input
|
|
id={fid("image")}
|
|
type="file"
|
|
accept="image/png,image/jpeg,image/webp,image/gif"
|
|
disabled={saving}
|
|
onChange={(e) => {
|
|
const file = e.target.files?.[0];
|
|
if (file) props.onChooseImage(file);
|
|
}}
|
|
/>
|
|
{pendingImage ? <p className="text-xs text-muted-foreground">{t("CmsUploadsOnSave", { name: pendingImage.name })}</p> : null}
|
|
{pendingImage?.preview || block.imageUrl ? (
|
|
// eslint-disable-next-line @next/next/no-img-element -- a local data URL or a drive URL of unknown size
|
|
<img src={pendingImage?.preview ?? block.imageUrl} alt={t("CatImagePreviewAlt")} className="max-h-40 rounded-md border" />
|
|
) : null}
|
|
</Field>
|
|
);
|
|
|
|
const titleOf = (cid: number) => collections.find((c) => c.id === cid)?.title ?? t("CmsUnknownCollection", { id: cid });
|
|
const ids = block.collectionIds ?? [];
|
|
const moveId = (pos: number, delta: -1 | 1) => {
|
|
const next = [...ids];
|
|
[next[pos], next[pos + delta]] = [next[pos + delta], next[pos]];
|
|
set({ collectionIds: next });
|
|
};
|
|
|
|
const items = block.items ?? [];
|
|
const setItems = (next: typeof items) => set({ items: next });
|
|
|
|
return (
|
|
<fieldset className="space-y-4 rounded-lg border bg-card p-4">
|
|
<legend className="px-1 text-sm font-medium">
|
|
{t("CmsBlockLegend", { number: index + 1, type: t(BLOCK_LABELS[block.type]) })}
|
|
</legend>
|
|
<div className="flex items-center gap-1">
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsMoveUp")} disabled={saving || index === 0} onClick={() => onMove(-1)}>
|
|
<ArrowUp aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsMoveDown")} disabled={saving || index === count - 1} onClick={() => onMove(1)}>
|
|
<ArrowDown aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsRemove")} disabled={saving} onClick={onRemove}>
|
|
<Trash2 aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
</div>
|
|
{topErrors.map(([path, message]) => <FieldError key={path} message={message} />)}
|
|
|
|
{block.type === "HERO" && (
|
|
<>
|
|
{image}
|
|
{text("heading", "CmsHeading")}
|
|
{text("text", "CmsText")}
|
|
{text("buttonLabel", "CmsButtonLabel")}
|
|
{text("buttonUrl", "CmsButtonLink")}
|
|
</>
|
|
)}
|
|
|
|
{block.type === "RICH_TEXT" && richText("CmsContent")}
|
|
|
|
{block.type === "IMAGE_TEXT" && (
|
|
<>
|
|
{image}
|
|
<RadioGroup
|
|
aria-label={t("CmsImageSide")}
|
|
value={block.imagePosition ?? "LEFT"}
|
|
disabled={saving}
|
|
onValueChange={(v) => set({ imagePosition: v as "LEFT" | "RIGHT" })}
|
|
className="flex gap-4"
|
|
>
|
|
{(["LEFT", "RIGHT"] as const).map((side) => (
|
|
<div key={side} className="flex items-center gap-2">
|
|
<RadioGroupItem value={side} id={fid(side)} />
|
|
<Label htmlFor={fid(side)} className="font-normal">
|
|
{t(side === "LEFT" ? "CmsImageLeft" : "CmsImageRight")}
|
|
</Label>
|
|
</div>
|
|
))}
|
|
</RadioGroup>
|
|
<FieldError message={errors.imagePosition} />
|
|
{text("heading", "CmsHeading")}
|
|
{richText("CmsText")}
|
|
{text("buttonLabel", "CmsButtonLabel")}
|
|
{text("buttonUrl", "CmsButtonLink")}
|
|
</>
|
|
)}
|
|
|
|
{block.type === "FEATURED_PRODUCTS" && (
|
|
<>
|
|
{text("heading", "CmsHeading")}
|
|
<Field id={fid("collection")} label={t("CollCardCollection")} error={errors.collectionId}>
|
|
<select
|
|
id={fid("collection")}
|
|
className={SELECT_CLASS}
|
|
value={block.collectionId ?? ""}
|
|
disabled={saving}
|
|
onChange={(e) => set({ collectionId: e.target.value ? Number(e.target.value) : null })}
|
|
>
|
|
<option value="">{t("CmsChooseCollection")}</option>
|
|
{collections.map((c) => <option key={c.id} value={c.id}>{c.title}</option>)}
|
|
</select>
|
|
</Field>
|
|
<Field id={fid("limit")} label={t("CmsNumberOfProducts")} error={errors.limit}>
|
|
<Input
|
|
id={fid("limit")}
|
|
type="number"
|
|
min={1}
|
|
max={24}
|
|
className="w-28"
|
|
value={block.limit ?? ""}
|
|
disabled={saving}
|
|
onChange={(e) => set({ limit: e.target.value === "" ? undefined : Number(e.target.value) })}
|
|
/>
|
|
</Field>
|
|
</>
|
|
)}
|
|
|
|
{block.type === "COLLECTION_LIST" && (
|
|
<>
|
|
{text("heading", "CmsHeading")}
|
|
<Field id={fid("add-collection")} label={t("CmsAddCollection")} error={errors.collectionIds}>
|
|
<select
|
|
id={fid("add-collection")}
|
|
className={SELECT_CLASS}
|
|
value=""
|
|
disabled={saving}
|
|
onChange={(e) => {
|
|
const cid = Number(e.target.value);
|
|
if (cid && !ids.includes(cid)) set({ collectionIds: [...ids, cid] });
|
|
}}
|
|
>
|
|
<option value="">{t("CmsChooseCollection")}</option>
|
|
{collections.filter((c) => !ids.includes(c.id)).map((c) => <option key={c.id} value={c.id}>{c.title}</option>)}
|
|
</select>
|
|
</Field>
|
|
{ids.length > 0 && (
|
|
<ul className="divide-y rounded-md border">
|
|
{ids.map((cid, pos) => (
|
|
<li key={cid} className="flex items-center gap-1 px-3 py-1.5 text-sm">
|
|
<span className="min-w-0 flex-1">{titleOf(cid)}</span>
|
|
<FieldError message={errors[`collectionIds[${pos}]`]} />
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsMoveItemUp", { title: titleOf(cid) })} disabled={saving || pos === 0} onClick={() => moveId(pos, -1)}>
|
|
<ArrowUp aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsMoveItemDown", { title: titleOf(cid) })} disabled={saving || pos === ids.length - 1} onClick={() => moveId(pos, 1)}>
|
|
<ArrowDown aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
<Button type="button" variant="ghost" size="sm" aria-label={t("CmsRemoveItem", { title: titleOf(cid) })} disabled={saving} onClick={() => set({ collectionIds: ids.filter((x) => x !== cid) })}>
|
|
<Trash2 aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</>
|
|
)}
|
|
|
|
{block.type === "FAQ" && (
|
|
<>
|
|
{text("heading", "CmsHeading")}
|
|
{items.map((item, i) => (
|
|
<div key={i} className="space-y-2 rounded-md border p-3">
|
|
<Field id={fid(`q${i}`)} label={t("CmsQuestionN", { number: i + 1 })} error={errors[`items[${i}].question`]}>
|
|
<Input
|
|
id={fid(`q${i}`)}
|
|
value={item.question}
|
|
disabled={saving}
|
|
onChange={(e) => setItems(items.map((it, j) => (j === i ? { ...it, question: e.target.value } : it)))}
|
|
/>
|
|
</Field>
|
|
<Field id={fid(`a${i}`)} label={t("CmsAnswerN", { number: i + 1 })} error={errors[`items[${i}].answer`]}>
|
|
<Textarea
|
|
id={fid(`a${i}`)}
|
|
rows={3}
|
|
value={item.answer}
|
|
disabled={saving}
|
|
onChange={(e) => setItems(items.map((it, j) => (j === i ? { ...it, answer: e.target.value } : it)))}
|
|
/>
|
|
</Field>
|
|
<Button type="button" variant="ghost" size="sm" disabled={saving} onClick={() => setItems(items.filter((_, j) => j !== i))}>
|
|
{t("CmsRemoveQuestionN", { number: i + 1 })}
|
|
</Button>
|
|
</div>
|
|
))}
|
|
<FieldError message={errors.items} />
|
|
<Button type="button" variant="outline" size="sm" disabled={saving} onClick={() => setItems([...items, { question: "", answer: "" }])}>
|
|
<Plus aria-hidden="true" className="size-4" />
|
|
{t("CmsAddQuestion")}
|
|
</Button>
|
|
</>
|
|
)}
|
|
|
|
{block.type === "FORM" && (
|
|
<>
|
|
{text("heading", "CmsHeading")}
|
|
<Field id={fid("form")} label={t("CmsBlockForm")} error={errors.formId}>
|
|
<select
|
|
id={fid("form")}
|
|
className={SELECT_CLASS}
|
|
value={block.formId ?? ""}
|
|
disabled={saving}
|
|
onChange={(e) => set({ formId: e.target.value ? Number(e.target.value) : null })}
|
|
>
|
|
<option value="">{t("CmsChooseForm")}</option>
|
|
{forms.map((f) => (
|
|
<option key={f.id} value={f.id}>
|
|
{f.status === "N" ? t("CmsFormHiddenOption", { name: f.name }) : f.name}
|
|
</option>
|
|
))}
|
|
{block.formId != null && !forms.some((f) => f.id === block.formId) ? (
|
|
<option value={block.formId}>{t("CmsUnknownForm", { id: block.formId })}</option>
|
|
) : null}
|
|
</select>
|
|
</Field>
|
|
</>
|
|
)}
|
|
</fieldset>
|
|
);
|
|
}
|