WIP 1.51.8: frontend on v2 for event detail, options, sections, attachments, costs, uploads
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
This commit is contained in:
@@ -216,6 +216,17 @@ export async function fetchManagedEvents<T = Event>(token: string | null | undef
|
||||
return data as unknown as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* One event in full (API v2 GET /events/{id}/full): ticket options with what's left, sessions,
|
||||
* form, attachments, bundles. Staff signed in (`authToken`) also see inactive and not-yet-live
|
||||
* events; `nextOptions` passes Next.js caching (e.g. revalidate) through for server pages.
|
||||
*/
|
||||
export async function fetchEvent<T = Event>(id: string, options: { authToken?: string | null; nextOptions?: RequestInit } = {}): Promise<T> {
|
||||
const { apiV2 } = await import("@/lib/apiV2");
|
||||
const { data } = await apiV2("get", "/events/{id}/full", { params: { id }, authToken: options.authToken, nextOptions: options.nextOptions });
|
||||
return data as unknown as T;
|
||||
}
|
||||
|
||||
export function fetchAllUsers(token: string, params: Record<string, string> = {}): Promise<User[]> {
|
||||
return fetchAllPages<User>("/api/users", token, params);
|
||||
}
|
||||
|
||||
@@ -27,18 +27,21 @@ type Body<O> = O extends { requestBody: { content: { "application/json": infer B
|
||||
|
||||
type IfAny<T, Then> = [T] extends [never] ? object : Then;
|
||||
export type V2Options<P extends keyof paths, M extends Method> =
|
||||
{ authToken?: string | null }
|
||||
{ authToken?: string | null; nextOptions?: RequestInit }
|
||||
& IfAny<PathParams<Op<P, M>>, { params: PathParams<Op<P, M>> }>
|
||||
& IfAny<QueryParams<Op<P, M>>, { query?: QueryParams<Op<P, M>> }>
|
||||
& IfAny<Body<Op<P, M>>, { body: Body<Op<P, M>> }>;
|
||||
|
||||
export function apiV2<M extends Method, P extends PathsWith<M>>(method: M, path: P, ...[options]: object extends V2Options<P, M> ? [V2Options<P, M>?] : [V2Options<P, M>]): Promise<V2Result<P, M>> {
|
||||
const opts = (options || {}) as { authToken?: string | null; params?: Record<string, string>; query?: Record<string, unknown>; body?: unknown };
|
||||
const opts = (options || {}) as { authToken?: string | null; nextOptions?: RequestInit; params?: Record<string, string>; query?: Record<string, unknown>; body?: unknown };
|
||||
let url = String(path).replace(/\{(\w+)\}/g, (_, name: string) => encodeURIComponent(String(opts.params?.[name] ?? "")));
|
||||
const qs = new URLSearchParams();
|
||||
for (const [k, v] of Object.entries(opts.query || {})) {
|
||||
if (v !== undefined && v !== null && v !== "") qs.set(k, String(v));
|
||||
}
|
||||
if (qs.toString()) url += `?${qs.toString()}`;
|
||||
return apiFetch<V2Result<P, M>>(`/api/v2${url}`, { method: method.toUpperCase(), authToken: opts.authToken, body: opts.body });
|
||||
return apiFetch<V2Result<P, M>>(`/api/v2${url}`, { method: method.toUpperCase(), authToken: opts.authToken, body: opts.body, nextOptions: opts.nextOptions });
|
||||
}
|
||||
|
||||
/** The JSON body a v2 endpoint takes. */
|
||||
export type V2Body<P extends keyof paths, M extends Method> = Body<Op<P, M>>;
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { apiV2, type V2Body } from "@/lib/apiV2";
|
||||
import type { EventAttachment, EventOption } from "@/types";
|
||||
|
||||
/**
|
||||
* Event set-up calls on API v2 (1.51.8): ticket options and variants, scanning sections,
|
||||
* attachments, planned costs and image uploads. Each returns what the screens already work with.
|
||||
*/
|
||||
|
||||
type Token = string | null | undefined;
|
||||
|
||||
// ─── Ticket options ──────────────────────────────────────────────────────────
|
||||
|
||||
export async function createTicketOption(token: Token, eventId: string, body: V2Body<"/events/{id}/ticket-options", "post">): Promise<EventOption> {
|
||||
const { data } = await apiV2("post", "/events/{id}/ticket-options", { params: { id: eventId }, authToken: token, body });
|
||||
return data as unknown as EventOption;
|
||||
}
|
||||
|
||||
/** Only what's sent changes; `earlyBirdTiers` and `variants`, when sent, are the full lists. */
|
||||
export async function updateTicketOption(token: Token, optionId: string, body: V2Body<"/ticket-options/{id}", "patch">): Promise<EventOption> {
|
||||
const { data } = await apiV2("patch", "/ticket-options/{id}", { params: { id: optionId }, authToken: token, body });
|
||||
return data as unknown as EventOption;
|
||||
}
|
||||
|
||||
export async function deleteTicketOption(token: Token, optionId: string): Promise<void> {
|
||||
await apiV2("delete", "/ticket-options/{id}", { params: { id: optionId }, authToken: token });
|
||||
}
|
||||
|
||||
// ─── Scanning sections ───────────────────────────────────────────────────────
|
||||
|
||||
export type SectionRecord = { id: string; eventId: string; name: string; allowedOptions?: { eventOptionId: string; eventOption?: { id: string; name: string } }[] };
|
||||
|
||||
export async function listSections<T = SectionRecord>(token: Token, eventId?: string): Promise<T[]> {
|
||||
const { data } = await apiV2("get", "/sections", { authToken: token, query: eventId ? { eventId } : {} });
|
||||
return data as unknown as T[];
|
||||
}
|
||||
|
||||
export async function createSection(token: Token, body: V2Body<"/sections", "post">): Promise<SectionRecord> {
|
||||
const { data } = await apiV2("post", "/sections", { authToken: token, body });
|
||||
return data as unknown as SectionRecord;
|
||||
}
|
||||
|
||||
export async function updateSection(token: Token, sectionId: string, body: V2Body<"/sections/{id}", "patch">): Promise<SectionRecord> {
|
||||
const { data } = await apiV2("patch", "/sections/{id}", { params: { id: sectionId }, authToken: token, body });
|
||||
return data as unknown as SectionRecord;
|
||||
}
|
||||
|
||||
export async function deleteSection(token: Token, sectionId: string): Promise<void> {
|
||||
await apiV2("delete", "/sections/{id}", { params: { id: sectionId }, authToken: token });
|
||||
}
|
||||
|
||||
// ─── Attachments ─────────────────────────────────────────────────────────────
|
||||
|
||||
export async function listAttachments(eventId: string): Promise<EventAttachment[]> {
|
||||
const { data } = await apiV2("get", "/events/{id}/attachments", { params: { id: eventId } });
|
||||
return data as unknown as EventAttachment[];
|
||||
}
|
||||
|
||||
/** Uploads a file (PDF, Office, text, CSV or ZIP, up to 20 MB) to an event. */
|
||||
export async function uploadAttachment(token: Token, eventId: string, file: File): Promise<EventAttachment> {
|
||||
const fd = new FormData();
|
||||
fd.append("file", file);
|
||||
const { apiFetch } = await import("@/lib/api");
|
||||
const { data } = await apiFetch<{ data: EventAttachment }>(`/api/v2/events/${encodeURIComponent(eventId)}/attachments`, { method: "POST", body: fd, authToken: token });
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function deleteAttachment(token: Token, eventId: string, attachmentId: string): Promise<void> {
|
||||
await apiV2("delete", "/events/{id}/attachments/{attachmentId}", { params: { id: eventId, attachmentId }, authToken: token });
|
||||
}
|
||||
|
||||
// ─── Planned costs ───────────────────────────────────────────────────────────
|
||||
|
||||
export async function createCost(token: Token, eventId: string, body: V2Body<"/events/{id}/costs", "post">): Promise<void> {
|
||||
await apiV2("post", "/events/{id}/costs", { params: { id: eventId }, authToken: token, body });
|
||||
}
|
||||
|
||||
export async function updateCost(token: Token, costId: string, body: V2Body<"/costs/{id}", "patch">): Promise<void> {
|
||||
await apiV2("patch", "/costs/{id}", { params: { id: costId }, authToken: token, body });
|
||||
}
|
||||
|
||||
export async function deleteCost(token: Token, costId: string): Promise<void> {
|
||||
await apiV2("delete", "/costs/{id}", { params: { id: costId }, authToken: token });
|
||||
}
|
||||
|
||||
// ─── Image uploads ───────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Uploads an image (field "image") and returns the path to save (`/uploads/…`). `logo` and
|
||||
* `favicon` work without a token during first-time setup.
|
||||
*/
|
||||
export async function uploadImage(token: Token, kind: "event-image" | "logo" | "favicon", file: File | Blob): Promise<string> {
|
||||
const fd = new FormData();
|
||||
fd.append("image", file);
|
||||
const { apiFetch } = await import("@/lib/api");
|
||||
const { data } = await apiFetch<{ data: { path: string; url: string } }>(`/api/v2/uploads/${kind}`, { method: "POST", body: fd, authToken: token });
|
||||
return data.path;
|
||||
}
|
||||
Reference in New Issue
Block a user