Files
website/lib/website-carousels.ts
T

70 lines
2.0 KiB
TypeScript

import type { Producer } from "@/data/home";
const API_BASE_URL =
process.env.NEXT_PUBLIC_MARKETPLACE_API_URL?.replace(/\/$/, "") ||
process.env.MARKETPLACE_API_URL?.replace(/\/$/, "") ||
"http://localhost:3001/api/v1";
export type WebsiteCarouselBusiness = {
id: string;
slug: string;
name: string;
tagline: string;
about: string;
logoUrl: string | null;
bannerUrl: string | null;
categoryName: string | null;
productCount: number;
publishedAt: string | null;
};
export type WebsiteCarousel = {
id: string;
title: string;
subtext: string;
slug: string;
businesses: WebsiteCarouselBusiness[];
};
const PLACEHOLDER_COVER = "/images/producers/cover-laundry.png";
const PLACEHOLDER_LOGO = "/images/producers/logo-sani.png";
const DEFAULT_DESCRIPTION =
"we will show your business to verity of visitor around the world.";
export async function getWebsiteCarousels(): Promise<WebsiteCarousel[]> {
try {
const response = await fetch(`${API_BASE_URL}/website/carousels`, {
cache: "no-store",
});
if (!response.ok) return [];
const data = (await response.json()) as { carousels?: WebsiteCarousel[] };
return Array.isArray(data.carousels) ? data.carousels : [];
} catch {
return [];
}
}
export function carouselBusinessToProducer(
business: WebsiteCarouselBusiness,
): Producer {
const since = business.publishedAt
? String(new Date(business.publishedAt).getFullYear())
: "—";
return {
slug: business.slug,
name: business.name || business.slug,
tagline: business.tagline || "",
products: String(business.productCount ?? 0),
since,
// English about when present; otherwise English placeholder.
// Farsi about is already stripped by the API.
description: business.about?.trim() || DEFAULT_DESCRIPTION,
typeIcon: "gear",
typeLabel: "Business",
category: business.categoryName || "Business",
cover: business.bannerUrl || PLACEHOLDER_COVER,
logo: business.logoUrl || PLACEHOLDER_LOGO,
};
}