-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #730 from colonial-heritage/research-guide-frontend
Research guide frontend
- Loading branch information
Showing
7 changed files
with
364 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
apps/researcher/src/app/[locale]/research-guide/filterGuides.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
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'}, | ||
], | ||
}; | ||
|
||
const expected: ResearchGuide = { | ||
id: 'top', | ||
seeAlso: [ | ||
{ | ||
id: 'level1-1', | ||
seeAlso: [ | ||
{ | ||
id: 'level2-1', | ||
seeAlso: [{id: 'level3-1'}], | ||
}, | ||
], | ||
}, | ||
{id: 'level1-2'}, | ||
], | ||
}; | ||
|
||
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: 'level2-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); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
apps/researcher/src/app/[locale]/research-guide/filterGuides.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 || '') | ||
) || []; | ||
|
||
return { | ||
...guide, | ||
seeAlso: sortedSeeAlso.map(sortGuides), | ||
}; | ||
}; | ||
|
||
return sortGuides(topLevel); | ||
} |
Oops, something went wrong.