1
Fork 0

Add a load more button for search results (#16).

This commit is contained in:
Bauke 2022-02-03 12:23:10 +01:00
parent cc98bd87f9
commit 279f40004f
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 34 additions and 1 deletions

View File

@ -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

View File

@ -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',