From dab72bb270ee2ee47a0b472d5e9e240cba7cbf0f Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 13 May 2022 15:39:55 +0200 Subject: chore: handle blog pagination --- src/services/graphql/articles.ts | 66 +++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 21 deletions(-) (limited to 'src/services') diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts index 7aff3e0..1eb112e 100644 --- a/src/services/graphql/articles.ts +++ b/src/services/graphql/articles.ts @@ -1,17 +1,18 @@ -import { type Article, type ArticleCard } from '@ts/types/app'; +import { Slug, type Article, type ArticleCard } from '@ts/types/app'; import { type RawArticle, type RawArticlePreview, type TotalItems, } from '@ts/types/raw-data'; import { getAuthorFromRawData } from '@utils/helpers/author'; -import { getDates } from '@utils/helpers/dates'; import { getImageFromRawData } from '@utils/helpers/images'; import { getPageLinkFromRawData } from '@utils/helpers/pages'; -import { EdgesVars, fetchAPI, getAPIUrl, PageInfo } from './api'; +import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl, PageInfo } from './api'; import { + articleBySlugQuery, articlesCardQuery, articlesQuery, + articlesSlugQuery, totalArticlesQuery, } from './articles.query'; @@ -66,10 +67,7 @@ export const getArticleFromRawData = (data: RawArticle): Article => { cover: featuredImage?.node ? getImageFromRawData(featuredImage.node) : undefined, - dates: { - publication: date, - update: modified, - }, + dates: { publication: date, update: modified }, readingTime: info.readingTime, seo: { description: seo?.metaDesc || '', @@ -91,25 +89,19 @@ export const getArticleFromRawData = (data: RawArticle): Article => { /** * Retrieve the given number of articles from API. * - * @param {EdgesVars} obj - An object. - * @param {number} obj.first - The number of articles. - * @returns {Promise} - The articles data. + * @param {EdgesVars} props - An object of GraphQL variables. + * @returns {Promise>} The articles data. */ -export const getArticles = async ({ - first, -}: EdgesVars): Promise => { +export const getArticles = async ( + props: EdgesVars +): Promise> => { const response = await fetchAPI({ api: getAPIUrl(), query: articlesQuery, - variables: { first }, + variables: { ...props }, }); - return { - articles: response.posts.edges.map((edge) => - getArticleFromRawData(edge.node) - ), - pageInfo: response.posts.pageInfo, - }; + return response.posts; }; /** @@ -123,7 +115,7 @@ const getArticleCardFromRawData = (data: RawArticlePreview): ArticleCard => { return { cover: featuredImage ? getImageFromRawData(featuredImage.node) : undefined, - dates: getDates(date, ''), + dates: { publication: date }, id: databaseId, slug, title, @@ -148,3 +140,35 @@ export const getArticlesCard = async ({ return response.posts.nodes.map((node) => getArticleCardFromRawData(node)); }; + +/** + * Retrieve an Article object by slug. + * + * @param {string} slug - The article slug. + * @returns {Promise
} The requested article. + */ +export const getArticleBySlug = async (slug: string): Promise
=> { + const response = await fetchAPI({ + api: getAPIUrl(), + query: articleBySlugQuery, + variables: { slug }, + }); + + return getArticleFromRawData(response.post); +}; + +/** + * Retrieve all the articles slugs. + * + * @returns {Promise} - An array of articles slugs. + */ +export const getAllArticlesSlugs = async (): Promise => { + const totalArticles = await getTotalArticles(); + const response = await fetchAPI({ + api: getAPIUrl(), + query: articlesSlugQuery, + variables: { first: totalArticles }, + }); + + return response.posts.edges.map((edge) => edge.node.slug); +}; -- cgit v1.2.3