Skip to content

Commit

Permalink
feat: pagination on search (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
camarm-dev committed Jul 13, 2024
1 parent 41dbd8f commit 3fcd134
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/src/functions/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class RemedeDatabase {
return await this.query(statement)
}

async search(query: string) {
const statement = `SELECT word FROM wordlist WHERE indexed LIKE '${query}%' ORDER BY word ASC`
async search(query: string, page: number = 0) {
const statement = `SELECT word FROM wordlist WHERE indexed LIKE '${query}%' ORDER BY word ASC LIMIT 50 OFFSET ${page * 50}`
return await this.query(statement)
}

Expand Down
14 changes: 7 additions & 7 deletions app/src/functions/dictionnary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ async function getAutocompleteFromDatabase(word: string) {
return await database?.getAutocomplete(removeAccents(word)) as any[]
}

async function getSearchResultsWithAPI(query: string) {
return await fetch(`https://api-remede.camarm.fr/search/${query}`).then(resp => resp.json())
async function getSearchResultsWithAPI(query: string, page: number) {
return await fetch(`https://api-remede.camarm.fr/search/${query}?page=${page}`).then(resp => resp.json())
}

async function getSearchResultsFromDatabase(query: string) {
return await database?.search(removeAccents(query)) as any[]
async function getSearchResultsFromDatabase(query: string, page: number) {
return await database?.search(removeAccents(query), page) as any[]
}

async function getWordWithAPI(word: string) {
Expand Down Expand Up @@ -62,11 +62,11 @@ async function getAutocomplete(word: string) {
}


async function getSearchResults(query: string) {
async function getSearchResults(query: string, page: number) {
if (await useApi()) {
return await getSearchResultsWithAPI(query)
return await getSearchResultsWithAPI(query, page)
}
return await getSearchResultsFromDatabase(query)
return await getSearchResultsFromDatabase(query, page)
}

async function getWordDocument(word: string) {
Expand Down

0 comments on commit 3fcd134

Please sign in to comment.