-
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.
Research Guide frontend: Do not use the removed identifier property a…
…nd new top level layout.
- Loading branch information
Showing
3 changed files
with
220 additions
and
43 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
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,109 @@ | ||
import {filterLevel3Guides, sortLevel1Guides} from './filterGuides'; | ||
|
||
describe('filterLevel3Guides', () => { | ||
it('only shows each level 3 guide once', () => { | ||
const topLevel = { | ||
id: 'top', | ||
seeAlso: [ | ||
{ | ||
id: 'level1-1', | ||
seeAlso: [ | ||
{ | ||
id: 'level2-1', | ||
seeAlso: [{id: 'level3-1'}, {id: 'level3-2'}, {id: 'level3-3'}], | ||
}, | ||
{ | ||
id: 'level2-2', | ||
seeAlso: [{id: 'level3-1'}, {id: 'level3-4'}], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'level1-2', | ||
seeAlso: [ | ||
{ | ||
id: 'level2-3', | ||
seeAlso: [{id: 'level3-2'}, {id: 'level3-5'}], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const filteredTopLevel = filterLevel3Guides(topLevel); | ||
|
||
expect(filteredTopLevel.seeAlso?.[0].seeAlso?.[0].seeAlso).toEqual([ | ||
{id: 'level3-1'}, | ||
{id: 'level3-2'}, | ||
{id: 'level3-3'}, | ||
]); | ||
expect(filteredTopLevel.seeAlso?.[0].seeAlso?.[1].seeAlso).toEqual([ | ||
{id: 'level3-4'}, | ||
]); | ||
expect(filteredTopLevel.seeAlso?.[1].seeAlso?.[0].seeAlso).toEqual([ | ||
{id: 'level3-5'}, | ||
]); | ||
}); | ||
|
||
it('filters out level 1 and 2 guides from the level 2 seeAlso', () => { | ||
const topLevel = { | ||
id: 'top', | ||
seeAlso: [ | ||
{ | ||
id: 'level1-1', | ||
seeAlso: [ | ||
{ | ||
id: 'level2-1', | ||
seeAlso: [ | ||
{id: 'level1-2'}, // Level 1 guide | ||
{id: 'level2-2'}, // Level 2 guide | ||
{id: 'level3-1'}, // Level 3 guide | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'level1-2', | ||
seeAlso: [ | ||
{ | ||
id: 'level2-2', | ||
seeAlso: [ | ||
{id: 'level3-2'}, // Level 3 guide | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const filteredTopLevel = filterLevel3Guides(topLevel); | ||
|
||
expect(filteredTopLevel.seeAlso?.[0].seeAlso?.[0].seeAlso).toEqual([ | ||
{id: 'level3-1'}, | ||
]); | ||
expect(filteredTopLevel.seeAlso?.[1].seeAlso?.[0].seeAlso).toEqual([ | ||
{id: 'level3-2'}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('sortLevel1Guides', () => { | ||
it('sorts level 1 guides by their names', () => { | ||
const topLevel = { | ||
id: 'top', | ||
seeAlso: [ | ||
{id: '2', name: 'B'}, | ||
{id: '1', name: 'A'}, | ||
{id: '3', name: 'C'}, | ||
], | ||
}; | ||
|
||
const sortedLevel1Guides = sortLevel1Guides(topLevel); | ||
|
||
expect(sortedLevel1Guides).toEqual([ | ||
{id: '1', name: 'A'}, | ||
{id: '2', name: 'B'}, | ||
{id: '3', name: 'C'}, | ||
]); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
type Guide = { | ||
id: string; | ||
name?: string; | ||
seeAlso?: Guide[]; | ||
}; | ||
|
||
/** | ||
* Filters out level 1 and level 2 guides from the seeAlso arrays of level 2 guides | ||
* and ensures each level 3 guide is only shown once. | ||
* | ||
* 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. | ||
* - Level 3 guides should only be shown once across all level 2 guides. | ||
*/ | ||
export const filterLevel3Guides = (topLevel: Guide): Guide => { | ||
const displayedLevel3Guides = new Set<string>(); | ||
|
||
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 and has not been displayed yet, it's a level 3 guide | ||
if ( | ||
!isLevel1Guide && | ||
!isLevel2Guide && | ||
!displayedLevel3Guides.has(guide.id) | ||
) { | ||
displayedLevel3Guides.add(guide.id); | ||
return true; | ||
} | ||
return false; | ||
}) || []; | ||
level2Guide.seeAlso = filteredSeeAlso; | ||
}); | ||
}); | ||
|
||
return topLevel; | ||
}; | ||
|
||
/** | ||
* Sorts the level 1 guides by their names. | ||
*/ | ||
export const sortLevel1Guides = (topLevel: Guide): Guide[] => { | ||
return ( | ||
topLevel.seeAlso?.sort((a, b) => | ||
(a.name || '').localeCompare(b.name || '') | ||
) || [] | ||
); | ||
}; |
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