Skip to content

Commit

Permalink
feat: downloading TM csv
Browse files Browse the repository at this point in the history
  • Loading branch information
1grzyb1 committed Mar 23, 2024
1 parent aad6f9b commit fc9e788
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion odyseja-ui/src/lib/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function showToast(message: string, background: string) {
toastStore.trigger(t);
}

function getBearer(): string {
export function getBearer(): string {
return 'Bearer ' + getCookie("access_token")
}

Expand Down
20 changes: 18 additions & 2 deletions odyseja-ui/src/routes/panel/tm/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
<script>
import {generateCsv} from './tm.ts';
import {generateCsv} from './tm';
let zspId = "";
async function downloadCsv() {
let csv = await generateCsv(zspId);
const blob = new Blob([csv], {type: 'text/csv'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `tm_${zspId}.csv`;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>

<h1 class="m-5">TM</h1>
Expand All @@ -12,6 +28,6 @@

<input bind:value={zspId} class="input m-5" placeholder="ZSP ID"
type="text"/>
<button class="btn btn-md variant-filled-secondary h-10" on:click={generateCsv}>Generuj csv</button>
<button class="btn btn-md variant-filled-secondary h-10" on:click={downloadCsv}>Generuj csv</button>
</div>

21 changes: 17 additions & 4 deletions odyseja-ui/src/routes/panel/tm/tm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import {post} from "$lib/apiService";
import type {ZspIdRequest} from "$lib/types";
import {BASE_URL, getBearer, showHappyToast, showSadToast} from "$lib/apiService";


export async function generateCsv(zspIdRequest: ZspIdRequest) {
await post(zspIdRequest, '/api/v1/tm/generate', 'Csv generated');
export async function generateCsv(zspIdRequest: string) {
const response = await fetch(BASE_URL + '/api/v1/tm/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: getBearer(),
},
body: JSON.stringify({zspId: zspIdRequest})
})
if (!response.ok) {
showSadToast('Coś poszło nie tak :c')
throw new Error(`HTTP error! status: ${response.status}`);
}

showHappyToast('Csv generated')
return response.text();
}

0 comments on commit fc9e788

Please sign in to comment.