Merge pull request #115 from EETagent/download_csv

(frontend) download csv on admin page
This commit is contained in:
Vojtěch Jungmann 2023-01-01 16:19:06 +01:00 committed by GitHub
commit 70edff49a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 3 deletions

View file

@ -120,3 +120,23 @@ export const apiFetchCandidate = async (id: number, fetchSsr?: Fetch): Promise<C
throw errorHandler(e, 'Failed to fetch candidate data');
}
};
// SSR compatible
// List all candidates /admin/list/candidates
export const apiListCandidatesCSV = async (
fetchSsr?: Fetch,
): Promise<Blob> => {
const apiFetch = fetchSsr || fetch;
try {
const res = await apiFetch(API_URL + '/admin/list/candidates_csv', {
method: 'GET',
credentials: 'include'
});
if (res.status != 200) {
throw Error(await res.text());
}
return await res.blob();
} catch (e) {
throw errorHandler(e, 'List candidates CSV failed');
}
};

View file

@ -1,5 +1,5 @@
<script lang="ts">
import { apiDeleteCandidate, apiListCandidates } from '$lib/@api/admin';
import { apiDeleteCandidate, apiListCandidates, apiListCandidatesCSV } from '$lib/@api/admin';
import Home from '$lib/components/icons/Home.svelte';
import TextField from '$lib/components/textfield/TextField.svelte';
import type { CandidatePreview } from '$lib/stores/candidate';
@ -74,6 +74,19 @@
if (id) await apiDeleteCandidate(id);
getCandidates();
};
const downloadCSV = async () => {
try {
const csvBlob = await apiListCandidatesCSV();
const url = window.URL.createObjectURL(new Blob([csvBlob]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'UCHAZECI' + '.csv');
link.click();
} catch (e) {
console.log(e);
}
}
</script>
{#if createCandidateModal}
@ -99,9 +112,14 @@
<TextField on:keyup={search} bind:value={searchValue} placeholder="Hledat" />
<button
on:click={openCreateCandidateModal}
class="bg-sspsBlue hover:bg-sspsBlueDark ml-3 w-2/5 rounded-lg p-3 py-4 text-xl font-semibold text-white transition-colors duration-300"
class="bg-gray-500 hover:bg-gray-600 ml-3 w-2/5 rounded-lg p-3 py-4 text-xl font-semibold text-white transition-colors duration-300"
>Nový uchazeč</button
>
<button
on:click={downloadCSV}
class="bg-gray-500 hover:bg-gray-600 ml-3 w-2/5 rounded-lg p-3 py-4 text-xl font-semibold text-white transition-colors duration-300"
>CSV</button
>
</div>
{#if scrollTop > 200}
<div class="fixed bottom-8 right-8">
@ -113,7 +131,7 @@
</div>
{/if}
<Table candidates={candidatesTable} on:delete={(event) => deleteCandidate(event.detail.id)} />
<Table candidates={candidatesTable} on:delete={(event) => deleteCandidate(event.detail.id)} />
</div>
</div>
</div>