add report selection via url hash
All checks were successful
delpoy / build-and-deploy (push) Successful in 48s

This commit is contained in:
bytedream 2023-10-05 13:07:30 +02:00
parent 444631f649
commit 1f8cf66e90
3 changed files with 71 additions and 9 deletions

View File

@ -22,8 +22,6 @@
export let data: LayoutData;
if (data.reportCount) $reportCount = data.reportCount;
if (data.adminCount) $adminCount = data.adminCount;
errorMessage.subscribe(console.log);
</script>
{#if $page.url.pathname !== `${env.PUBLIC_BASE_PATH}/admin/login`}

View File

@ -11,10 +11,13 @@
import HeaderBar from './HeaderBar.svelte';
import { IconOutline } from 'svelte-heros-v2';
import NewReportModal from './NewReportModal.svelte';
import { onDestroy, onMount } from 'svelte';
import { goto } from '$app/navigation';
export let data: PageData;
let currentPageReports: (typeof Report.prototype.dataValues)[] = [];
let currentPageReportsRequest: Promise<any> = Promise.resolve();
let reportsPerPage = 50;
let reportPage = 0;
let reportFilter = { draft: false, status: null, reporter: null, reported: null };
@ -22,8 +25,8 @@
async function fetchPageReports(
page: number,
filter: any
): Promise<(typeof Report.prototype.dataValues)[]> {
filter: typeof reportFilter | { hash: string }
): Promise<typeof currentPageReports> {
if (!browser) return [];
const response = await fetch(`${env.PUBLIC_BASE_PATH}/admin/reports`, {
@ -31,10 +34,46 @@
body: JSON.stringify({ ...filter, limit: reportsPerPage, from: reportPage * page })
});
if (activeReport) {
activeReport = null;
await goto(window.location.href.split('#')[0], { replaceState: true });
}
return await response.json();
}
$: fetchPageReports(reportPage, reportFilter).then((r) => (currentPageReports = r));
$: currentPageReportsRequest = fetchPageReports(reportPage, reportFilter).then((r) => {
currentPageReports = r;
});
async function openHashReport() {
if (!window.location.hash) return;
const requestedHash = window.location.hash.substring(1);
let report = currentPageReports.find((r) => r.url_hash === requestedHash);
if (!report) {
const hashReport = (await fetchPageReports(0, { hash: requestedHash }))[0];
if (hashReport) {
currentPageReports = [hashReport, ...currentPageReports];
report = hashReport;
} else {
await goto(window.location.href.split('#')[0], { replaceState: true });
return;
}
}
activeReport = report;
activeReport.originalStatus = report;
}
onMount(async () => {
await currentPageReportsRequest;
await openHashReport();
if (browser) window.addEventListener('hashchange', openHashReport);
});
onDestroy(() => {
if (browser) window.removeEventListener('hashchange', openHashReport);
});
async function updateActiveReport() {
await fetch(`${env.PUBLIC_BASE_PATH}/admin/reports`, {
@ -80,8 +119,11 @@
{#each currentPageReports as report}
<tr
class="hover [&>*]:text-sm cursor-pointer"
class:bg-base-200={activeReport === report}
class:bg-base-200={activeReport?.url_hash === report.url_hash}
on:click={() => {
goto(`${window.location.href.split('#')[0]}#${report.url_hash}`, {
replaceState: true
});
activeReport = report;
activeReport.originalStatus = report.status;
}}
@ -158,6 +200,15 @@
class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-max"
>
<li>
<button
on:click={() => {
navigator.clipboard.writeText(
`${window.location.protocol}//${window.location.host}${env.PUBLIC_BASE_PATH}/admin/reports#${activeReport.url_hash}`
);
}}
>
Internen Link kopieren
</button>
<button
on:click={() =>
navigator.clipboard.writeText(
@ -167,8 +218,12 @@
</li>
</ul>
</form>
<button class="btn btn-sm btn-circle btn-ghost" on:click={() => (activeReport = null)}
>✕</button
<button
class="btn btn-sm btn-circle btn-ghost"
on:click={() => {
activeReport = null;
goto(window.location.href.split('#')[0], { replaceState: true });
}}>✕</button
>
</div>
<h3 class="font-roboto font-semibold text-2xl">Report</h3>

View File

@ -22,9 +22,11 @@ export const POST = (async ({ request, cookies }) => {
status: 'none' | 'review' | 'reviewed' | null;
reporter: string | null;
reported: string | null;
hash: string | null;
} = await request.json();
const reportFindOptions: Attributes<Report> = {};
let reportFindOptions: Attributes<Report> = {};
if (data.draft != null) reportFindOptions.draft = data.draft;
reportFindOptions.status = data.status == null ? ['none', 'review'] : data.status;
if (data.reporter != null) {
@ -41,6 +43,13 @@ export const POST = (async ({ request, cookies }) => {
});
reportFindOptions.reported_id = reported_ids.map((u) => u.id);
}
if (data.hash != null) {
reportFindOptions = { url_hash: data.hash };
data.from = 0;
data.limit = 1;
}
let reports = await Report.findAll({
where: reportFindOptions,
include: [