qazaq-legacy-foundation-web/src/lib/content.ts

46 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import config from '@payload-config'
import { getPayload } from 'payload'
import { visibleNews } from './news-model'
import type { Locale } from '@/i18n'
import type { CmsHome, CmsNews } from './home-model'
import type { CmsLegal } from './legal-model'
/** Читает контент из Payload. Любая ошибка (нет базы, не создана таблица) — пустой контент, страница остаётся рабочей. */
export async function loadContent(locale: Locale): Promise<{ home: CmsHome | null; news: CmsNews[] }> {
try {
const payload = await getPayload({ config })
const [home, news] = await Promise.all([
payload.findGlobal({ slug: 'home', locale, fallbackLocale: false }),
loadNews(locale),
])
return { home: home as unknown as CmsHome, news: visibleNews(news).slice(0, 3) }
} catch (e) {
console.error('content load failed', e)
return { home: null, news: [] }
}
}
/** Юридические документы. Без подстановки другого языка: пустой текст на языке страницы → «нужно уточнить». */
export async function loadLegal(locale: Locale): Promise<CmsLegal | null> {
try {
const payload = await getPayload({ config })
return (await payload.findGlobal({ slug: 'legal', locale, fallbackLocale: false })) as unknown as CmsLegal
} catch (e) {
console.error('legal load failed', e)
return null
}
}
/** Все новости текущего языка; не ограничиваем каталог тремя карточками главной. */
export async function loadNews(locale: Locale): Promise<CmsNews[]> {
const payload = await getPayload({ config })
const items: CmsNews[] = []
let page = 1
while (true) {
const result = await payload.find({ collection: 'news', locale, fallbackLocale: false, sort: '-date', limit: 100, page })
items.push(...result.docs as unknown as CmsNews[])
if (!result.hasNextPage) return items
page++
}
}