nxtgauge-admin-solid/src/routes/admin/order.tsx

145 lines
5.5 KiB
TypeScript
Raw Normal View History

import { createResource, createSignal, createMemo, Show, For } from 'solid-js';
import AdminShell from '~/components/AdminShell';
const API = '/api/gateway';
type Order = {
id: string;
order_number?: string;
user_name?: string;
user_email?: string;
package_name?: string;
tracecoin_amount?: number;
coupon_code?: string;
total?: number;
amount?: number;
status: string;
created_at?: string;
};
async function loadOrders(): Promise<Order[]> {
try {
const res = await fetch(`${API}/api/admin/orders`);
if (!res.ok) throw new Error('Failed to load');
const data = await res.json();
return Array.isArray(data) ? data : (data.orders || []);
} catch {
return [];
}
}
function statusClass(status: string): string {
const s = (status || '').toUpperCase();
if (s === 'PAID' || s === 'COMPLETED') return 'active';
if (s === 'PENDING') return 'pending';
if (s === 'FAILED') return 'failed';
return '';
}
function statusStyle(status: string): string {
const s = (status || '').toUpperCase();
if (s === 'PAID' || s === 'COMPLETED') return 'background:#dcfce7;color:#166534;border-color:#bbf7d0';
if (s === 'PENDING') return 'background:#fff7ed;color:#c2410c;border-color:#fed7aa';
if (s === 'FAILED') return 'background:#fee2e2;color:#991b1b;border-color:#fecaca';
if (s === 'REFUNDED') return 'background:#f1f5f9;color:#475569;border-color:#e2e8f0';
return '';
}
export default function OrderPage() {
const [orders] = createResource(loadOrders);
const [search, setSearch] = createSignal('');
const filtered = createMemo(() => {
const q = search().toLowerCase().trim();
const all = orders() ?? [];
if (!q) return all;
return all.filter((o) => {
const orderNum = (o.order_number || o.id || '').toLowerCase();
const email = (o.user_email || '').toLowerCase();
const name = (o.user_name || '').toLowerCase();
const status = (o.status || '').toLowerCase();
return orderNum.includes(q) || email.includes(q) || name.includes(q) || status.includes(q);
});
});
const formatAmount = (order: Order) => {
const raw = order.total ?? order.amount;
if (raw == null) return '—';
return `${(raw / 100).toFixed(2)}`;
};
return (
<AdminShell>
<div class="mb-6 flex items-start justify-between gap-4">
<div>
<h1 class="text-2xl font-bold text-gray-900">Order Management</h1>
<p class="mt-1 text-sm text-gray-500">TraceCoin package purchase orders</p>
</div>
</div>
<div style="margin-bottom:16px">
<input
type="text"
placeholder="Search by order number, user email, or status..."
value={search()}
onInput={(e) => setSearch(e.currentTarget.value)}
style="width:100%;max-width:420px;padding:8px 12px;border:1px solid #e2e8f0;border-radius:8px;font-size:14px"
/>
</div>
<section class="rounded-xl border border-gray-200 bg-white shadow-sm" style="padding: 0; overflow: hidden;">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr>
<th>Order #</th>
<th>User</th>
<th>Package</th>
<th>TraceCoins</th>
<th>Coupon</th>
<th>Total</th>
<th>Status</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<Show when={orders.loading}>
<tr><td colspan="8" style="text-align:center;padding:32px;color:#64748b">Loading...</td></tr>
</Show>
<Show when={!orders.loading && orders.error}>
<tr><td colspan="8" style="text-align:center;padding:32px;color:#b91c1c">Failed to load. Is the backend running?</td></tr>
</Show>
<Show when={!orders.loading && !orders.error && filtered().length === 0}>
<tr><td colspan="8" style="text-align:center;padding:32px;color:#94a3b8">No orders found.</td></tr>
</Show>
<Show when={!orders.loading && !orders.error && filtered().length > 0}>
<For each={filtered()}>
{(item) => (
<tr>
<td style="font-weight:600;color:#0f172a;font-family:monospace">{item.order_number || item.id}</td>
<td style="color:#475569">{item.user_name || item.user_email || '—'}</td>
<td style="color:#475569">{item.package_name || '—'}</td>
<td style="color:#475569">{item.tracecoin_amount ?? '—'}</td>
<td style="color:#475569">{item.coupon_code || '—'}</td>
<td style="color:#475569">{formatAmount(item)}</td>
<td>
<span
class="inline-flex items-center rounded-full bg-gray-100 px-2.5 py-0.5 text-xs font-medium text-gray-600"
style={`${statusStyle(item.status)};padding:2px 10px;border-radius:999px;font-size:12px;font-weight:600;border:1px solid`}
>
{item.status || '—'}
</span>
</td>
<td style="color:#475569">{item.created_at ? new Date(item.created_at).toLocaleString() : '—'}</td>
</tr>
)}
</For>
</Show>
</tbody>
</table>
</div>
</section>
</AdminShell>
);
}