mirror of
https://git.meshkee.com/novintrades/website.git
synced 2026-08-11 20:50:58 +04:30
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { api } from "./api";
|
|
import { byNewest, filterByCategorySlug, paginate } from "./format";
|
|
import type { Blog, Brand, Category, Reportage } from "./types";
|
|
|
|
export const PAGE_SIZE = 9;
|
|
|
|
export const ADMIN_URL =
|
|
import.meta.env.VITE_ADMIN_URL ?? "http://novintrades.local:5173";
|
|
|
|
export async function fetchTopCategories(): Promise<Category[]> {
|
|
const categories = await api<Category[]>("/api/categories");
|
|
return categories
|
|
.filter((category) => category.parentId === null)
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
export async function fetchApprovedBlogs(): Promise<Blog[]> {
|
|
const blogs = await api<Blog[]>("/api/blogs");
|
|
return blogs
|
|
.filter((blog) => blog.verificationStatus === "APPROVED")
|
|
.sort(byNewest);
|
|
}
|
|
|
|
export async function fetchApprovedReportages(): Promise<Reportage[]> {
|
|
const reportages = await api<Reportage[]>("/api/reportages");
|
|
return reportages
|
|
.filter((item) => item.verificationStatus === "APPROVED")
|
|
.sort(byNewest);
|
|
}
|
|
|
|
export async function fetchApprovedBrands(): Promise<Brand[]> {
|
|
const brands = await api<Brand[]>("/api/brands");
|
|
return brands
|
|
.filter((brand) => brand.verificationStatus === "APPROVED")
|
|
.sort(byNewest);
|
|
}
|
|
|
|
export async function fetchLatestBlogs(limit = 3): Promise<Blog[]> {
|
|
const blogs = await fetchApprovedBlogs();
|
|
return blogs.slice(0, limit);
|
|
}
|
|
|
|
export async function fetchLatestBrands(limit = 4): Promise<Brand[]> {
|
|
const brands = await fetchApprovedBrands();
|
|
return brands.slice(0, limit);
|
|
}
|
|
|
|
export async function fetchBlogBySlug(slug: string): Promise<Blog | null> {
|
|
const blogs = await fetchApprovedBlogs();
|
|
return blogs.find((blog) => blog.slug === slug) ?? null;
|
|
}
|
|
|
|
export async function fetchReportageBySlug(
|
|
slug: string,
|
|
): Promise<Reportage | null> {
|
|
const reportages = await fetchApprovedReportages();
|
|
return reportages.find((item) => item.slug === slug) ?? null;
|
|
}
|
|
|
|
export async function fetchBrandBySlug(slug: string): Promise<Brand | null> {
|
|
const brands = await fetchApprovedBrands();
|
|
return brands.find((brand) => brand.slug === slug) ?? null;
|
|
}
|
|
|
|
export function listPage<T extends { categories: Array<{ category: { slug: string } }> }>(
|
|
items: T[],
|
|
categorySlug: string | null,
|
|
page: number,
|
|
pageSize = PAGE_SIZE,
|
|
) {
|
|
return paginate(filterByCategorySlug(items, categorySlug), page, pageSize);
|
|
}
|