diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-02 18:57:29 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-03 15:22:24 +0200 |
| commit | 732d0943f8041d76262222a092b014f2557085ef (patch) | |
| tree | 16f6f76648b479a9591400ab15bb3e9c914f2226 /src/services | |
| parent | ca921d7536cfe950b5a7d442977bbf900b48faf4 (diff) | |
chore: add homepage
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/graphql/articles.ts | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/src/services/graphql/articles.ts b/src/services/graphql/articles.ts index e5ce7a5..7aff3e0 100644 --- a/src/services/graphql/articles.ts +++ b/src/services/graphql/articles.ts @@ -1,10 +1,19 @@ -import { Article } from '@ts/types/app'; -import { RawArticle, TotalItems } from '@ts/types/raw-data'; +import { 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 { articlesQuery, totalArticlesQuery } from './articles.query'; +import { + articlesCardQuery, + articlesQuery, + totalArticlesQuery, +} from './articles.query'; /** * Retrieve the total number of articles. @@ -102,3 +111,40 @@ export const getArticles = async ({ pageInfo: response.posts.pageInfo, }; }; + +/** + * Convert a raw article preview to an article card. + * + * @param {RawArticlePreview} data - A raw article preview. + * @returns {ArticleCard} An article card. + */ +const getArticleCardFromRawData = (data: RawArticlePreview): ArticleCard => { + const { databaseId, date, featuredImage, slug, title } = data; + + return { + cover: featuredImage ? getImageFromRawData(featuredImage.node) : undefined, + dates: getDates(date, ''), + id: databaseId, + slug, + title, + }; +}; + +/** + * Retrieve the given number of article cards from API. + * + * @param {EdgesVars} obj - An object. + * @param {number} obj.first - The number of articles. + * @returns {Promise<ArticleCard[]>} - The article cards data. + */ +export const getArticlesCard = async ({ + first, +}: EdgesVars): Promise<ArticleCard[]> => { + const response = await fetchAPI<RawArticlePreview, typeof articlesCardQuery>({ + api: getAPIUrl(), + query: articlesCardQuery, + variables: { first }, + }); + + return response.posts.nodes.map((node) => getArticleCardFromRawData(node)); +}; |
