Files
persionalBrand/apps/admin/src/main.ts
T

35 lines
3.4 KiB
TypeScript

import { ApiClient, type AdminBooking, type BookingStatus } from "@personal-brand/api-client";
export interface AdminDashboardOptions { client: ApiClient; root: HTMLElement }
export class AdminDashboard {
private bookings: AdminBooking[] = [];
private selected: AdminBooking | undefined;
constructor(private readonly options: AdminDashboardOptions) {}
async load(): Promise<void> {
const { root, client } = this.options;
const [bookings, unread] = await Promise.all([client.listAdminBookings(), client.getAdminUnreadCount()]);
this.bookings = bookings;
root.innerHTML = `<h1>品牌后台</h1><p>未读订单:${unread.count}</p><label>状态 <select id="status"><option value="">全部</option><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select></label><button id="refresh">刷新</button><ul id="bookings">${this.listHtml()}</ul><section id="detail">选择订单查看详情</section>`;
root.querySelector("#refresh")?.addEventListener("click", () => void this.load());
root.querySelector("#status")?.addEventListener("change", () => void this.filter());
root.querySelectorAll<HTMLElement>("[data-id]").forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id)));
}
private listHtml(): string { return this.bookings.map((item) => `<li><button data-id="${item.id}">${item.bookingNo} · ${item.customerName} · ${item.status}</button></li>`).join("") || "<li>暂无订单</li>"; }
private async filter(): Promise<void> { const status = (this.options.root.querySelector("#status") as HTMLSelectElement).value as BookingStatus | ""; this.bookings = await this.options.client.listAdminBookings(status ? { status } : {}); const list = this.options.root.querySelector("#bookings"); if (list) list.innerHTML = this.listHtml(); }
private select(id: string | undefined): void { this.selected = this.bookings.find((item) => item.id === id); if (!this.selected) return; const detail = this.options.root.querySelector("#detail"); if (!detail) return; detail.innerHTML = `<h2>${this.selected.bookingNo}</h2><p>客户:${this.selected.customerName}</p><select id="detail-status"><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select><textarea id="note" placeholder="内部备注">${this.selected.adminNote ?? ""}</textarea><button id="save">保存</button>`; detail.querySelector("#save")?.addEventListener("click", () => void this.save()); }
private async save(): Promise<void> { if (!this.selected) return; const detail = this.options.root.querySelector("#detail")!; const status = (detail.querySelector("#detail-status") as HTMLSelectElement).value as BookingStatus; const note = (detail.querySelector("#note") as HTMLTextAreaElement).value; this.selected = await this.options.client.updateAdminBookingStatus(this.selected.id, status, note); await this.load(); }
}
export const adminAppName = "personal-brand-admin";
export function bootstrapAdmin(root: HTMLElement, baseUrl = "http://localhost:3001"): AdminDashboard {
const token = localStorage.getItem("adminToken");
const client = new ApiClient({ baseUrl, fetcher: (input, init) => fetch(input, init), ...(token ? { token } : {}) });
const dashboard = new AdminDashboard({ client, root });
void dashboard.load();
return dashboard;
}