Skip to content

Commit 779e1d0

Browse files
committed
updateTopicChoices can filter for allowed tags
1 parent e2f6139 commit 779e1d0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/app/services/questionSearch.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export function initialiseListState(tags: GroupBase<Item<string>>[]): OpenListsS
7979
};
8080
}
8181

82-
export const updateTopicChoices = (topicSelections: Partial<Record<TAG_ID | TAG_LEVEL, Item<TAG_ID>[]>>[], pageContext?: PageContextState) => {
82+
export const updateTopicChoices = (
83+
topicSelections: Partial<Record<TAG_ID | TAG_LEVEL, Item<TAG_ID>[]>>[],
84+
pageContext?: PageContextState,
85+
allowedTags?: TAG_ID[]
86+
): ChoiceTree[] => {
8387
const subject = pageContext?.subject ? [tags.getById(pageContext?.subject as TAG_ID)] : tags.allSubjectTags;
8488
const choices: ChoiceTree[] = [ { subject: subject.map(itemiseTag) } ];
8589

@@ -98,5 +102,19 @@ export const updateTopicChoices = (topicSelections: Partial<Record<TAG_ID | TAG_
98102
pageContext.subject ? choices[1][pageContext?.subject]?.push(itemiseTag(tags.getById(tag))) : null
99103
);
100104
}
105+
if (allowedTags) {
106+
return choices.map(c => filterChoice(c, allowedTags));
107+
}
101108
return choices;
102109
};
110+
111+
function filterChoice(c: ChoiceTree, t: TAG_ID[]): ChoiceTree;
112+
function filterChoice(c: Item<TAG_ID>[], t: TAG_ID[]): Item<TAG_ID>[];
113+
function filterChoice(choice: ChoiceTree | Item<TAG_ID>[], allowedTags: TAG_ID[] ): ChoiceTree | Item<TAG_ID>[] {
114+
if (Array.isArray(choice)) {
115+
return choice.filter((tag => allowedTags.includes(tag.value)));
116+
}
117+
return Object.fromEntries(
118+
Object.entries(choice).map(([key, value]) => [key, filterChoice(value, allowedTags)])
119+
);
120+
};

src/test/services/questionSearch.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,37 @@ describe('updateTopicChoices', () => {
147147
});
148148
});
149149
});
150+
151+
describe('when a list of allowed tags is given', () => {
152+
const pageContext = { subject: undefined, stage: [] };
153+
154+
it('filters the subjects', () => {
155+
const allowedTags = [TAG_ID.physics, TAG_ID.chemistry, TAG_ID.bonding];
156+
const choices = updateTopicChoices([{}, {}, {}], pageContext, allowedTags);
157+
expect(choices).toEqual([subject(TAG_ID.physics, TAG_ID.chemistry)]);
158+
});
159+
160+
it('filters the fields', () => {
161+
const allowedTags = [TAG_ID.physics, TAG_ID.chemistry, TAG_ID.thermal];
162+
const choices = updateTopicChoices([subject(TAG_ID.physics), {}, {}], pageContext, allowedTags);
163+
expect(choices).toEqual([
164+
subject(TAG_ID.physics, TAG_ID.chemistry),
165+
physics(TAG_ID.thermal)
166+
]);
167+
});
168+
169+
it('filters the topics', () => {
170+
const allowedTags = [TAG_ID.physics, TAG_ID.chemistry, TAG_ID.thermal, TAG_ID.thermalRadiation];
171+
const choices = updateTopicChoices([
172+
subject(TAG_ID.physics), physics(TAG_ID.thermal), {}
173+
], pageContext, allowedTags);
174+
expect(choices).toEqual([
175+
subject(TAG_ID.physics, TAG_ID.chemistry),
176+
physics(TAG_ID.thermal),
177+
thermal(TAG_ID.thermalRadiation)
178+
]);
179+
});
180+
});
150181
});
151182

152183
const subject = (...choices: TAG_ID[]): ChoiceTree => ({ subject: choices.map(choice) });

0 commit comments

Comments
 (0)