Skip to content

Commit

Permalink
Admin panel - Export Donations to excel (#1245)
Browse files Browse the repository at this point in the history
* branch init

* add api endpoint for export donations
add export to excel method in donation service

* clear console logs

* clear console log

* export downloadFile logic and use it

Co-authored-by: Andrey <andrey.goranov@mentormate.com>
  • Loading branch information
AndreyGoranov and Andrey authored Dec 11, 2022
1 parent 9a9c4ec commit 2593a22
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions public/locales/bg/donations.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"requiredError": "Полето е задължително."
},
"cta": {
"download": "Изтегли",
"add": "Добави",
"confirm": "Потвърди",
"cancel": "Отказ",
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/donations.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"requiredError": "Fields is required!"
},
"cta": {
"download": "Download",
"add": "Add",
"confirm": "Confirm",
"cancel": "Cancel",
Expand Down
8 changes: 8 additions & 0 deletions src/common/util/downloadFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const downloadFile = (filename: string, data: Blob | MediaSource) => {
const url = window.URL.createObjectURL(data)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = filename
anchor.dispatchEvent(new MouseEvent('click'))
window.URL.revokeObjectURL(url)
}
2 changes: 2 additions & 0 deletions src/components/donations/grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ export default observer(function Grid() {
}: UseQueryResult<CampaignDonationHistoryResponse> = useDonationsList(campaignId, page, pageSize)

const { data }: UseQueryResult<PersonResponse[]> = usePersonList()

const RenderVaultCell = ({ params }: RenderCellProps) => {
return <>{params.row.targetVault.name}</>
}

const RenderPersonCell = ({ params }: RenderCellProps) => {
const { firstName, lastName } = params.row.person
? params.row.person
Expand Down
21 changes: 20 additions & 1 deletion src/components/donations/grid/GridAppbar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'
import { Box, Toolbar, Tooltip, Typography } from '@mui/material'
import { Add as AddIcon, Receipt } from '@mui/icons-material'
import { Add as AddIcon, Receipt, GetApp as DownloadFileIcon } from '@mui/icons-material'

import { routes } from 'common/routes'
import { useMutation } from '@tanstack/react-query'
import { useExportToExcel } from 'service/donation'
import { AlertStore } from 'stores/AlertStore'
import { downloadFile } from './../../../common/util/downloadFile'

const addIconStyles = {
background: '#4ac3ff',
Expand All @@ -16,6 +20,14 @@ const addIconStyles = {
export default function GridAppbar() {
const router = useRouter()
const { t } = useTranslation()
const exportToExcel = useMutation({
mutationFn: useExportToExcel(),
onError: () => AlertStore.show(t('common:alerts.error'), 'error'),
onSuccess: ({ data }) => {
downloadFile('Donations.xls', data)
AlertStore.show(t('common:alerts.success'), 'success')
},
})

return (
<Toolbar
Expand Down Expand Up @@ -45,6 +57,13 @@ export default function GridAppbar() {
onClick={() => router.push(routes.admin.donations.create)}
/>
</Tooltip>
<Tooltip title={t('donations:cta:download') || ''}>
<DownloadFileIcon
sx={addIconStyles}
fontSize="large"
onClick={() => exportToExcel.mutate()}
/>
</Tooltip>
</Box>
</Box>
</Toolbar>
Expand Down
1 change: 1 addition & 0 deletions src/service/apiEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const endpoints = {
userDonations: <Endpoint>{ url: 'donation/user-donations', method: 'GET' },
uploadBankTransactionsFile: (bankTransactionsFileId: string) =>
<Endpoint>{ url: `/bank-transactions-file/${bankTransactionsFileId}`, method: 'POST' },
exportToExcel: <Endpoint>{ url: '/donation/export-excel', method: 'GET' },
},
documents: {
documentsList: <Endpoint>{ url: '/document', method: 'GET' },
Expand Down
10 changes: 10 additions & 0 deletions src/service/donation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ export const useUploadBankTransactionsFiles = () => {
)
}
}

export const useExportToExcel = () => {
const { data: session } = useSession()
return async () => {
return await apiClient(endpoints.donation.exportToExcel.url, {
...authConfig(session?.accessToken),
responseType: 'blob',
})
}
}

0 comments on commit 2593a22

Please sign in to comment.