diff --git a/apps/researcher/src/app/[locale]/research-guide/filterGuides.test.ts b/apps/researcher/src/app/[locale]/research-guide/filterGuides.test.ts deleted file mode 100644 index 53d400a6..00000000 --- a/apps/researcher/src/app/[locale]/research-guide/filterGuides.test.ts +++ /dev/null @@ -1,197 +0,0 @@ -import {ResearchGuide} from '@colonial-collections/api'; -import {filterLevel3Guides, sortResearchGuide} from './filterGuides'; - -describe('filterLevel3Guides', () => { - it('filters out level 1 and level 2 guides from level 3 list', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: 'level1-1', - seeAlso: [ - { - id: 'level2-1', - seeAlso: [{id: 'level3-1'}, {id: 'level1-2'}, {id: 'level2-2'}], - }, - ], - }, - {id: 'level1-2', seeAlso: [{id: 'level2-2'}]}, - ], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: 'level1-1', - seeAlso: [ - { - id: 'level2-1', - seeAlso: [{id: 'level3-1'}], - }, - ], - }, - {id: 'level1-2', seeAlso: [{id: 'level2-2', seeAlso: []}]}, - ], - }; - - const result = filterLevel3Guides(topLevel); - expect(result).toEqual(expected); - }); - - it('handles cases with no level 3 guides', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: 'level1-1', - seeAlso: [ - { - id: 'level2-1', - seeAlso: [{id: 'level1-2'}], - }, - ], - }, - {id: 'level1-2'}, - ], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: 'level1-1', - seeAlso: [ - { - id: 'level2-1', - seeAlso: [], - }, - ], - }, - {id: 'level1-2'}, - ], - }; - - const result = filterLevel3Guides(topLevel); - expect(result).toEqual(expected); - }); - - it('handles cases with no seeAlso arrays', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [], - }; - - const result = filterLevel3Guides(topLevel); - expect(result).toEqual(expected); - }); -}); - -describe('sortResearchGuide', () => { - it('sorts guides by their names', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [ - {id: '2', name: 'Beta'}, - {id: '1', name: 'Alpha'}, - {id: '3', name: 'Gamma'}, - ], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [ - {id: '1', name: 'Alpha'}, - {id: '2', name: 'Beta'}, - {id: '3', name: 'Gamma'}, - ], - }; - - const result = sortResearchGuide(topLevel); - expect(result).toEqual(expected); - }); - - it('handles guides with missing names', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [{id: '2', name: 'Beta'}, {id: '1'}, {id: '3', name: 'Gamma'}], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [{id: '1'}, {id: '2', name: 'Beta'}, {id: '3', name: 'Gamma'}], - }; - - const result = sortResearchGuide(topLevel); - expect(result).toEqual(expected); - }); - - it('handles empty seeAlso arrays', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [], - }; - - const result = sortResearchGuide(topLevel); - expect(result).toEqual(expected); - }); - - it('sorts nested seeAlso arrays', () => { - const topLevel: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: '1', - name: 'Alpha', - seeAlso: [ - {id: '3', name: 'Gamma'}, - {id: '2', name: 'Beta'}, - ], - }, - { - id: '2', - name: 'Beta', - seeAlso: [ - {id: '5', name: 'Epsilon'}, - {id: '4', name: 'Delta'}, - ], - }, - ], - }; - - const expected: ResearchGuide = { - id: 'top', - seeAlso: [ - { - id: '1', - name: 'Alpha', - seeAlso: [ - {id: '2', name: 'Beta'}, - {id: '3', name: 'Gamma'}, - ], - }, - { - id: '2', - name: 'Beta', - seeAlso: [ - {id: '4', name: 'Delta'}, - {id: '5', name: 'Epsilon'}, - ], - }, - ], - }; - - const result = sortResearchGuide(topLevel); - expect(result).toEqual(expected); - }); -}); diff --git a/apps/researcher/src/app/[locale]/research-guide/filterGuides.ts b/apps/researcher/src/app/[locale]/research-guide/filterGuides.ts deleted file mode 100644 index 01423a0b..00000000 --- a/apps/researcher/src/app/[locale]/research-guide/filterGuides.ts +++ /dev/null @@ -1,49 +0,0 @@ -import {ResearchGuide} from '@colonial-collections/api'; - -/** - * Filters out level 1 and level 2 guides from the seeAlso arrays of level 2 guides. - * - * Assumptions: - * - topLevel.seeAlso contains level 1 guides. - * - Each level 1 guide's seeAlso contains level 2 guides. - * - Each level 2 guide's seeAlso may contain level 1, level 2, and level 3 guides. - */ -export function filterLevel3Guides(topLevel: ResearchGuide): ResearchGuide { - topLevel.seeAlso?.forEach(level1Guide => { - level1Guide.seeAlso?.forEach(level2Guide => { - const filteredSeeAlso = - level2Guide.seeAlso?.filter(guide => { - // Check if the guide is a level 1 guide - const isLevel1Guide = topLevel.seeAlso?.some( - l1 => l1.id === guide.id - ); - // Check if the guide is a level 2 guide - const isLevel2Guide = topLevel.seeAlso?.some( - l1 => l1.seeAlso?.some(l2 => l2.id === guide.id) - ); - - // If the guide is not a level 1 or level 2 guide, it's a level 3 guide - return !isLevel1Guide && !isLevel2Guide; - }) || []; - level2Guide.seeAlso = filteredSeeAlso; - }); - }); - - return topLevel; -} - -export function sortResearchGuide(topLevel: ResearchGuide): ResearchGuide { - const sortGuides = (guide: ResearchGuide): ResearchGuide => { - const sortedSeeAlso = - guide.seeAlso?.sort((a, b) => - (a.name || '').localeCompare(b.name || '') - ) || undefined; - - return { - ...guide, - seeAlso: sortedSeeAlso?.map(sortGuides), - }; - }; - - return sortGuides(topLevel); -} diff --git a/apps/researcher/src/app/[locale]/research-guide/page.tsx b/apps/researcher/src/app/[locale]/research-guide/page.tsx index 728b4d44..cf7c0b4f 100644 --- a/apps/researcher/src/app/[locale]/research-guide/page.tsx +++ b/apps/researcher/src/app/[locale]/research-guide/page.tsx @@ -5,10 +5,7 @@ import {Link} from '@/navigation'; import {ChevronRightIcon} from '@heroicons/react/24/solid'; import {getLocale, getTranslations} from 'next-intl/server'; import StringToMarkdown from './string-to-markdown'; -import { - filterLevel3Guides, - sortResearchGuide, -} from '@/app/[locale]/research-guide/filterGuides'; +import {sortResearchGuide} from '@/app/[locale]/research-guide/sort-guides'; export default async function Page() { const locale = (await getLocale()) as LocaleEnum; @@ -23,10 +20,9 @@ export default async function Page() { const topLevel = topLevels[0]; const sortedGuides = sortResearchGuide(topLevel); - const filteredTopLevel = filterLevel3Guides(sortedGuides); - const firstLevel1Guide = filteredTopLevel.seeAlso?.[0]; - const nextLevel1Guides = filteredTopLevel.seeAlso?.slice(1) || []; + const firstLevel1Guide = sortedGuides.hasParts?.[0]; + const nextLevel1Guides = sortedGuides.hasParts?.slice(1) || []; return ( <> @@ -45,7 +41,7 @@ export default async function Page() { {firstLevel1Guide.name}
- {firstLevel1Guide.seeAlso?.map(item => ( + {firstLevel1Guide.hasParts?.map(item => (
- {level1Guide.seeAlso?.map(level2Guides => ( + {level1Guide.hasParts?.map(level2Guides => (
- {level2Guides.seeAlso?.map(level3Guides => ( + {level2Guides.hasParts?.map(level3Guides => ( { + it('sorts guides by their names', () => { + const topLevel: ResearchGuide = { + id: 'top', + hasParts: [ + {id: '2', name: 'Beta'}, + {id: '1', name: 'Alpha'}, + {id: '3', name: 'Gamma'}, + ], + }; + + const expected: ResearchGuide = { + id: 'top', + hasParts: [ + {id: '1', name: 'Alpha'}, + {id: '2', name: 'Beta'}, + {id: '3', name: 'Gamma'}, + ], + }; + + const result = sortResearchGuide(topLevel); + expect(result).toEqual(expected); + }); + + it('handles guides with missing names', () => { + const topLevel: ResearchGuide = { + id: 'top', + hasParts: [{id: '2', name: 'Beta'}, {id: '1'}, {id: '3', name: 'Gamma'}], + }; + + const expected: ResearchGuide = { + id: 'top', + hasParts: [{id: '1'}, {id: '2', name: 'Beta'}, {id: '3', name: 'Gamma'}], + }; + + const result = sortResearchGuide(topLevel); + expect(result).toEqual(expected); + }); + + it('handles empty hasParts arrays', () => { + const topLevel: ResearchGuide = { + id: 'top', + hasParts: [], + }; + + const expected: ResearchGuide = { + id: 'top', + hasParts: [], + }; + + const result = sortResearchGuide(topLevel); + expect(result).toEqual(expected); + }); + + it('sorts nested hasParts arrays', () => { + const topLevel: ResearchGuide = { + id: 'top', + hasParts: [ + { + id: '1', + name: 'Alpha', + hasParts: [ + {id: '3', name: 'Gamma'}, + {id: '2', name: 'Beta'}, + ], + }, + { + id: '2', + name: 'Beta', + hasParts: [ + {id: '5', name: 'Epsilon'}, + {id: '4', name: 'Delta'}, + ], + }, + ], + }; + + const expected: ResearchGuide = { + id: 'top', + hasParts: [ + { + id: '1', + name: 'Alpha', + hasParts: [ + {id: '2', name: 'Beta'}, + {id: '3', name: 'Gamma'}, + ], + }, + { + id: '2', + name: 'Beta', + hasParts: [ + {id: '4', name: 'Delta'}, + {id: '5', name: 'Epsilon'}, + ], + }, + ], + }; + + const result = sortResearchGuide(topLevel); + expect(result).toEqual(expected); + }); +}); diff --git a/apps/researcher/src/app/[locale]/research-guide/sort-guides.ts b/apps/researcher/src/app/[locale]/research-guide/sort-guides.ts new file mode 100644 index 00000000..d436023b --- /dev/null +++ b/apps/researcher/src/app/[locale]/research-guide/sort-guides.ts @@ -0,0 +1,17 @@ +import {ResearchGuide} from '@colonial-collections/api'; + +export function sortResearchGuide(topLevel: ResearchGuide) { + const sortGuides = (guide: ResearchGuide): ResearchGuide => { + const sortedParts = + guide.hasParts?.sort((a, b) => + (a.name || '').localeCompare(b.name || '') + ) || undefined; + + return { + ...guide, + hasParts: sortedParts?.map(sortGuides), + }; + }; + + return sortGuides(topLevel); +}