Files
manoon-headless/src/lib/saleor/products.ts
Unchained 7c05bd2346 Redesign phase 1: Homepage polish and design system foundation
- Fix newsletter subscribe box centering on homepage
- Fix header overlap on product pages (pt-[72px] instead of pt-[100px])
- Add scroll-mt-[72px] for smooth scroll anchor offset
- Add HeroVideo component with video hero placeholder
- Add REDESIGN_SPECIFICATION.md with 9-phase design plan
- Clean up globals.css theme declarations and comments
- Update Header with improved sticky behavior and cart
- Update ProductDetail with better layout and spacing
- Update CartDrawer with improved slide-out cart UI
- Add English translations for updated pages
- Various CSS refinements across pages
2026-03-21 16:22:17 +02:00

152 lines
3.9 KiB
TypeScript

import { saleorClient } from "./client";
import { GET_PRODUCTS, GET_PRODUCT_BY_SLUG } from "./queries/Products";
import type { Product } from "@/types/saleor";
const CHANNEL = process.env.NEXT_PUBLIC_SALEOR_CHANNEL || "default-channel";
// GraphQL Response Types
interface ProductsResponse {
products?: {
edges: Array<{ node: Product }>;
};
}
interface ProductResponse {
product?: Product | null;
}
export async function getProducts(
locale: string = "SR",
first: number = 100
): Promise<Product[]> {
try {
const { data } = await saleorClient.query<ProductsResponse>({
query: GET_PRODUCTS,
variables: {
channel: CHANNEL,
locale: locale.toUpperCase(),
first,
},
});
return data?.products?.edges.map((edge) => edge.node) || [];
} catch (error) {
console.error("Error fetching products from Saleor:", error);
return [];
}
}
export async function getProductBySlug(
slug: string,
locale: string = "SR"
): Promise<Product | null> {
try {
const { data } = await saleorClient.query<ProductResponse>({
query: GET_PRODUCT_BY_SLUG,
variables: {
slug,
channel: CHANNEL,
locale: locale.toUpperCase(),
},
});
return data?.product || null;
} catch (error) {
console.error(`Error fetching product ${slug} from Saleor:`, error);
return null;
}
}
export function getProductPrice(product: Product): string {
const variant = product.variants?.[0];
if (!variant?.pricing?.price?.gross?.amount) {
return "";
}
return formatPrice(
variant.pricing.price.gross.amount,
variant.pricing.price.gross.currency
);
}
export function getProductImage(product: Product): string {
if (product.media && product.media.length > 0) {
return product.media[0].url;
}
if (product.variants?.[0]?.media && product.variants[0].media.length > 0) {
return product.variants[0].media[0].url;
}
return "/placeholder-product.jpg";
}
export function isProductAvailable(product: Product): boolean {
const variant = product.variants?.[0];
if (!variant) return false;
return (variant.quantityAvailable || 0) > 0;
}
export function formatPrice(amount: number, currency: string = "RSD"): string {
return new Intl.NumberFormat("sr-RS", {
style: "currency",
currency: currency,
minimumFractionDigits: 0,
}).format(amount);
}
// Parse Saleor's JSON description format (EditorJS) to plain text/HTML
export function parseDescription(description: string | null | undefined): string {
if (!description) return "";
// If it's already plain text (not JSON), return as-is
if (!description.startsWith("{")) {
return description;
}
try {
const parsed = JSON.parse(description);
// Handle EditorJS format: { blocks: [{ data: { text: "..." } }] }
if (parsed.blocks && Array.isArray(parsed.blocks)) {
return parsed.blocks
.map((block: any) => {
if (block.data?.text) {
return block.data.text;
}
return "";
})
.filter(Boolean)
.join("\n\n");
}
// Fallback: return stringified if unknown format
return description;
} catch (e) {
// If JSON parse fails, return original
return description;
}
}
// Get localized product data
export function getLocalizedProduct(
product: Product,
locale: string = "SR"
): {
name: string;
slug: string;
description: string;
seoTitle?: string;
seoDescription?: string;
} {
const isEnglish = locale.toLowerCase() === "en";
const translation = isEnglish ? product.translation : null;
const rawDescription = translation?.description || product.description;
return {
name: translation?.name || product.name,
slug: translation?.slug || product.slug,
description: parseDescription(rawDescription),
seoTitle: translation?.seoTitle || product.seoTitle,
seoDescription: translation?.seoDescription || product.seoDescription,
};
}