Files
co-lite/solvway-admin-portal/app/lib/services/publicRegistration.ts
T

48 lines
1.5 KiB
TypeScript

import { ApiError } from "@/app/lib/clientApi";
export interface RegistrationInfo {
eventName: string;
startDate: string;
endDate: string;
location: string;
companyName: string;
notice: { id: number; textEn: string; textMs: string };
interests: { id: number; name: string }[];
}
export interface RegistrationBody {
name: string;
mobile: string;
email: string;
companyName: string;
jobTitle: string;
interestIds: number[];
consent: boolean;
website: string;
}
const url = (token: string) => `/api/public/registration/${encodeURIComponent(token)}`;
// Plain fetch on purpose: clientApi redirects a 401 to the admin /login, which a visitor must never see.
export async function publicCall<T>(url: string, init?: RequestInit): Promise<T> {
let res: Response;
try {
res = await fetch(url, { ...init, headers: init?.body ? { "Content-Type": "application/json" } : {} });
} catch {
throw new ApiError("Request failed", 0);
}
const text = await res.text();
let data: unknown;
try {
data = text ? JSON.parse(text) : undefined;
} catch {
data = undefined;
}
if (!res.ok) throw new ApiError(`Request failed (${res.status})`, res.status, data);
return data as T;
}
export const getRegistration = (token: string) => publicCall<RegistrationInfo>(url(token));
export const submitRegistration = (token: string, body: RegistrationBody) =>
publicCall<{ eventName: string }>(url(token), { method: "POST", body: JSON.stringify(body) });