diff --git a/apps/admin/src/main.ts b/apps/admin/src/main.ts index 0117b3c..fafaf30 100644 --- a/apps/admin/src/main.ts +++ b/apps/admin/src/main.ts @@ -1,15 +1,34 @@ -import type { ApiClient } from "@personal-brand/api-client"; +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 { const { root, client } = this.options; const [bookings, unread] = await Promise.all([client.listAdminBookings(), client.getAdminUnreadCount()]); - root.innerHTML = `

品牌后台

未读订单:${unread.count}

`; + this.bookings = bookings; + root.innerHTML = `

品牌后台

未读订单:${unread.count}

选择订单查看详情
`; + root.querySelector("#refresh")?.addEventListener("click", () => void this.load()); + root.querySelector("#status")?.addEventListener("change", () => void this.filter()); + root.querySelectorAll("[data-id]").forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id))); } + + private listHtml(): string { return this.bookings.map((item) => `
  • `).join("") || "
  • 暂无订单
  • "; } + private async filter(): Promise { 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 = `

    ${this.selected.bookingNo}

    客户:${this.selected.customerName}

    `; detail.querySelector("#save")?.addEventListener("click", () => void this.save()); } + private async save(): Promise { 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; +} diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index 4523cb6..c4044b2 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -5,6 +5,7 @@ import type { FulfillmentType, PublicService, } from "@personal-brand/types"; +export type { BookingStatus } from "@personal-brand/types"; export interface BrandProfile { id: string;