enabled download of exported csv

This commit is contained in:
Djeeberjr 2025-10-20 14:30:15 +02:00
parent 2e75ba2908
commit 770dca5b0f
2 changed files with 94 additions and 81 deletions

View File

@ -3,15 +3,21 @@
import IDTable from "./lib/IDTable.svelte"; import IDTable from "./lib/IDTable.svelte";
import LastId from "./lib/LastID.svelte"; import LastId from "./lib/LastID.svelte";
import AddIDModal from "./lib/AddIDModal.svelte"; import AddIDModal from "./lib/AddIDModal.svelte";
import ExportModal from "./lib/ExportModal.svelte";
import { generateCSVFile } from "./lib/exporting";
import { fetchMapping, type IDMap } from "./lib/IDMapping";
import { downloadBlob } from "./lib/downloadBlob";
let lastID: string = $state(""); let lastID: string = $state("");
let mapping: IDMap | null = $state(null);
let addModal: AddIDModal; let addModal: AddIDModal;
let idTable: IDTable; let exportModal: ExportModal;
onMount(async () => {
mapping = await fetchMapping();
onMount(() => {
let sse = new EventSource("/api/idevent"); let sse = new EventSource("/api/idevent");
sse.onmessage = (e) => { sse.onmessage = (e) => {
lastID = e.data; lastID = e.data;
}; };
@ -25,13 +31,14 @@
<h1 class="text-3xl sm:text-4xl font-bold text-gray-800">Anwesenheit</h1> <h1 class="text-3xl sm:text-4xl font-bold text-gray-800">Anwesenheit</h1>
</div> </div>
<a <button
class="px-6 py-3 text-lg font-semibold text-white bg-indigo-600 rounded-2xl shadow-md hover:bg-indigo-700 transition" class="px-6 py-3 text-lg font-semibold text-white bg-indigo-600 rounded-2xl shadow-md hover:bg-indigo-700 transition"
href="/api/csv" onclick={() => {
download="anwesenheit.csv" exportModal.open();
}}
> >
Download CSV Export CSV
</a> </button>
<div class="pt-3 pb-2"> <div class="pt-3 pb-2">
<LastId <LastId
@ -42,15 +49,32 @@
/> />
</div> </div>
<div> <div>
<IDTable bind:this={idTable} onEdit={(id,firstName,lastName)=>{ {#if mapping}
<IDTable
data={mapping}
onEdit={(id, firstName, lastName) => {
addModal.open(id, firstName, lastName); addModal.open(id, firstName, lastName);
}}/> }}
/>
{/if}
</div> </div>
<AddIDModal <AddIDModal
bind:this={addModal} bind:this={addModal}
onSubmitted={() => { onSubmitted={async () => {
idTable.reloadData(); mapping = await fetchMapping();
}}
/>
<ExportModal
bind:this={exportModal}
onSubmitted={async (from, to) => {
if (!mapping) {
return;
}
let csvFile = await generateCSVFile(from, to, mapping);
downloadBlob("export.csv",csvFile,"text/csv");
}} }}
/> />
</main> </main>

View File

@ -1,16 +1,13 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { type IDMap } from "./IDMapping";
import { fetchMapping, type IDMap } from "./IDMapping";
let data: IDMap | undefined = $state();
let { let {
onEdit, onEdit,
}: { onEdit?: (id: string, firstName: string, lastName: string) => void } = data,
$props(); }: {
onEdit?: (id: string, firstName: string, lastName: string) => void;
export async function reloadData() { data: IDMap;
data = await fetchMapping(); } = $props();
}
let rows = $derived( let rows = $derived(
data data
@ -44,15 +41,8 @@
if (sortKey !== key) return ""; if (sortKey !== key) return "";
return sortDirection === "asc" ? "▲" : "▼"; return sortDirection === "asc" ? "▲" : "▼";
} }
onMount(async () => {
await reloadData();
});
</script> </script>
{#if data == null}
Loading...
{:else}
<div class="bg-indigo-500 py-2 rounded-2xl overflow-x-auto"> <div class="bg-indigo-500 py-2 rounded-2xl overflow-x-auto">
<table class="px-10"> <table class="px-10">
<thead> <thead>
@ -106,7 +96,6 @@
</tbody> </tbody>
</table> </table>
</div> </div>
{/if}
<style lang="css" scoped> <style lang="css" scoped>
@reference "../app.css"; @reference "../app.css";