Files
co-lite/solvway-admin-portal/app/components/locations/tabs/GeoAddressTab.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

122 lines
3.9 KiB
TypeScript

"use client";
import { useEffect, useState, useCallback } from "react";
import { Plus, Minus } from "lucide-react";
import { Button } from "@/app/components/ui/button";
import { Skeleton } from "@/app/components/ui/skeleton";
import type { AdminGeoLocationNode } from "@/app/lib/types";
import { useT } from "@/app/lib/i18n/MessagesProvider";
async function fetchLevel(parentId?: number): Promise<AdminGeoLocationNode[]> {
const qs = parentId ? `?parentId=${parentId}` : "";
const res = await fetch(`/api/geo-locations${qs}`);
if (!res.ok) throw new Error("Failed to load geo locations");
return res.json();
}
function GeoRow({ node, depth = 0 }: { node: AdminGeoLocationNode; depth?: number }) {
const t = useT();
const [expanded, setExpanded] = useState(false);
const [children, setChildren] = useState<AdminGeoLocationNode[] | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
async function loadChildren() {
setLoading(true);
setError(false);
try {
setChildren(await fetchLevel(node.id));
} catch {
setError(true);
} finally {
setLoading(false);
}
}
async function toggle() {
if (expanded) {
setExpanded(false);
return;
}
if (children === null) {
await loadChildren();
}
setExpanded(true);
}
return (
<div style={{ paddingLeft: depth * 20 }}>
<div className="flex h-8 items-center gap-2 text-sm">
<button
type="button"
aria-label={expanded ? t("LocTreeCollapse", { name: node.name }) : t("LocTreeExpand", { name: node.name })}
onClick={toggle}
className="flex size-4 items-center justify-center rounded-sm border border-input"
>
{expanded ? <Minus className="size-2.5" /> : <Plus className="size-2.5" />}
</button>
<span>{node.name}</span>
</div>
{loading && (
<div role="status" aria-label={t("LocGeoLoadingChildren", { name: node.name })} className="pl-6">
<Skeleton className="h-4 w-32" />
</div>
)}
{expanded && error && (
<div className="flex items-center gap-2 pl-6 text-xs">
<span className="text-destructive">{t("LocGeoChildrenError", { name: node.name })}</span>
<button type="button" className="underline" onClick={loadChildren}>
{t("UiRetry")}
</button>
</div>
)}
{expanded &&
children?.map((child) => <GeoRow key={child.id} node={child} depth={depth + 1} />)}
</div>
);
}
export function GeoAddressTab() {
const t = useT();
const [roots, setRoots] = useState<AdminGeoLocationNode[] | null>(null);
const [error, setError] = useState(false);
const load = useCallback(() => {
setError(false);
setRoots(null);
fetchLevel()
.then(setRoots)
.catch(() => setError(true));
}, []);
// eslint-disable-next-line react-hooks/set-state-in-effect -- load resets geo-tree state ahead of its fetch; it is also the Retry handler, and the fetch itself is the actual synchronization.
useEffect(load, [load]);
return (
<div className="space-y-3">
<div>
<p className="text-sm font-medium">{t("LocGeoTitle")}</p>
<p className="text-xs text-muted-foreground">
{t("LocGeoDescription")}
</p>
</div>
{error && (
<div className="space-y-2">
<p className="text-sm text-destructive">{t("LocGeoTreeError")}</p>
<Button type="button" onClick={load}>
{t("UiRetry")}
</Button>
</div>
)}
{!error && roots === null && (
<div role="status" aria-label={t("LocGeoLoadingRegions")} className="space-y-2">
<Skeleton className="h-4 w-40" />
<Skeleton className="h-4 w-32" />
<Skeleton className="h-4 w-36" />
</div>
)}
{!error && roots?.map((root) => <GeoRow key={root.id} node={root} />)}
</div>
);
}