From a0d00743cbbdb77b27c1a3d5711407ffed5befac Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sat, 20 Aug 2022 21:39:51 +0200 Subject: refactor(types): move and rename GraphQL types The api file in services was not really readable. So I move the types and I also rewrite a little the fetch function. I also rename most of the type to avoid conflict with preexisting types (like Node) and to keep consistency. --- src/services/graphql/topics.ts | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) (limited to 'src/services/graphql/topics.ts') diff --git a/src/services/graphql/topics.ts b/src/services/graphql/topics.ts index 0b1971b..5448d89 100644 --- a/src/services/graphql/topics.ts +++ b/src/services/graphql/topics.ts @@ -1,4 +1,6 @@ import { PageLink, Slug, Topic } from '@ts/types/app'; +import { GraphQLEdgesInput } from '@ts/types/graphql/generics'; +import { EdgesResponse } from '@ts/types/graphql/queries'; import { RawArticle, RawTopic, @@ -6,8 +8,11 @@ import { TotalItems, } from '@ts/types/raw-data'; import { getImageFromRawData } from '@utils/helpers/images'; -import { getPageLinkFromRawData } from '@utils/helpers/pages'; -import { EdgesResponse, EdgesVars, fetchAPI, getAPIUrl } from './api'; +import { + getPageLinkFromRawData, + sortPageLinksByName, +} from '@utils/helpers/pages'; +import { fetchAPI } from './api'; import { getArticleFromRawData } from './articles'; import { topicBySlugQuery, @@ -23,7 +28,6 @@ import { */ export const getTotalTopics = async (): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: totalTopicsQuery, }); @@ -33,14 +37,13 @@ export const getTotalTopics = async (): Promise => { /** * Retrieve the given number of topics from API. * - * @param {EdgesVars} props - An object of GraphQL variables. + * @param {GraphQLEdgesInput} props - An object of GraphQL variables. * @returns {Promise>} The topics data. */ export const getTopicsPreview = async ( - props: EdgesVars + props: GraphQLEdgesInput ): Promise> => { const response = await fetchAPI({ - api: getAPIUrl(), query: topicsListQuery, variables: props, }); @@ -89,21 +92,8 @@ export const getTopicFromRawData = (data: RawTopic): Topic => { const uniqueThematics = thematics.filter( ({ id }, index) => !thematicsIds.includes(id, index + 1) ); - const sortThematicByName = (a: PageLink, b: PageLink) => { - var nameA = a.name.toUpperCase(); // ignore upper and lowercase - var nameB = b.name.toUpperCase(); // ignore upper and lowercase - if (nameA < nameB) { - return -1; - } - if (nameA > nameB) { - return 1; - } - - // names must be equal - return 0; - }; - return uniqueThematics.sort(sortThematicByName); + return uniqueThematics.sort(sortPageLinksByName); }; return { @@ -139,7 +129,6 @@ export const getTopicFromRawData = (data: RawTopic): Topic => { */ export const getTopicBySlug = async (slug: string): Promise => { const response = await fetchAPI({ - api: getAPIUrl(), query: topicBySlugQuery, variables: { slug }, }); @@ -155,7 +144,6 @@ export const getTopicBySlug = async (slug: string): Promise => { export const getAllTopicsSlugs = async (): Promise => { const totalTopics = await getTotalTopics(); const response = await fetchAPI({ - api: getAPIUrl(), query: topicsSlugQuery, variables: { first: totalTopics }, }); -- cgit v1.2.3