Merge pull request #3429 from sanderhahn/fix-offset

Fix offset and next page link
pull/3432/head
Rich Harris 5 years ago committed by GitHub
commit 51498421bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,13 +3,14 @@ import { query } from '../../utils/db';
export async function get(req, res) { export async function get(req, res) {
if (req.user) { if (req.user) {
const offset = req.params.offset || 0; const page_size = 100;
const offset = req.query.offset ? parseInt(req.query.offset) : 0;
const rows = await query(` const rows = await query(`
select g.uid, g.name, coalesce(g.updated_at, g.created_at) as updated_at select g.uid, g.name, coalesce(g.updated_at, g.created_at) as updated_at
from gists g from gists g
where g.user_id = $1 where g.user_id = $1
order by updated_at desc order by id desc
limit 100 limit ${page_size + 1}
offset $2 offset $2
`, [req.user.id, offset]); `, [req.user.id, offset]);
@ -17,7 +18,8 @@ export async function get(req, res) {
row.uid = row.uid.replace(/-/g, ''); row.uid = row.uid.replace(/-/g, '');
}); });
send(res, 200, rows); const more = rows.length > page_size;
send(res, 200, { apps: rows.slice(0, page_size), offset: more ? offset + page_size : null });
} else { } else {
send(res, 401); send(res, 401);
} }

@ -1,17 +1,22 @@
<script context="module"> <script context="module">
export async function preload(page, { user }) { export async function preload(page, { user }) {
let apps; let apps = [];
let offset = null;
if (user) { if (user) {
const r = await this.fetch('apps.json', { var url = 'apps.json';
if (page.query.offset) {
url += `?offset=${encodeURIComponent(page.query.offset)}`;
}
const r = await this.fetch(url, {
credentials: 'include' credentials: 'include'
}); });
if (!r.ok) return this.error(r.status, await r.text()); if (!r.ok) return this.error(r.status, await r.text());
apps = await r.json(); ({ apps, offset } = await r.json());
} }
return { user, apps }; return { user, apps, offset };
} }
</script> </script>
@ -20,6 +25,7 @@
export let user; export let user;
export let apps; export let apps;
export let offset;
const { login, logout } = getContext('app'); const { login, logout } = getContext('app');
@ -62,6 +68,10 @@
</li> </li>
{/each} {/each}
</ul> </ul>
{#if offset !== null}
<div><a href="apps?offset={offset}">Next page...</a></div>
{/if}
{:else} {:else}
<p>Please <a on:click|preventDefault={login} href="auth/login">log in</a> to see your saved apps.</p> <p>Please <a on:click|preventDefault={login} href="auth/login">log in</a> to see your saved apps.</p>
{/if} {/if}

Loading…
Cancel
Save