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
147 lines
5.2 KiB
TypeScript
147 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import { DIAL_CODES, PINNED, flagEmoji, type DialCode } from "@/app/lib/dialCodes";
|
|
|
|
interface DialCodeSelectProps {
|
|
/** Currently selected dial code, e.g. "+60". */
|
|
value: string;
|
|
/** Called with the chosen dial code when the user selects an entry. */
|
|
onChange: (dialCode: string) => void;
|
|
}
|
|
|
|
const triggerClass =
|
|
"rounded-xl px-2 py-3 text-sm font-semibold outline-none transition-all duration-200 flex-shrink-0 w-[92px] flex items-center justify-center gap-1 cursor-pointer";
|
|
|
|
/** PINNED entries first (in PINNED order), then the rest alphabetically by name. */
|
|
function orderedList(): DialCode[] {
|
|
const pinned = PINNED
|
|
.map((iso2) => DIAL_CODES.find((d) => d.iso2 === iso2))
|
|
.filter((d): d is DialCode => Boolean(d));
|
|
const rest = DIAL_CODES
|
|
.filter((d) => !PINNED.includes(d.iso2))
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
return [...pinned, ...rest];
|
|
}
|
|
|
|
const ORDERED = orderedList();
|
|
|
|
export default function DialCodeSelect({ value, onChange }: DialCodeSelectProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const [query, setQuery] = useState("");
|
|
// Track the chosen country so the trigger flag reflects the selection even
|
|
// for shared dial codes (e.g. +1). Falls back to the first match of `value`.
|
|
const [selectedIso2, setSelectedIso2] = useState<string | null>(null);
|
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const onDocClick = (e: MouseEvent) => {
|
|
if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
|
|
};
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape") setOpen(false);
|
|
};
|
|
document.addEventListener("mousedown", onDocClick);
|
|
document.addEventListener("keydown", onKey);
|
|
return () => {
|
|
document.removeEventListener("mousedown", onDocClick);
|
|
document.removeEventListener("keydown", onKey);
|
|
};
|
|
}, [open]);
|
|
|
|
// Derive the trigger's country during render: prefer the explicitly chosen
|
|
// country (so shared dial codes like +1 keep the right flag), but ignore it
|
|
// once the controlled `value` no longer matches, falling back to the first
|
|
// match of `value`. Deriving here avoids syncing state in an effect.
|
|
const current = useMemo(() => {
|
|
const chosen = DIAL_CODES.find((d) => d.iso2 === selectedIso2);
|
|
if (chosen && chosen.dialCode === value) return chosen;
|
|
return DIAL_CODES.find((d) => d.dialCode === value);
|
|
}, [selectedIso2, value]);
|
|
|
|
const items = useMemo(() => {
|
|
const q = query.trim().toLowerCase();
|
|
if (!q) return ORDERED;
|
|
return ORDERED.filter(
|
|
(d) => d.name.toLowerCase().includes(q) || d.dialCode.includes(q),
|
|
);
|
|
}, [query]);
|
|
|
|
const select = (d: DialCode) => {
|
|
setSelectedIso2(d.iso2);
|
|
onChange(d.dialCode);
|
|
setOpen(false);
|
|
setQuery("");
|
|
};
|
|
|
|
return (
|
|
<div ref={rootRef} className="relative flex-shrink-0">
|
|
<button
|
|
type="button"
|
|
onClick={() => setOpen((o) => !o)}
|
|
aria-haspopup="listbox"
|
|
aria-expanded={open}
|
|
className={triggerClass}
|
|
style={{
|
|
background: "var(--bg-primary)",
|
|
border: `1.5px solid ${open ? "rgba(255,107,0,0.65)" : "var(--border-color)"}`,
|
|
boxShadow: open ? "0 0 0 3px rgba(255,107,0,0.1)" : "none",
|
|
color: "var(--text-primary)",
|
|
}}
|
|
>
|
|
<span>{current ? flagEmoji(current.iso2) : "🏳️"}</span>
|
|
<span>{value}</span>
|
|
</button>
|
|
|
|
{open && (
|
|
<div
|
|
className="absolute z-50 mt-1 w-64 rounded-xl overflow-hidden"
|
|
style={{
|
|
background: "var(--bg-card)",
|
|
border: "1px solid var(--border-color)",
|
|
boxShadow: "var(--shadow-card)",
|
|
}}
|
|
>
|
|
<input
|
|
autoFocus
|
|
type="text"
|
|
placeholder="Search country or code"
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
className="w-full px-3 py-2 text-sm outline-none"
|
|
style={{
|
|
background: "var(--bg-primary)",
|
|
color: "var(--text-primary)",
|
|
borderBottom: "1px solid var(--border-color)",
|
|
}}
|
|
/>
|
|
<ul role="listbox" aria-label="Country dial codes" className="max-h-56 overflow-y-auto">
|
|
{items.map((d) => (
|
|
<li key={d.iso2}>
|
|
<button
|
|
type="button"
|
|
role="option"
|
|
aria-selected={d.dialCode === value}
|
|
onClick={() => select(d)}
|
|
className="w-full flex items-center gap-2 px-3 py-2 text-sm text-left hover:opacity-80"
|
|
style={{ color: "var(--text-primary)" }}
|
|
>
|
|
<span>{flagEmoji(d.iso2)}</span>
|
|
<span className="flex-1 truncate">{d.name}</span>
|
|
<span style={{ color: "var(--text-muted)" }}>{d.dialCode}</span>
|
|
</button>
|
|
</li>
|
|
))}
|
|
{items.length === 0 && (
|
|
<li className="px-3 py-2 text-sm" style={{ color: "var(--text-muted)" }}>
|
|
No matches
|
|
</li>
|
|
)}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|