Files
co-lite/solvway-admin-portal/app/pos/_components/GiftCardCodesField.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

121 lines
4.0 KiB
TypeScript

"use client";
import { useState } from "react";
import { X } from "lucide-react";
import { cn } from "@/app/lib/utils";
import { useMoney } from "@/app/pos/currency";
import { useT } from "@/app/lib/i18n/MessagesProvider";
import type { AppliedGiftCard } from "@/app/lib/types";
import { checkPosGiftCard, cardCover } from "@/app/lib/services/posGiftCards";
// add-gift-card-counter: the cashier enters up to 3 gift card codes to pay towards a POS total.
// Codes are checked against the balance API; refused codes show an error, applied cards show
// what they contribute and the remaining balance.
interface GiftCardCodesFieldProps {
cards: AppliedGiftCard[];
total: number;
onChange: (cards: AppliedGiftCard[]) => void;
disabled: boolean;
}
export function GiftCardCodesField({ cards, total, onChange, disabled }: GiftCardCodesFieldProps) {
const money = useMoney();
const t = useT();
const [leftPre, leftPost] = t("PosLeftToPay").split("{amount}");
const [code, setCode] = useState("");
const [checking, setChecking] = useState(false);
const [error, setError] = useState<string | null>(null);
const { perCard, left } = cardCover(cards, total);
async function apply() {
const normalised = code.replace(/[\s-]/g, "").toUpperCase();
if (!normalised) return;
// Duplicate: refuse without calling the API
if (cards.some((c) => c.code === normalised)) {
setError("PosGcAlreadyApplied");
return;
}
setChecking(true);
setError(null);
try {
const res = await checkPosGiftCard(normalised);
onChange([...cards, { code: normalised, last4: res.last4, balance: res.balance }]);
} catch (err) {
setError(err instanceof Error && err.message ? err.message : "PosGcCheckFailed");
} finally {
setChecking(false);
setCode("");
}
}
function remove(index: number) {
onChange(cards.filter((_, i) => i !== index));
}
return (
<div className="rounded-lg border bg-muted/40 p-3">
<div className="text-sm font-semibold mb-2">{t("PosGiftCards")}</div>
{cards.map((card, i) => (
<div key={card.code} className="flex items-center justify-between py-1">
<span className="text-sm">
•••• {card.last4}{" "}
<span className="text-muted-foreground">
{t("PosGcCoverage", { amount: money(perCard[i]), balance: money(card.balance) })}
</span>
</span>
<button
type="button"
aria-label={t("PosRemoveGiftCard")}
disabled={disabled || checking}
onClick={() => remove(i)}
className="p-1 text-muted-foreground hover:text-foreground"
>
<X className="size-4" />
</button>
</div>
))}
{cards.length > 0 && (
<div className="text-sm mt-1">
{leftPre}<span className="font-semibold">{money(left)}</span>{leftPost}
</div>
)}
{error && <p role="alert" className="text-sm text-destructive mt-2">{t(error)}</p>}
<div className="flex gap-2 mt-2">
<input
type="text"
aria-label={t("PosGiftCardCode")}
value={code}
onChange={(e) => setCode(e.target.value)}
disabled={disabled || checking || cards.length >= 3}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
apply();
}
}}
className={cn(
"flex-1 rounded-md border bg-background px-3 py-1.5 text-sm outline-none",
"placeholder:text-muted-foreground",
"focus:ring-2 focus:ring-primary/30",
)}
placeholder={t("PosEnterCode")}
/>
<button
type="button"
disabled={disabled || checking || cards.length >= 3 || !code.trim()}
onClick={apply}
className="rounded-md bg-primary px-3 py-1.5 text-sm text-primary-foreground disabled:opacity-50"
>
{checking ? t("PosChecking") : t("PosApply")}
</button>
</div>
</div>
);
}