myheats

Live heats, scoring and leaderboard for sport events
git clone https://git.in0rdr.ch/myheats.git
Log | Files | Refs | Pull requests | README | LICENSE

commit 19c68c28c1af19cd51093c331675e2246413a1ae
parent 3872d32efdac5e2ff97a8f6867ec86f4056bae0a
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Thu, 26 Sep 2024 22:19:53 +0200

feat(athletes): export csv client side only

Diffstat:
Msrc/api/db.cjs | 22----------------------
Msrc/api/server.cjs | 20--------------------
Msrc/frontend/Athletes.jsx | 6+++---
Msrc/frontend/utils.js | 45++++++++++++++++++++-------------------------
4 files changed, 23 insertions(+), 70 deletions(-)

diff --git a/src/api/db.cjs b/src/api/db.cjs @@ -117,27 +117,6 @@ async function exportHeatsToCSV() { } } -async function exportAthletesToCSV() { - try { - const athletes = await sql` - copy (select created_at, - nr, - firstname, - lastname, - birthday, - school - from athletes) - to 'athlete_rows.csv' - delimiter ',' - csv header - ` - return athletes - } catch (error) { - console.log('Error occurred in exportAthletesToCSV:', error); - throw error - } -} - async function allHeats() { try { const heats = await sql` @@ -440,5 +419,4 @@ module.exports = { removeAthlete, removeAthleteFromHeat, exportHeatsToCSV, - exportAthletesToCSV, } diff --git a/src/api/server.cjs b/src/api/server.cjs @@ -71,7 +71,6 @@ const paths = [ '/v1/leaderboard/addAthleteToHeat', '/v1/leaderboard/removeAthleteFromHeat', '/v1/leaderboard/exportHeatsToCSV', - '/v1/leaderboard/exportAthletesToCSV', '/v1/leaderboard/addAthlete', '/v1/leaderboard/removeAthlete', ] @@ -182,25 +181,6 @@ server.on('request', async (req, res) => { } catch(error) { serverError(res, error); } - } else if (url.pathname === '/v1/leaderboard/exportAthletesToCSV') { - try { - const athletes = await db.exportAthletesToCSV() - - res.setHeader('Content-Type', 'text/csv'); - res.setHeader("Content-Disposition", "attachment;filename=myfilename.csv"); - res.end(athletes); - } catch(error) { - serverError(res, error); - } - } else { - const pathExists = paths.find((i) => i === url.pathname); - if (pathExists) { - // wrong method for this path - notAllowed(res, req.method); - } else { - notFound(res, url.pathname); - } - } } else if (req.method === 'POST') { if (url.pathname === '/v1/echo') { let body = []; diff --git a/src/frontend/Athletes.jsx b/src/frontend/Athletes.jsx @@ -52,10 +52,10 @@ async function deleteAthlete(e, athleteId, athleteFirstName, athleteLastName) { } // export athletes -function ExportForm() { +function ExportForm(athletes) { return ( <div className='exportForm'> - <form method='post' onSubmit={e => exportAthletesToCSV(e)}> + <form method='post' onSubmit={e => exportAthletesToCSV(e, athletes)}> <button type='submit'>&#9663; export</button> </form> </div> @@ -134,7 +134,7 @@ function AthleteForm({session}) { </tbody> </table> </form> - <ExportForm /> + <ExportForm athletes={athletes} /> </div> ) } diff --git a/src/frontend/utils.js b/src/frontend/utils.js @@ -5,40 +5,35 @@ export const exportHeatsToCSV = async function(e) { e.preventDefault() const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/exportHeatsToCSV`) - const { data, error } = await res.text() - if (error) { - console.error(error) - } - if (error !== null) { + try { + const data = await res.text() + if (data.length === 0) { + alert('No heats yet, nothing to export') + return + } + + exportCSV(data, "heats") + } catch (error) { + console.error(error) alert(error.message) - return - } else if (data.length === 0) { - alert('No heats yet, nothing to export') - return } - - exportCSV(data, "heats") } -export const exportAthletesToCSV = async function(e) { +export const exportAthletesToCSV = async function(e, { athletes }) { e.preventDefault() - const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/exportAthletesToCSV`) - const { data, error } = await res.text() - if (error) { - console.error(error) - } + // csv header + let csv = "created_at,nr,firstname,lastname,birthday,school\n" - if (error !== null) { - alert(error.message) - return - } else if (data.length === 0) { - alert('No athletes yet, nothing to export') - return + // 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" } - exportCSV(data, "athletes") + exportCSV(csv, "athletes") } export const exportLeaderboardToCSV = async function(e, leaderboard, heatSelection, rankingComp) { @@ -49,7 +44,7 @@ export const exportLeaderboardToCSV = async function(e, leaderboard, heatSelecti return } - // concatenante heat labels + // concatenate heat labels const heatNames = heatSelection.map(h => h.label) // rank leaderboard by selected comparator