Skip to content

Commit

Permalink
refactor getBibles
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Dec 26, 2024
1 parent 4444c15 commit cfbbefe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
20 changes: 13 additions & 7 deletions src/lib/getBibleStaticProps.ts → src/lib/getBibles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BibleIndexProps } from '~src/containers/bible';
import { getAudiobibleIndexData } from '~src/containers/bible/__generated__';
import { BOOK_ID_MAP } from '~src/services/fcbh/constants';
import { getBibleBookChapters } from '~src/services/fcbh/getBibleBookChapters';
import { getBibles } from '~src/services/fcbh/getBibles';
import { getBibles as _getFcbhBibles } from '~src/services/fcbh/getBibles';
import { IBibleBookChapter, IBibleVersion } from '~src/services/fcbh/types';

import root from './routes';
Expand Down Expand Up @@ -87,11 +87,11 @@ async function transform(
};
}

export async function getFcbhBibles(
async function getFcbhBibles(
languageRoute: string,
): Promise<ApiBible[] | null> {
try {
const response = await getBibles();
const response = await _getFcbhBibles();

if (!response) {
return null;
Expand All @@ -104,21 +104,27 @@ export async function getFcbhBibles(
}
}

export async function getApiBibles(
languageId: Language,
): Promise<ApiBible[] | null> {
async function getApiBibles(languageId: Language): Promise<ApiBible[] | null> {
const apiData = await getAudiobibleIndexData({
language: languageId,
}).catch(() => null);

return apiData?.collections.nodes || null;
}

export function concatBibles(
function concatBibles(
first: ApiBible[] | null,
second: ApiBible[] | null,
): ApiBible[] {
return [...(first || []), ...(second || [])].sort((a, b) =>
a.title.localeCompare(b.title),
);
}

export default async function getBibles(languageId: Language) {
const fcbh = await getFcbhBibles(languageId);
const api = await getApiBibles(languageId);
const all = concatBibles(fcbh, api);

return { fcbh, api, all };
}
18 changes: 6 additions & 12 deletions src/pages/[language]/bibles/chapters/[id]/[[...slugs]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { REVALIDATE, REVALIDATE_FAILURE } from '~lib/constants';
import { getDetailStaticPaths } from '~lib/getDetailStaticPaths';
import { RecordingContentType } from '~src/__generated__/graphql';
import { BibleIndexProps } from '~src/containers/bible';
import {
concatBibles,
getApiBibles,
getFcbhBibles,
} from '~src/lib/getBibleStaticProps';
import getBibles from '~src/lib/getBibles';
import { getLanguageIdByRoute } from '~src/lib/getLanguageIdByRoute';

export default Recording;
Expand All @@ -37,6 +33,7 @@ export async function getStaticProps({
const { recording } = await getAudiobibleBookDetailData({
id: params?.id || '',
});

if (
!recording ||
recording.contentType !== RecordingContentType.BibleChapter
Expand All @@ -49,25 +46,22 @@ export async function getStaticProps({

const languageRoute = params?.language || 'en';
const languageId = getLanguageIdByRoute(languageRoute);
const { fcbh, api, all } = await getBibles(languageId);

const apiBibles = await getApiBibles(languageId);

if (!apiBibles) {
if (!api) {
return {
notFound: true,
revalidate: REVALIDATE_FAILURE,
};
}

const fcbhBibles = await getFcbhBibles(languageRoute);

return {
props: {
versions: concatBibles(fcbhBibles, apiBibles),
versions: all,
recording,
title: recording?.title,
},
revalidate: fcbhBibles ? REVALIDATE : REVALIDATE_FAILURE,
revalidate: fcbh ? REVALIDATE : REVALIDATE_FAILURE,
};
}

Expand Down
18 changes: 5 additions & 13 deletions src/pages/[language]/bibles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { IBaseProps } from '~containers/base';
import Bible, { BibleIndexProps } from '~containers/bible';
import { LANGUAGES, REVALIDATE, REVALIDATE_FAILURE } from '~lib/constants';
import root from '~lib/routes';
import {
concatBibles,
getApiBibles,
getFcbhBibles,
} from '~src/lib/getBibleStaticProps';
import getBibles from '~src/lib/getBibles';
import getIntl from '~src/lib/getIntl';
import { getLanguageIdByRoute } from '~src/lib/getLanguageIdByRoute';

Expand All @@ -25,29 +21,25 @@ export async function getStaticProps({
> {
const languageRoute = params?.language || 'en';
const languageId = getLanguageIdByRoute(languageRoute);

const intl = await getIntl(languageId);
const { fcbh, api, all } = await getBibles(languageId);

const apiBibles = await getApiBibles(languageId);

if (!apiBibles) {
if (!api) {
return {
notFound: true,
revalidate: REVALIDATE_FAILURE,
};
}

const fcbhBibles = await getFcbhBibles(languageRoute);

return {
props: {
versions: concatBibles(fcbhBibles, apiBibles),
versions: all,
title: intl.formatMessage({
id: 'bible__title',
defaultMessage: 'Bible',
}),
},
revalidate: fcbhBibles ? REVALIDATE : REVALIDATE_FAILURE,
revalidate: fcbh ? REVALIDATE : REVALIDATE_FAILURE,
};
}

Expand Down

0 comments on commit cfbbefe

Please sign in to comment.