-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export participant list button
improve file naming of export participation urls exported file. improve component file naming to differentiate bt export participant list and export participation urls functionalities
- Loading branch information
Showing
5 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/(dashboard)/dashboard/participants/_components/ExportCSVParticipantURLs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use client'; | ||
|
||
import { Protocol, type Participant } from '@prisma/client'; | ||
import { Download } from 'lucide-react'; | ||
import { unparse } from 'papaparse'; | ||
import { useState } from 'react'; | ||
import { Button } from '~/components/ui/Button'; | ||
import { useToast } from '~/components/ui/use-toast'; | ||
import { useDownload } from '~/hooks/useDownload'; | ||
import { getBaseUrl } from '~/trpc/shared'; | ||
|
||
function ExportCSVParticipantURLs({ | ||
participants, | ||
protocol, | ||
disabled, | ||
}: { | ||
participants: Participant[] | undefined; | ||
protocol: Protocol | undefined; | ||
disabled: boolean; | ||
}) { | ||
const download = useDownload(); | ||
const [isExporting, setIsExporting] = useState(false); | ||
const { toast } = useToast(); | ||
|
||
const handleExport = () => { | ||
try { | ||
setIsExporting(true); | ||
if (!participants) return; | ||
if (!protocol?.id) return; | ||
|
||
// CSV file format | ||
const csvData = participants.map((participant) => ({ | ||
id: participant.id, | ||
identifier: participant.identifier, | ||
interview_url: `${getBaseUrl()}/onboard/${protocol.id}/?participantId=${ | ||
participant.id | ||
}`, | ||
})); | ||
|
||
const csv = unparse(csvData, { header: true }); | ||
|
||
// Create a download link | ||
const blob = new Blob([csv], { type: 'text/csv' }); | ||
const url = URL.createObjectURL(blob); | ||
// trigger the download | ||
const protocolNameWithoutExtension = protocol.name.split('.')[0]; | ||
const fileName = `participation_urls_${protocolNameWithoutExtension}.csv`; | ||
download(url, fileName); | ||
// Clean up the URL object | ||
URL.revokeObjectURL(url); | ||
toast({ | ||
description: 'Participation URLs CSV exported successfully', | ||
variant: 'success', | ||
duration: 3000, | ||
}); | ||
} catch (error) { | ||
toast({ | ||
title: 'Error', | ||
description: 'An error occurred while exporting participation URLs', | ||
variant: 'destructive', | ||
}); | ||
throw new Error('An error occurred while exporting participation URLs'); | ||
} | ||
|
||
setIsExporting(false); | ||
}; | ||
|
||
return ( | ||
<Button disabled={disabled || isExporting} onClick={handleExport}> | ||
<Download className="mr-2 h-4 w-4" /> | ||
{isExporting ? 'Exporting...' : 'Export Participation URLs'} | ||
</Button> | ||
); | ||
} | ||
|
||
export default ExportCSVParticipantURLs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters