Skip to content

Instantly share code, notes, and snippets.

@armorpreston
Last active January 21, 2020 21:53
Show Gist options
  • Save armorpreston/87bba8d541dd7d9b06091ec13f7cc2cc to your computer and use it in GitHub Desktop.
Save armorpreston/87bba8d541dd7d9b06091ec13f7cc2cc to your computer and use it in GitHub Desktop.
Wikipedia Http Type Ahead
<http-type-ahead [service]="wikipediaService"></http-type-ahead>
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