mirror of
https://git.meshkee.com/novintrades/website.git
synced 2026-08-12 05:00:58 +04:30
Initial commit: NovinTrades website monorepo.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user