Last active
January 21, 2020 21:53
-
-
Save armorpreston/87bba8d541dd7d9b06091ec13f7cc2cc to your computer and use it in GitHub Desktop.
Wikipedia Http Type Ahead
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
<http-type-ahead [service]="wikipediaService"></http-type-ahead> |
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
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpParams } from '@angular/common/http'; | |
import { of } from 'rxjs'; | |
import { map } from 'rxjs/operators'; | |
import { AbstractHttpTypeAheadService } from '../../../projects/brandkit/src/lib/components/type-ahead/abstract-http-type-ahead.service'; | |
@Injectable() | |
export class WikipediaHttpTypeAheadService extends AbstractHttpTypeAheadService<string> { | |
constructor(private http: HttpClient) { | |
super(); | |
} | |
public url = 'https://en.wikipedia.org/w/api.php'; | |
public params = new HttpParams({ | |
fromObject: { | |
action: 'opensearch', | |
format: 'json', | |
origin: '*' | |
} | |
}); | |
public placeholder = 'Search Wikipedia'; | |
public search = (term: string) => { | |
if (term === '') { return of([]); } | |
return this.http | |
.get(this.url, {params: this.params.set('search', term)}).pipe( | |
map((response) => response[1]) | |
); | |
} | |
public remove(toRemove: string): void { | |
this.selected = this.selected.filter((selectedItem) => { | |
return selectedItem !== toRemove; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment