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

106 lines
3.6 KiB
TypeScript

"use client";
import { useId, useState } from "react";
import { Button } from "@/app/components/ui/button";
import { Input } from "@/app/components/ui/input";
import { useT } from "@/app/lib/i18n/MessagesProvider";
import { walletLabelCode } from "@/app/lib/walletLabels";
import type { PosWalletOption } from "@/app/lib/types";
import { useMoney } from "@/app/pos/currency";
// One leg per wallet: an RM amount box, a slider, Clear / Use max. `cap` is the most this wallet may
// take right now (see walletCap); `error` is a message CODE shown on the leg.
export function WalletAmountLeg({
option,
value,
cap,
yellowTotal,
error,
onChange,
}: {
option: PosWalletOption;
value: number;
cap: number;
yellowTotal?: number;
error?: string | null;
onChange: (amount: number) => void;
}) {
const t = useT();
const formatMoney = useMoney();
const errorId = useId();
const draftOf = (n: number) => (n === 0 ? "" : String(n));
const [draft, setDraft] = useState(draftOf(value));
const [seen, setSeen] = useState(value);
// The value this box last reported; the parent echoing it back must not overwrite the draft.
const [typedValue, setTyped] = useState<number | null>(null);
// The box keeps what the operator typed ("0", "1.") until the value moves from outside (slider, Use max,
// Clear, a clamp), then it follows the value.
if (seen !== value) {
setSeen(value);
if (value !== typedValue) setDraft(draftOf(value));
setTyped(null);
}
const name = t(walletLabelCode(option.walletTypeCode));
const typed = (raw: string) => {
const n = Number.parseFloat(raw);
const rounded = Math.round((Number.isFinite(n) ? n : 0) * 100) / 100;
const clamped = Math.max(0, Math.min(rounded, cap));
setDraft(clamped !== rounded || raw === "" ? draftOf(clamped) : raw);
setTyped(clamped);
onChange(clamped);
};
return (
<div role="group" aria-label={name} className="space-y-2 rounded-lg border p-3">
<div className="flex items-baseline justify-between gap-2">
<strong className="text-sm">{name}</strong>
<span className="text-xs text-muted-foreground">
{t("WalletBalanceShort", { amount: option.balance.toFixed(2) })}
</span>
</div>
{option.walletTypeCode === "ACORN" && yellowTotal !== undefined && (
<p className="text-xs text-muted-foreground">
{t("AcornsYellowOnlyNote", { amount: formatMoney(yellowTotal) })}
</p>
)}
<div className="flex items-center gap-3">
<Input
type="number"
inputMode="decimal"
min={0}
max={cap}
step="0.01"
className="w-28"
value={draft}
placeholder="0.00"
aria-invalid={error ? true : undefined}
aria-describedby={error ? errorId : undefined}
aria-label={t("WalletAmountLabel", { wallet: name })}
onChange={(e) => typed(e.target.value)}
/>
<input
type="range"
className="flex-1"
min={0}
max={cap}
step="0.01"
value={value}
aria-label={t("WalletSliderLabel", { wallet: name })}
onChange={(e) => typed(e.target.value)}
/>
</div>
<div className="flex gap-2">
<Button type="button" variant="secondary" size="sm" onClick={() => onChange(0)}>{t("WalletClear")}</Button>
<Button type="button" variant="secondary" size="sm" onClick={() => onChange(cap)}>{t("WalletUseMax")}</Button>
</div>
{error && (
<p id={errorId} role="alert" className="text-sm text-destructive">
{t(error, { wallet: name })}
</p>
)}
</div>
);
}