From 53f85355352cd69eb2654ba1b4c94b9b12787844 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 27 Oct 2023 13:10:20 -0400 Subject: [PATCH] search pagination in web app (#183) --- CHANGELOG.md | 6 ++ playlet-web/src/lib/Api/InvidiousApi.ts | 3 +- .../src/lib/Screens/SearchScreen.svelte | 55 +++++++++++++++---- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78c51525..e58d2663 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 ### Added diff --git a/playlet-web/src/lib/Api/InvidiousApi.ts b/playlet-web/src/lib/Api/InvidiousApi.ts index 65df189d..4cc86d3c 100644 --- a/playlet-web/src/lib/Api/InvidiousApi.ts +++ b/playlet-web/src/lib/Api/InvidiousApi.ts @@ -27,7 +27,7 @@ export class InvidiousApi { 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)}®ion=${this.userCountryCode}`; for (let filter in filters) { if (typeof filters[filter] === 'string') { @@ -42,6 +42,7 @@ export class InvidiousApi { url += `&${filter}=${filters[filter].join(',')}`; } } + url += `&page=${page}`; const response = await fetch(url); return await response.json(); } diff --git a/playlet-web/src/lib/Screens/SearchScreen.svelte b/playlet-web/src/lib/Screens/SearchScreen.svelte index 67301612..e186266f 100644 --- a/playlet-web/src/lib/Screens/SearchScreen.svelte +++ b/playlet-web/src/lib/Screens/SearchScreen.svelte @@ -16,6 +16,7 @@ let searchBox; let searchBoxText = ""; let suggestions: { suggestions: any[] } = { suggestions: [] }; + let page = 1; let videos = []; let searchHistory = []; let isLoading = false; @@ -75,26 +76,33 @@ async function suggestionClicked(query) { searchBoxText = query; + page = 1; + videos = []; await searchVideos(); } async function searchVideos() { suggestions = { suggestions: [] }; - videos = []; if (searchBoxText.length === 0) { return; } try { isLoading = true; - // TODO:P1: search pagination - load more results as we scroll - videos = await invidiousApi.search(searchBoxText, searchFilters); + const newVideos = await invidiousApi.search( + searchBoxText, + searchFilters, + page + ); + videos = [...videos, ...newVideos]; } finally { isLoading = false; } - const newSearchHistory = await PlayletApi.putSearchHistory(searchBoxText); - searchHistoryStore.set(newSearchHistory); + if (page === 1) { + const newSearchHistory = await PlayletApi.putSearchHistory(searchBoxText); + searchHistoryStore.set(newSearchHistory); + } } @@ -103,6 +111,8 @@
{ e.preventDefault(); + page = 1; + videos = []; await searchVideos(); }} > @@ -124,7 +134,14 @@ }, 200); }} /> - + + {/if} + + {#if isLoading} +
+ +
+ {/if}