import * as React from "react" import { GetStaticPathsResult, GetStaticPropsResult } from "next" import { useRouter } from "next/router" import { DrupalNode, JsonApiResponse } from "next-drupal" import { DrupalJsonApiParams } from "drupal-jsonapi-params" import { drupal } from "lib/drupal" import { getMenus } from "lib/get-menus" import { Layout, LayoutProps } from "components/layout" import { Pager, PagerProps } from "components/pager" import { Node } from "components/node" import { Meta } from "components/meta" export const NUMBER_OF_POSTS_PER_PAGE = 2 export interface BlogPageProps extends LayoutProps { page: Pick nodes: DrupalNode[] } export default function BlogPage({ nodes, menus, page }: BlogPageProps) { const { locale } = useRouter() const title = locale === "en" ? "Latest Articles." : "Ăšltimas Publicaciones." return (

{title}

{nodes.length ? (
{nodes.map((article) => ( ))}
) : (

No posts found

)} {page ? ( (page === 0 ? `/blog` : `/blog/page/${page}`)} className="py-8 mt-8" /> ) : null}
) } export async function getStaticPaths(): Promise { // Use SSG for the first pages, then fallback to SSR for other pages. const paths = Array(5) .fill(0) .map((_, page) => ({ params: { page: `${page + 1}`, }, })) return { paths, fallback: "blocking", } } export async function getStaticProps( context ): Promise> { const current = parseInt(context.params.page) const params = new DrupalJsonApiParams() .addFilter( "field_site.meta.drupal_internal__target_id", process.env.DRUPAL_SITE_ID ) .addInclude(["uid", "field_image"]) .addFields("node--article", [ "title", "path", "body", "uid", "created", "field_image", ]) .addFields("user--user", ["field_name"]) .addFilter("status", "1") .addSort("created", "DESC") const result = await drupal.getResourceCollectionFromContext( "node--article", context, { deserialize: false, params: { ...params.getQueryObject(), page: { limit: NUMBER_OF_POSTS_PER_PAGE, offset: context.params.page ? NUMBER_OF_POSTS_PER_PAGE * current : 0, }, }, } ) if (!result.data?.length) { return { notFound: true, } } const nodes = drupal.deserialize(result) as DrupalNode[] return { props: { nodes, page: { current, total: Math.ceil(result.meta.count / NUMBER_OF_POSTS_PER_PAGE), }, menus: await getMenus(context), }, } }