add report pagination
All checks were successful
delpoy / build-and-deploy (push) Successful in 40s

This commit is contained in:
bytedream 2024-01-10 23:41:51 +01:00
parent f74f1fe19e
commit 6d9f3c41aa
3 changed files with 29 additions and 10 deletions

View File

@ -1,18 +1,15 @@
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import { env } from '$env/dynamic/public'; import { env } from '$env/dynamic/public';
import { getSession } from '$lib/server/session';
import { Permissions } from '$lib/permissions';
import { StrikeReason } from '$lib/server/database'; import { StrikeReason } from '$lib/server/database';
export const load: PageServerLoad = async ({ parent, cookies }) => { export const load: PageServerLoad = async ({ parent }) => {
const { reportCount } = await parent(); const { reportCount } = await parent();
if (reportCount == null) throw redirect(302, `${env.PUBLIC_BASE_PATH}/admin`); if (reportCount == null) throw redirect(302, `${env.PUBLIC_BASE_PATH}/admin`);
const { self } = await parent(); const { self } = await parent();
return { return {
count: getSession(cookies, { permissions: [Permissions.UserRead] }) != null ? reportCount : 0,
strike_reasons: JSON.parse(JSON.stringify(await StrikeReason.findAll())), strike_reasons: JSON.parse(JSON.stringify(await StrikeReason.findAll())),
self: self self: self
}; };

View File

@ -20,6 +20,7 @@
export let data: PageData; export let data: PageData;
let currentPageReports: (typeof Report.prototype.dataValues)[] = []; let currentPageReports: (typeof Report.prototype.dataValues)[] = [];
let currentPageTotal = 0;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
let currentPageReportsRequest: Promise<any> = Promise.resolve(); let currentPageReportsRequest: Promise<any> = Promise.resolve();
let reportsPerPage = 50; let reportsPerPage = 50;
@ -30,12 +31,16 @@
async function fetchPageReports( async function fetchPageReports(
page: number, page: number,
filter: typeof reportFilter | { hash: string } filter: typeof reportFilter | { hash: string }
): Promise<typeof currentPageReports> { ): Promise<{ reports: typeof currentPageReports; count: number }> {
if (!browser) return []; if (!browser) return { reports: [], count: 0 };
const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/reports`, { const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/reports`, {
method: 'POST', method: 'POST',
body: JSON.stringify({ ...filter, limit: reportsPerPage, from: reportPage * page }) body: JSON.stringify({
...filter,
limit: reportsPerPage,
from: reportsPerPage * page
})
}); });
if (activeReport) { if (activeReport) {
@ -47,7 +52,8 @@
} }
$: currentPageReportsRequest = fetchPageReports(reportPage, reportFilter).then((r) => { $: currentPageReportsRequest = fetchPageReports(reportPage, reportFilter).then((r) => {
currentPageReports = r; currentPageReports = r.reports;
currentPageTotal = r.count;
}); });
async function openHashReport() { async function openHashReport() {
@ -56,7 +62,7 @@
const requestedHash = window.location.hash.substring(1); const requestedHash = window.location.hash.substring(1);
let report = currentPageReports.find((r) => r.url_hash === requestedHash); let report = currentPageReports.find((r) => r.url_hash === requestedHash);
if (!report) { if (!report) {
const hashReport = (await fetchPageReports(0, { hash: requestedHash }))[0]; const hashReport = (await fetchPageReports(0, { hash: requestedHash })).reports[0];
if (hashReport) { if (hashReport) {
currentPageReports = [hashReport, ...currentPageReports]; currentPageReports = [hashReport, ...currentPageReports];
report = hashReport; report = hashReport;
@ -191,6 +197,20 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div class="flex justify-center items-center mb-2 mt-4 w-full">
<div class="join">
<!-- eslint-disable-next-line @typescript-eslint/no-unused-vars -->
{#each Array(currentPageReports.length === reportsPerPage || reportPage > 0 ? Math.ceil(currentPageTotal / reportsPerPage) || 1 : 1) as _, i}
<button
class="join-item btn"
class:btn-active={i === reportPage}
on:click={() => {
reportPage = i;
}}>{i + 1}</button
>
{/each}
</div>
</div>
</div> </div>
{#if activeReport} {#if activeReport}
<div <div

View File

@ -85,7 +85,9 @@ export const POST = (async ({ request, cookies }) => {
return r; return r;
}); });
return new Response(JSON.stringify(reports)); return new Response(
JSON.stringify({ reports: reports, count: await Report.count({ where: reportFindOptions }) })
);
}) satisfies RequestHandler; }) satisfies RequestHandler;
export const PATCH = (async ({ request, cookies }) => { export const PATCH = (async ({ request, cookies }) => {