search pagination in web app (#183)

This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-10-27 13:10:20 -04:00
1 parent 982dd0cd2e
commit 53f8535535
3 files changed
+49 -11

No files matched your search

+6
View File
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Pagination support in web app search (load more button)
## [0.14.0] - 2023-10-25 ## [0.14.0] - 2023-10-25
### Added ### Added
+2 -1
View File
@@ -27,7 +27,7 @@ export class InvidiousApi {
return await response.json(); return await response.json();
} }
public async search(query: string, filters: any) { public async search(query: string, filters: any, page: number = 1) {
let url = `${this.instance}/api/v1/search?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`; let url = `${this.instance}/api/v1/search?q=${encodeURIComponent(query)}&region=${this.userCountryCode}`;
for (let filter in filters) { for (let filter in filters) {
if (typeof filters[filter] === 'string') { if (typeof filters[filter] === 'string') {
@@ -42,6 +42,7 @@ export class InvidiousApi {
url += `&${filter}=${filters[filter].join(',')}`; url += `&${filter}=${filters[filter].join(',')}`;
} }
} }
url += `&page=${page}`;
const response = await fetch(url); const response = await fetch(url);
return await response.json(); return await response.json();
} }
+41 -10
View File
@@ -16,6 +16,7 @@
let searchBox; let searchBox;
let searchBoxText = ""; let searchBoxText = "";
let suggestions: { suggestions: any[] } = { suggestions: [] }; let suggestions: { suggestions: any[] } = { suggestions: [] };
let page = 1;
let videos = []; let videos = [];
let searchHistory = []; let searchHistory = [];
let isLoading = false; let isLoading = false;
@@ -75,27 +76,34 @@
async function suggestionClicked(query) { async function suggestionClicked(query) {
searchBoxText = query; searchBoxText = query;
page = 1;
videos = [];
await searchVideos(); await searchVideos();
} }
async function searchVideos() { async function searchVideos() {
suggestions = { suggestions: [] }; suggestions = { suggestions: [] };
videos = [];
if (searchBoxText.length === 0) { if (searchBoxText.length === 0) {
return; return;
} }
try { try {
isLoading = true; isLoading = true;
// TODO:P1: search pagination - load more results as we scroll const newVideos = await invidiousApi.search(
videos = await invidiousApi.search(searchBoxText, searchFilters); searchBoxText,
searchFilters,
page
);
videos = [...videos, ...newVideos];
} finally { } finally {
isLoading = false; isLoading = false;
} }
if (page === 1) {
const newSearchHistory = await PlayletApi.putSearchHistory(searchBoxText); const newSearchHistory = await PlayletApi.putSearchHistory(searchBoxText);
searchHistoryStore.set(newSearchHistory); searchHistoryStore.set(newSearchHistory);
} }
}
</script> </script>
<div class={visibility ? "" : "hidden"}> <div class={visibility ? "" : "hidden"}>
@@ -103,6 +111,8 @@
<form <form
on:submit={async (e) => { on:submit={async (e) => {
e.preventDefault(); e.preventDefault();
page = 1;
videos = [];
await searchVideos(); await searchVideos();
}} }}
> >
@@ -124,7 +134,14 @@
}, 200); }, 200);
}} }}
/> />
<button class="join-item btn" on:click={searchVideos}> <button
class="join-item btn"
on:click={async () => {
page = 1;
videos = [];
await searchVideos();
}}
>
<div class="h-6"> <div class="h-6">
<SearchThinIcon /> <SearchThinIcon />
</div> </div>
@@ -160,12 +177,6 @@
</div> </div>
<div> <div>
{#if isLoading}
<div class="fixed w-full h-1/2 z-50 flex justify-center items-center">
<span class="loading loading-spinner loading-md" />
</div>
{/if}
<div <div
class="grid grid-flow-row-dense gap-4 items-center justify-center sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-3 m-4" class="grid grid-flow-row-dense gap-4 items-center justify-center sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-3 m-4"
> >
@@ -179,6 +190,26 @@
{/if} {/if}
{/each} {/each}
</div> </div>
{#if !isLoading && videos.length > 0 && searchBoxText}
<div class="flex justify-center items-center">
<button
class="btn w-1/2"
on:click={async () => {
page++;
await searchVideos();
}}
>
Load more
</button>
</div>
{/if}
{#if isLoading}
<div class="w-full h-1/2 z-50 flex justify-center items-center">
<span class="loading loading-spinner loading-md" />
</div>
{/if}
</div> </div>
<SearchFilters <SearchFilters