a mini social media app for small communities

basic pagination (untested)

Changed files
+38 -8
src
static
templates
webapp
-2
src/static/js/search.js
··· 2 const data = await fetch(`/api/search?query=${query}&limit=${limit}&offset=${offset}`, { 3 method: 'GET' 4 }) 5 - console.log(data) 6 const json = await data.json() 7 - console.log(json) 8 return json 9 }
··· 2 const data = await fetch(`/api/search?query=${query}&limit=${limit}&offset=${offset}`, { 3 method: 'GET' 4 }) 5 const json = await data.json() 6 return json 7 }
+37 -5
src/templates/search.html
··· 12 13 <br> 14 15 <div id="results"> 16 </div> 17 18 <script> 19 - const query = document.getElementById("query") 20 - const results = document.getElementById("results") 21 22 - document.getElementById("search").addEventListener("click", async () => { 23 results.innerHTML = '' // yeet the children! 24 - const search_results = await search(query.value, 10, 0) 25 if (search_results.length >= 0) { 26 for (result of search_results) { 27 // same as components/post_mini.html except js ··· 46 element.appendChild(p) 47 results.appendChild(element) 48 } 49 } else { 50 - results.innerText = 'No results!' 51 } 52 }) 53 </script>
··· 12 13 <br> 14 15 + <div id="pages"> 16 + </div> 17 + 18 <div id="results"> 19 </div> 20 21 <script> 22 + const params = new URLSearchParams(window.location.search) 23 + 24 + const pages = document.getElementById('pages') 25 + const results = document.getElementById('results') 26 + 27 + const query = document.getElementById('query') 28 + if (query.value == '' && params.get('q')) { 29 + query.value = params.get('q') 30 + } 31 32 + let offset = params.get('offset') 33 + if (!offset) { 34 + offset = 0 35 + } 36 + 37 + document.getElementById('search').addEventListener('click', async () => { 38 results.innerHTML = '' // yeet the children! 39 + pages.innerHTML = '' // yeet more children! 40 + 41 + const search_results = await search(query.value, 10, Number(offset.value)) 42 if (search_results.length >= 0) { 43 for (result of search_results) { 44 // same as components/post_mini.html except js ··· 63 element.appendChild(p) 64 results.appendChild(element) 65 } 66 + 67 + // set up pagination 68 + if (offset > 0) { 69 + const back_link = document.createElement('a') 70 + // we escape the $ here because otherwise V will try to perform replacements at compile-time. 71 + //todo: report this, this behaviour should be changed or at least looked into further. 72 + back_link.href = '/search?q=' + query.value + '&limit=10&offset=' + Math.min(0, offset.value - 10) 73 + back_link.innerText = '<-' 74 + pages.appendChild(back_link) 75 + 76 + const next_link = document.createElement('a') 77 + next_link.href = '/search?q=' + query.value + '&limit=10&offset=' + (offset.value + 10) 78 + next_link.innerText = '->' 79 + pages.appendChild(next_link) 80 + } 81 } else { 82 + results.innerText = 'no results!' 83 } 84 }) 85 </script>
+1 -1
src/webapp/pages.v
··· 175 } 176 177 @['/search'] 178 - fn (mut app App) search(mut ctx Context) veb.Result { 179 user := app.whoami(mut ctx) or { 180 ctx.error('not logged in') 181 return ctx.redirect('/login')
··· 175 } 176 177 @['/search'] 178 + fn (mut app App) search(mut ctx Context, q string, offset int) veb.Result { 179 user := app.whoami(mut ctx) or { 180 ctx.error('not logged in') 181 return ctx.redirect('/login')