Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication: Add FAQ search bar #9423

Merged
merged 13 commits into from
Oct 12, 2024
Merged
12 changes: 12 additions & 0 deletions src/main/webapp/app/faq/faq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ export class FaqService {
return categories.some((category) => filteredCategory.has(category!));
}
}

hasSearchTokens(faq: Faq, searchTerm: string) {
if (searchTerm == '') {
return true;
}
const tokens = searchTerm.split(' ');
if (tokens) {
let faqText = faq.questionTitle + ' ' + faq.questionAnswer;
faqText = faqText.toLowerCase();
return tokens.every((token) => faqText.includes(token.toLowerCase()));
}
}
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div>
<div class="input-group mb-2 rounded-3 p-2 me-2 module-bg" style="display: flex; justify-content: end">
<div class="me-2" aria-label="Filter Dropdown" ngbDropdown>
<div class="input-group mb-2 rounded-3 p-2 me-2 module-bg d-flex justify-content-between">
<jhi-search-filter class="flex-grow-1" (newSearchEvent)="setSearchValue($event)" />
cremertim marked this conversation as resolved.
Show resolved Hide resolved
<div class="ms-2 me-2" aria-label="Filter Dropdown" ngbDropdown>
cremertim marked this conversation as resolved.
Show resolved Hide resolved
<button class="btn" [ngClass]="{ 'btn-secondary': activeFilters.size === 0, 'btn-success': activeFilters.size > 0 }" ngbDropdownToggle id="filter-dropdown-button">
<fa-icon [icon]="faFilter" />
<span class="d-s-none d-md-inline" jhiTranslate="artemisApp.courseOverview.exerciseList.filter" [translateValues]="{ num: activeFilters.size }"></span>
Expand Down
31 changes: 25 additions & 6 deletions src/main/webapp/app/overview/course-faq/course-faq.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Component, OnDestroy, OnInit, ViewEncapsulation, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs/operators';
import { Subject, Subscription } from 'rxjs';
import { debounceTime, map } from 'rxjs/operators';
import { BehaviorSubject, Subject, Subscription } from 'rxjs';
import { faFilter } from '@fortawesome/free-solid-svg-icons';
import { ButtonType } from 'app/shared/components/button.component';
import { SidebarData } from 'app/types/sidebar';
import { ArtemisSharedComponentModule } from 'app/shared/components/shared-component.module';
import { ArtemisSharedModule } from 'app/shared/shared.module';
import { CourseFaqAccordionComponent } from 'app/overview/course-faq/course-faq-accordion-component';
Expand All @@ -16,14 +15,15 @@ import { FaqCategory } from 'app/entities/faq-category.model';
import { loadCourseFaqCategories } from 'app/faq/faq.utils';
import { CustomExerciseCategoryBadgeComponent } from 'app/shared/exercise-categories/custom-exercise-category-badge/custom-exercise-category-badge.component';
import { onError } from 'app/shared/util/global.utils';
import { SearchFilterComponent } from 'app/shared/search-filter/search-filter.component';

@Component({
selector: 'jhi-course-faq',
templateUrl: './course-faq.component.html',
styleUrls: ['../course-overview.scss', 'course-faq.component.scss'],
encapsulation: ViewEncapsulation.None,
standalone: true,
imports: [ArtemisSharedComponentModule, ArtemisSharedModule, CourseFaqAccordionComponent, CustomExerciseCategoryBadgeComponent],
imports: [ArtemisSharedComponentModule, ArtemisSharedModule, CourseFaqAccordionComponent, CustomExerciseCategoryBadgeComponent, SearchFilterComponent],
})
export class CourseFaqComponent implements OnInit, OnDestroy {
private ngUnsubscribe = new Subject<void>();
Expand All @@ -36,10 +36,11 @@ export class CourseFaqComponent implements OnInit, OnDestroy {
existingCategories: FaqCategory[];
activeFilters = new Set<string>();

sidebarData: SidebarData;
hasCategories = false;
isCollapsed = false;

searchInput = new BehaviorSubject<string>('');

readonly ButtonType = ButtonType;

// Icons
Expand All @@ -56,6 +57,9 @@ export class CourseFaqComponent implements OnInit, OnDestroy {
this.loadFaqs();
this.loadCourseExerciseCategories(this.courseId);
});
this.searchInput.pipe(debounceTime(500)).subscribe((searchTerm: string) => {
this.defineSearchedAndFilteredFaq(searchTerm);
});
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}

private loadCourseExerciseCategories(courseId: number) {
Expand Down Expand Up @@ -86,10 +90,25 @@ export class CourseFaqComponent implements OnInit, OnDestroy {

toggleFilters(category: string) {
this.activeFilters = this.faqService.toggleFilter(category, this.activeFilters);
this.applyFilters();
this.defineSearchedAndFilteredFaq(this.searchInput.getValue());
}

private applyFilters(): void {
this.filteredFaqs = this.faqService.applyFilters(this.activeFilters, this.faqs);
}

private applySearch(searchTerm: string) {
this.filteredFaqs = this.filteredFaqs.filter((faq) => {
return this.faqService.hasSearchTokens(faq, searchTerm);
});
}

setSearchValue(searchValue: string) {
this.searchInput.next(searchValue);
}

defineSearchedAndFilteredFaq(searchTerm: string) {
this.applyFilters();
this.applySearch(searchTerm);
}
cremertim marked this conversation as resolved.
Show resolved Hide resolved
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Component, EventEmitter, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { faMagnifyingGlass, faTimes } from '@fortawesome/free-solid-svg-icons';
import { ArtemisSharedModule } from 'app/shared/shared.module';

@Component({
selector: 'jhi-search-filter',
templateUrl: './search-filter.component.html',
styleUrls: ['./search-filter.component.scss'],
standalone: true,
imports: [ArtemisSharedModule],
cremertim marked this conversation as resolved.
Show resolved Hide resolved
})
export class SearchFilterComponent {
faMagnifyingGlass = faMagnifyingGlass;
Expand Down
3 changes: 0 additions & 3 deletions src/main/webapp/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { StickyPopoverDirective } from 'app/shared/sticky-popover/sticky-popover
import { ConfirmEntityNameComponent } from 'app/shared/confirm-entity-name/confirm-entity-name.component';
import { DetailOverviewNavigationBarComponent } from 'app/shared/detail-overview-navigation-bar/detail-overview-navigation-bar.component';
import { ScienceDirective } from 'app/shared/science/science.directive';
import { SearchFilterComponent } from './search-filter/search-filter.component';

@NgModule({
imports: [ArtemisSharedLibsModule, ArtemisSharedCommonModule, ArtemisSharedPipesModule, RouterModule],
Expand Down Expand Up @@ -56,7 +55,6 @@ import { SearchFilterComponent } from './search-filter/search-filter.component';
AssessmentWarningComponent,
StickyPopoverDirective,
ScienceDirective,
SearchFilterComponent,
],
exports: [
ArtemisSharedLibsModule,
Expand Down Expand Up @@ -87,7 +85,6 @@ import { SearchFilterComponent } from './search-filter/search-filter.component';
CompetencySelectionComponent,
StickyPopoverDirective,
ScienceDirective,
SearchFilterComponent,
],
})
export class ArtemisSharedModule {}
2 changes: 2 additions & 0 deletions src/main/webapp/app/shared/sidebar/sidebar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SidebarCardDirective } from 'app/shared/sidebar/sidebar-card.directive'
import { ConversationOptionsComponent } from 'app/shared/sidebar/conversation-options/conversation-options.component';
import { AccordionAddOptionsComponent } from 'app/shared/sidebar/accordion-add-options/accordion-add-options.component';
import { ArtemisExamSharedModule } from 'app/exam/shared/exam-shared.module';
import { SearchFilterComponent } from 'app/shared/search-filter/search-filter.component';

@NgModule({
imports: [
Expand All @@ -28,6 +29,7 @@ import { ArtemisExamSharedModule } from 'app/exam/shared/exam-shared.module';
SubmissionResultStatusModule,
SidebarCardDirective,
ArtemisExamSharedModule,
SearchFilterComponent,
],
declarations: [
SidebarAccordionComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { FaqCategory } from 'app/entities/faq-category.model';
function createFaq(id: number, category: string, color: string): Faq {
const faq = new Faq();
faq.id = id;
faq.questionTitle = 'questionTitle';
faq.questionAnswer = 'questionAnswer';
faq.questionTitle = 'questionTitle ' + id;
faq.questionAnswer = 'questionAnswer ' + id;
faq.categories = [new FaqCategory(category, color)];
return faq;
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -84,6 +84,9 @@ describe('CourseFaqs', () => {
applyFilters: () => {
return [faq2, faq3];
},
hasSearchTokens: () => {
return true;
},
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}),
],
})
Expand Down Expand Up @@ -125,6 +128,20 @@ describe('CourseFaqs', () => {
expect(courseFaqComponent.filteredFaqs).toEqual([faq2, faq3]);
});

it('should search through already filtered array', () => {
const searchSpy = jest.spyOn(faqService, 'hasSearchTokens');
const applyFilterSpy = jest.spyOn(faqService, 'applyFilters');
courseFaqComponent.setSearchValue('questionTitle');
courseFaqComponent.defineSearchedAndFilteredFaq(courseFaqComponent.searchInput.getValue());
expect(applyFilterSpy).toHaveBeenCalledOnce();
expect(searchSpy).toHaveBeenCalledTimes(2);
expect(searchSpy).toHaveBeenCalledWith(faq2, 'questionTitle');
expect(searchSpy).toHaveBeenCalledWith(faq3, 'questionTitle');
expect(courseFaqComponent.filteredFaqs).toHaveLength(2);
expect(courseFaqComponent.filteredFaqs).not.toContain(faq1);
expect(courseFaqComponent.filteredFaqs).toEqual([faq2, faq3]);
});

it('should catch error if no categories are found', () => {
alertServiceStub = jest.spyOn(alertService, 'error');
const error = { status: 404 };
Expand Down
9 changes: 9 additions & 0 deletions src/test/javascript/spec/service/faq.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,14 @@ describe('Faq Service', () => {
const convertedCategory = FaqService.stringifyFaqCategories(faq2);
expect(convertedCategory).toEqual(['{"color":"red","category":"testing"}']);
});

it('should return if all tokens exist in FAQ title or answer', () => {
const faq1 = new Faq();
faq1.questionTitle = 'Title';
faq1.questionAnswer = 'Answer';

expect(service.hasSearchTokens(faq1, 'title answer')).toBeTrue();
expect(service.hasSearchTokens(faq1, 'title answer missing')).toBeFalse();
});
cremertim marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading