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

37 lines
1.7 KiB
TypeScript

import { apiDelete, apiGet, apiPost, apiPut } from "@/app/lib/clientApi";
import type { CmsPage, pageBody } from "@/app/lib/cmsPages";
export interface PageListParams {
q?: string;
page: number;
pageSize: number;
companyId?: number;
}
export async function listPages(params: PageListParams): Promise<{ items: CmsPage[]; total: number }> {
const qs = new URLSearchParams();
if (params.q) qs.set("q", params.q);
if (params.companyId) qs.set("companyId", String(params.companyId));
qs.set("page", String(Math.max(0, params.page - 1)));
qs.set("size", String(params.pageSize));
const raw = await apiGet<{ content?: CmsPage[]; totalElements?: number }>(`/api/pages?${qs.toString()}`);
const items = Array.isArray(raw?.content) ? raw.content : [];
return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length };
}
export const getPage = (id: number) => apiGet<CmsPage>(`/api/pages/${id}`);
export const savePage = (id: number | undefined, body: ReturnType<typeof pageBody>) =>
id == null ? apiPost<CmsPage>("/api/pages", body) : apiPut<CmsPage>(`/api/pages/${id}`, body);
/** Makes a published page's saved draft live; refusals carry a field like a save. */
export const publishPage = (id: number) => apiPost<CmsPage>(`/api/pages/${id}/publish`, {});
export const discardPage = (id: number) => apiPost<CmsPage>(`/api/pages/${id}/discard`, {});
export const deletePage = (id: number) => apiDelete<unknown>(`/api/pages/${id}`);
/** Base64 without the data-URL prefix; the counter checks type (PNG, JPEG, WebP) and size (2 MB). */
export const uploadPageImage = (companyId: number | undefined, data: string) =>
apiPost<{ id: number }>("/api/pages/images", { companyId, data });