commit b566fca5429800423b7ceb9193c9bd3a82c3f631
parent 19c68c28c1af19cd51093c331675e2246413a1ae
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Thu, 26 Sep 2024 22:24:20 +0200
feat(heats): export csv client side only
Diffstat:
4 files changed, 12 insertions(+), 48 deletions(-)
diff --git a/src/api/db.cjs b/src/api/db.cjs
@@ -98,25 +98,6 @@ async function getUser(email) {
}
}
-async function exportHeatsToCSV() {
- try {
- const heats = await sql`
- copy (select created_at,
- name,
- location,
- planned_start
- from heats)
- to 'heat_rows.csv'
- delimiter ','
- csv header
- `
- return heats
- } catch (error) {
- console.log('Error occurred in exportHeatsToCSV:', error);
- throw error
- }
-}
-
async function allHeats() {
try {
const heats = await sql`
@@ -418,5 +399,4 @@ module.exports = {
addAthlete,
removeAthlete,
removeAthleteFromHeat,
- exportHeatsToCSV,
}
diff --git a/src/api/server.cjs b/src/api/server.cjs
@@ -70,7 +70,6 @@ const paths = [
'/v1/leaderboard/setScore',
'/v1/leaderboard/addAthleteToHeat',
'/v1/leaderboard/removeAthleteFromHeat',
- '/v1/leaderboard/exportHeatsToCSV',
'/v1/leaderboard/addAthlete',
'/v1/leaderboard/removeAthlete',
]
@@ -171,16 +170,6 @@ server.on('request', async (req, res) => {
} catch(error) {
serverError(res, error);
}
- } else if (url.pathname === '/v1/leaderboard/exportHeatsToCSV') {
- try {
- const heats = await db.exportHeatsToCSV()
-
- res.setHeader('Content-Type', 'text/csv');
- res.setHeader("Content-Disposition", "attachment;filename=myfilename.csv");
- res.end(heats);
- } catch(error) {
- serverError(res, error);
- }
} else if (req.method === 'POST') {
if (url.pathname === '/v1/echo') {
let body = [];
diff --git a/src/frontend/Heats.jsx b/src/frontend/Heats.jsx
@@ -72,10 +72,10 @@ async function deleteHeat(e, heatId, heatName) {
}
// export heats
-function ExportForm() {
+function ExportForm(heats) {
return (
<div className='exportForm'>
- <form method='post' onSubmit={e => exportHeatsToCSV(e)}>
+ <form method='post' onSubmit={e => exportHeatsToCSV(e, heats)}>
<button type='submit'>▿ export</button>
</form>
</div>
@@ -143,7 +143,7 @@ function HeatForm({session}) {
</tbody>
</table>
</form>
- <ExportForm />
+ <ExportForm heats={heats} />
</div>
)
}
diff --git a/src/frontend/utils.js b/src/frontend/utils.js
@@ -1,23 +1,19 @@
const api_uri = import.meta.env.VITE_API_URI ? import.meta.env.VITE_API_URI: 'http://127.0.0.1'
const api_port = import.meta.env.VITE_API_PORT ? import.meta.env.VITE_API_PORT: '8000'
-export const exportHeatsToCSV = async function(e) {
+export const exportHeatsToCSV = async function(e, { heats }) {
e.preventDefault()
- const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/exportHeatsToCSV`)
-
- try {
- const data = await res.text()
- if (data.length === 0) {
- alert('No heats yet, nothing to export')
- return
- }
+ // csv header
+ let csv = "created_at,name,location,planned_start\n"
- exportCSV(data, "heats")
- } catch (error) {
- console.error(error)
- alert(error.message)
+ // append athlete data
+ for (let i = 0; i < heats.length; i++) {
+ csv += heats[i].created_at + "," + heats[i].name+ "," + heats[i].location+ ","
+ + heats[i].planned_start+ "\n"
}
+
+ exportCSV(csv, "heats")
}
export const exportAthletesToCSV = async function(e, { athletes }) {
@@ -27,7 +23,6 @@ export const exportAthletesToCSV = async function(e, { athletes }) {
let csv = "created_at,nr,firstname,lastname,birthday,school\n"
// append athlete data
- console.log(athletes)
for (let i = 0; i < athletes.length; i++) {
csv += athletes[i].created_at + "," + athletes[i].nr+ "," + athletes[i].firstname+ ","
+ athletes[i].lastname+ "," + athletes[i].birthday+ "," + athletes[i].school + "\n"