-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #989 from blessingbytes/fix-979
Fix: Add reusable component for downloading quest participants using …
- Loading branch information
Showing
3 changed files
with
88 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import Button from '@components/UI/button'; | ||
import { useNotification } from '@context/NotificationProvider'; | ||
import { AdminService } from '@services/authService'; | ||
|
||
interface DownloadQuestParticipantsButtonProps { | ||
questId: string | number; | ||
} | ||
|
||
const DownloadQuestParticipantsButton: React.FC<DownloadQuestParticipantsButtonProps> = ({ questId }) => { | ||
const { showNotification } = useNotification(); | ||
const [isLoading, setIsLoading] = React.useState(false); | ||
|
||
const handleDownload = async () => { | ||
setIsLoading(true); | ||
try { | ||
const data = await AdminService.getQuestParticipantsByQuestId({ id: Number(questId) }); | ||
|
||
const jsonString = JSON.stringify(data, null, 2); | ||
const blob = new Blob([jsonString], { type: 'application/json' }); | ||
|
||
const url = URL.createObjectURL(blob); | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = `quest_${questId}_participants_${new Date().toISOString().split('T')[0]}.json`; | ||
document.body.appendChild(a); | ||
a.click(); | ||
|
||
document.body.removeChild(a); | ||
URL.revokeObjectURL(url); | ||
|
||
showNotification('Quest participants downloaded successfully', 'success'); | ||
} catch (error) { | ||
console.error('Error downloading quest participants:', error); | ||
showNotification( | ||
'Failed to download quest participants', | ||
'error' | ||
); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="w-full flex justify-center"> | ||
<div className="w-full sm:w-fit"> | ||
<Button | ||
onClick={handleDownload} | ||
disabled={isLoading} | ||
> | ||
<p>{isLoading ? 'Downloading...' : 'Download quest participants'}</p> | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DownloadQuestParticipantsButton; |
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