Skip to content

Commit

Permalink
Add "Export to CSV" function
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneIRL committed Oct 5, 2023
1 parent 5e899ad commit c84b866
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/app/admin/applications/ApplicantDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,61 @@ export default function ApplicantDataTable({
}
}

const exportCsv = (anchor: HTMLAnchorElement, apps: readonly Row[]) => {
const stringify = (v: boolean | number | string | undefined) =>
v ? v.toString() : ''
const quote = (s: string) => s.includes(',') ? `"${s}"` : s
const cell = (v: boolean | number | string | undefined) =>
quote(stringify(v))
const fields = (r: Row) => [
r.firstName,
r.lastName,
r.age,
r.email,
r.school,
r.phoneNumber,
r.countryOfResidence,
r.levelOfStudy,
r.agreedMlhCoC === 'Yes' ? 'TRUE' : 'FALSE',
r.agreedMlhMarketing === 'Yes' ? 'TRUE' : 'FALSE',
r.agreedMlhSharing === 'Yes' ? 'TRUE' : 'FALSE',
]
const row = (r: Row) => fields(r).map(cell).join(',')
const headers = () => [
'First Name',
'Last Name',
'Age',
'Email',
'School',
'Phone Number',
'Country',
'Current Level of Study',
'Code of Conduct Checkbox',
'MLH Marketing Opt In Checkbox',
'Privacy Policy Checkbox',
]
const csv = [headers(), ...apps.map(row)].join('\n')
anchor.setAttribute(
'href',
encodeURI(`data:text/csv;charset=utf-8,${csv}`),
)
}

const counts = countBy(applications, 'status')

return (
<div className="flex flex-col gap-2">
<p>
Tot/Acc/Rej/Wai/Und:{' '}
{applications.length}/{counts.accepted ?? 0}/{counts.rejected
?? 0}/{counts.waitlisted ?? 0}/{counts.undecided ?? 0}.{' '}
<a
className="text-hackuta-blue underline cursor-pointer"
onClick={(e) => exportCsv(e.currentTarget, applications)}
download="applications.csv"
>
Export to CSV
</a>
</p>
<DataTable
value={applications}
Expand Down

0 comments on commit c84b866

Please sign in to comment.