qazaq-legacy-foundation-web/src/lib/content.ts
Peppa fc2b5a4127
All checks were successful
peppa/lint lint: success за 1с
peppa/typecheck typecheck: success за 2с
peppa/test test: success за 0с
peppa/build build: success за 41с
ci / checks (pull_request) Successful in 1m12s
deploy / deploy (pull_request) Successful in 14s
QLFW-55: учредитель, директор и попечительский совет на странице «О фонде»
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 18:59:21 +05:00

59 lines
2.6 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 { cache } from 'react'
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'
import type { CmsFoundation } from './foundation-people'
/** Читает контент из 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 const loadLegal = cache(async (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++
}
}
/** Руководство фонда; при ошибке чтения — null, страница покажет данные по умолчанию. */
export async function loadFoundation(): Promise<CmsFoundation | null> {
try {
const payload = await getPayload({ config })
return (await payload.findGlobal({ slug: 'foundation' })) as unknown as CmsFoundation
} catch (e) {
console.error('foundation load failed', e)
return null
}
}