Add pagination to activity and transactions

This commit is contained in:
2022-03-28 12:33:13 +02:00
parent a2f6857d20
commit f14b013932
2 changed files with 240 additions and 180 deletions

View File

@@ -1,56 +1,60 @@
<script>
import { onMount } from "svelte";
import { formatDataVolume, formatDate } from "../util/Formatting.svelte";
import { formatDate } from "../util/Formatting.svelte";
import Spinner from "../util/Spinner.svelte";
import Euro from "../util/Euro.svelte"
let loading = false
let months = []
let year = 0
let month = 0
let month_str = ""
let data = []
const load_activity = async () => {
loading = true
month_str = year + "-" + ("00"+(month)).slice(-2)
try {
// We keep fetching history until we have fetched two months without
// any activity
let empty_months = 0
let now = new Date()
while (empty_months < 2) {
const resp = await fetch(
window.api_endpoint+"/user/activity/" +
now.getFullYear()+"-"+("00"+(now.getMonth()+1)).slice(-2),
)
if(resp.status >= 400) {
let json = await resp.json()
if (json.value === "authentication_failed") {
window.location = "/login"
return
} else {
throw new Error(json.message)
}
const resp = await fetch(window.api_endpoint+"/user/activity/" + month_str)
if(resp.status >= 400) {
let json = await resp.json()
if (json.value === "authentication_failed") {
window.location = "/login"
return
} else {
throw new Error(json.message)
}
let month = {
rows: await resp.json(),
month: now.getFullYear()+"-"+("00"+(now.getMonth()+1)).slice(-2),
}
if (month.rows.length === 0) {
empty_months++
continue
}
months.push(month)
months = months
// Fetch the previous month
now.setMonth(now.getMonth()-1)
}
data = await resp.json()
} catch (err) {
alert(err)
} finally {
loading = false
}
};
const last_month = () => {
month--
if (month === 0) {
month = 12
year--
}
load_activity()
}
const next_month = () => {
month++
if (month === 13) {
month = 1
year++
}
load_activity()
}
onMount(() => {
let now = new Date()
year = now.getFullYear()
month = now.getMonth()+1
load_activity()
})
</script>
@@ -68,47 +72,71 @@ onMount(() => {
for breaking the content policy.
</p>
{#each months as month}
<h3>{month.month}</h3>
<div class="table_scroll">
<table style="text-align: left;">
<thead>
<h3>{month_str}</h3>
<div class="toolbar">
<button on:click={last_month}>
<i class="icon">chevron_left</i>
Previous month
</button>
<div class="toolbar_spacer"></div>
<button on:click={next_month}>
Next month
<i class="icon">chevron_right</i>
</button>
</div>
<div class="table_scroll">
<table style="text-align: left;">
<thead>
<tr>
<td>Time</td>
<td>Event</td>
<td>File name</td>
<td>File removal reason</td>
</tr>
</thead>
<tbody>
{#each data as row}
<tr>
<td>Time</td>
<td>Event</td>
<td>File name</td>
<td>File removal reason</td>
<td>
{formatDate(row.time, true, true, false)}
</td>
<td>
{#if row.event === "file_instance_blocked"}
File blocked for abuse
{:else if row.event === "file_instance_expired"}
File expired
{/if}
</td>
<td>
{#if row.event === "file_instance_blocked"}
<a href="/u/{row.file_id}">{row.file_name}</a>
{:else}
{row.file_name}
{/if}
</td>
<td>
{row.file_removal_reason}
</td>
</tr>
</thead>
<tbody>
{#each month.rows as row}
<tr>
<td>
{formatDate(row.time, true, true, false)}
</td>
<td>
{#if row.event === "file_instance_blocked"}
File blocked for abuse
{:else if row.event === "file_instance_expired"}
File expired
{/if}
</td>
<td>
{#if row.event === "file_instance_blocked"}
<a href="/u/{row.file_id}">{row.file_name}</a>
{:else}
{row.file_name}
{/if}
</td>
<td>
{row.file_removal_reason}
</td>
</tr>
{/each}
</tbody>
</table>
{/each}
</tbody>
</table>
</div>
{#if data.length > 100}
<div class="toolbar">
<button on:click={last_month}>
<i class="icon">chevron_left</i>
Previous month
</button>
<div class="toolbar_spacer"></div>
<button on:click={next_month}>
Next month
<i class="icon">chevron_right</i>
</button>
</div>
{/each}
{/if}
</section>
<style>
@@ -120,4 +148,11 @@ onMount(() => {
width: 100px;
z-index: 1000;
}
.toolbar {
display: flex;
flex-direction: row;
width: 100%;
}
.toolbar > * { flex: 0 0 auto; }
.toolbar_spacer { flex: 1 1 auto; }
</style>