Initial commit: dashboard
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { getAccessToken } from "@/lib/meshkee-api";
|
||||
|
||||
const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_MARKETPLACE_API_URL?.replace(/\/$/, "") ||
|
||||
"http://localhost:3001/api/v1";
|
||||
|
||||
export type SpecialCarousel = {
|
||||
id: string;
|
||||
title: string;
|
||||
subtext: string;
|
||||
slug: string;
|
||||
businessIds: string[];
|
||||
};
|
||||
|
||||
export type SpecialBusinessesConfig = {
|
||||
carousels: SpecialCarousel[];
|
||||
};
|
||||
|
||||
async function parseResponse<T>(response: Response): Promise<T> {
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const message =
|
||||
typeof payload?.message === "string"
|
||||
? payload.message
|
||||
: Array.isArray(payload?.message)
|
||||
? payload.message.join(", ")
|
||||
: `Request failed (${response.status})`;
|
||||
throw new Error(message);
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
export async function getSpecialBusinesses(): Promise<SpecialBusinessesConfig> {
|
||||
const response = await fetch(`${API_BASE_URL}/website/special-businesses`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
return parseResponse<SpecialBusinessesConfig>(response);
|
||||
}
|
||||
|
||||
export async function saveSpecialBusinesses(
|
||||
body: SpecialBusinessesConfig,
|
||||
): Promise<SpecialBusinessesConfig> {
|
||||
const response = await fetch(`${API_BASE_URL}/website/special-businesses`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(getAccessToken()
|
||||
? { Authorization: `Bearer ${getAccessToken()}` }
|
||||
: {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return parseResponse<SpecialBusinessesConfig>(response);
|
||||
}
|
||||
|
||||
export function slugFromTitle(title: string) {
|
||||
return (
|
||||
title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "") || "carousel"
|
||||
);
|
||||
}
|
||||
|
||||
export function newCarousel(
|
||||
title = "New carousel",
|
||||
subtext = "",
|
||||
): SpecialCarousel {
|
||||
return {
|
||||
id: `carousel-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
||||
title,
|
||||
subtext,
|
||||
slug: slugFromTitle(title),
|
||||
businessIds: [],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user