Skip to content

Commit

Permalink
Added debounce, fixed error handling for search results, fixed bug in…
Browse files Browse the repository at this point in the history
… paginated lists
  • Loading branch information
LotteHofstede committed Mar 19, 2019
1 parent f160196 commit 6d64326
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/app/+search-page/search-service/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DSpaceObject } from '../../core/shared/dspace-object.model';
import { GenericConstructor } from '../../core/shared/generic-constructor';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
import {
configureRequest,
configureRequest, filterSuccessfulResponses,
getResponseFromEntry,
getSucceededRemoteData
} from '../../core/shared/operators';
Expand Down Expand Up @@ -104,7 +104,7 @@ export class SearchService implements OnDestroy {

// get search results from response cache
const sqrObs: Observable<SearchQueryResponse> = requestEntryObs.pipe(
getResponseFromEntry(),
filterSuccessfulResponses(),
map((response: SearchSuccessResponse) => response.results)
);

Expand Down
5 changes: 4 additions & 1 deletion src/app/core/data/paginated-list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PageInfo } from '../shared/page-info.model';
import { hasValue } from '../../shared/empty.util';
import { hasNoValue, hasValue } from '../../shared/empty.util';

export class PaginatedList<T> {

Expand All @@ -22,6 +22,9 @@ export class PaginatedList<T> {
if (hasValue(this.pageInfo) && hasValue(this.pageInfo.totalElements)) {
return this.pageInfo.totalElements;
}
if (hasNoValue(this.page)) {
return 0;
}
return this.page.length;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
import { DSOSelectorComponent } from './dso-selector.component';
Expand All @@ -21,7 +21,12 @@ describe('DSOSelectorComponent', () => {
const type = DSpaceObjectType.ITEM;
const searchResult = new ItemSearchResult();
const item = new Item();
item.metadata = { 'dc.title': [Object.assign(new MetadataValue(), { value: 'Item title', language: undefined })] };
item.metadata = {
'dc.title': [Object.assign(new MetadataValue(), {
value: 'Item title',
language: undefined
})]
};
searchResult.dspaceObject = item;
searchResult.hitHighlights = {};
const searchService = jasmine.createSpyObj('searchService', {
Expand All @@ -46,6 +51,7 @@ describe('DSOSelectorComponent', () => {
debugElement = fixture.debugElement;
component.currentDSOId = currentDSOId;
component.type = type;

fixture.detectChanges();
});

Expand All @@ -59,7 +65,9 @@ describe('DSOSelectorComponent', () => {
dsoType: type,
pagination: (component as any).defaultPagination
});

expect(searchService.search).toHaveBeenCalledWith(searchOptions);
});

});
})
;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Expand All @@ -12,7 +11,7 @@ import {
import { FormControl } from '@angular/forms';

import { Observable } from 'rxjs';
import { map, startWith, switchMap, take } from 'rxjs/operators';
import { debounceTime, startWith, switchMap } from 'rxjs/operators';
import { SearchService } from '../../../+search-page/search-service/search.service';
import { PaginatedSearchOptions } from '../../../+search-page/paginated-search-options.model';
import { DSpaceObjectType } from '../../../core/shared/dspace-object-type.model';
Expand Down Expand Up @@ -68,6 +67,11 @@ export class DSOSelectorComponent implements OnInit {
*/
@ViewChildren('listEntryElement') listElements: QueryList<ElementRef>;

/**
* Time to wait before sending a search request to the server when a user types something
*/
debounceTime = 500;

constructor(private searchService: SearchService) {
}

Expand All @@ -79,6 +83,7 @@ export class DSOSelectorComponent implements OnInit {
this.input.setValue(this.currentDSOId);
this.listEntries$ = this.input.valueChanges
.pipe(
debounceTime(this.debounceTime),
startWith(this.currentDSOId),
switchMap((query) => {
return this.searchService.search(
Expand Down

0 comments on commit 6d64326

Please sign in to comment.