Add a load more button for search results (#16).
This commit is contained in:
parent
cc98bd87f9
commit
279f40004f
|
@ -24,6 +24,23 @@ export default class SearchBar extends Component<Props, State> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
searchMore = async (): Promise<void> => {
|
||||||
|
if (this.state.searchValue.length === 0) {
|
||||||
|
this.setState({searchResults: [], searchState: 'waiting'});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
searchResults: [
|
||||||
|
...this.state.searchResults,
|
||||||
|
...(await searchReleases(
|
||||||
|
this.state.searchValue,
|
||||||
|
this.state.searchResults.length,
|
||||||
|
)),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
searchQuery = async (): Promise<void> => {
|
searchQuery = async (): Promise<void> => {
|
||||||
if (this.state.searchValue.length === 0) {
|
if (this.state.searchValue.length === 0) {
|
||||||
this.setState({searchResults: [], searchState: 'waiting'});
|
this.setState({searchResults: [], searchState: 'waiting'});
|
||||||
|
@ -79,6 +96,17 @@ export default class SearchBar extends Component<Props, State> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resultAmount = this.state.searchResults.length;
|
||||||
|
if (resultAmount > 0 && resultAmount % 25 === 0) {
|
||||||
|
results.push(
|
||||||
|
html`
|
||||||
|
<li class="search-state">
|
||||||
|
<button onClick=${this.searchMore}>Load more…</button>
|
||||||
|
</li>
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="search-bar">
|
<div class="search-bar">
|
||||||
<input
|
<input
|
||||||
|
|
|
@ -19,9 +19,14 @@ export type SearchResult = {
|
||||||
|
|
||||||
export default async function searchReleases(
|
export default async function searchReleases(
|
||||||
query: string,
|
query: string,
|
||||||
|
offset?: number,
|
||||||
): Promise<SearchResult[]> {
|
): Promise<SearchResult[]> {
|
||||||
query = encodeURIComponent(query);
|
query = encodeURIComponent(query);
|
||||||
const url = `https://musicbrainz.org/ws/2/release?query=${query}`;
|
let url = `https://musicbrainz.org/ws/2/release?query=${query}`;
|
||||||
|
if (offset !== undefined) {
|
||||||
|
url += `&offset=${offset}`;
|
||||||
|
}
|
||||||
|
|
||||||
const response = await window.fetch(url, {
|
const response = await window.fetch(url, {
|
||||||
headers: {
|
headers: {
|
||||||
accept: 'application/json',
|
accept: 'application/json',
|
||||||
|
|
Loading…
Reference in New Issue