Skip to content

Commit ed933ef

Browse files
authored
Merge pull request #293 from SPARCS-UP-Mindanao/292-fe-add-button-to-download-registrations-csv-in-admin-registrations-page-in-addition-to-pre-registrations-page
fix: working export csv button on registrations pages
2 parents bc8b967 + 3541f79 commit ed933ef

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

frontend/src/api/registrations.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export interface RegistrationDto {
3535
transactionId: string | null;
3636
}
3737

38+
interface CsvResponse {
39+
downloadLink: string;
40+
objectKey: string;
41+
}
42+
3843
const mapRegistrationDtoToRegistration = (registration: RegistrationDto): Registration => ({
3944
...registration,
4045
type: 'registration'
@@ -101,3 +106,10 @@ export const deleteRegistration = (eventId: string, registrationId: string) =>
101106
url: `/registrations/${registrationId}`,
102107
queryParams: { eventId, entryId: registrationId }
103108
});
109+
110+
export const getCsvRegistrations = (eventId: string) =>
111+
createApi<CsvResponse>({
112+
authorize: true,
113+
method: 'get',
114+
url: `/registrations/${eventId}/csv_download`
115+
});

frontend/src/hooks/useGetCsv.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
import { useState } from 'react';
22
import { getCsvPreRegistrations } from '@/api/preregistrations';
3+
import { getCsvRegistrations } from '@/api/registrations';
34
import { CustomAxiosError } from '@/api/utils/createApi';
45
import { useApi } from './useApi';
56
import { useNotifyToast } from './useNotifyToast';
67

8+
type ApiResponse = {
9+
data: {
10+
downloadLink: string;
11+
objectKey: string;
12+
};
13+
status: number;
14+
errorData?: any;
15+
};
16+
717
export const useGetCsv = (eventId: string) => {
818
const { errorToast } = useNotifyToast();
919
const [isGettingCsv, setIsGettingCsv] = useState(false);
1020
const api = useApi();
1121

12-
const getCsv = async () => {
22+
const getCsv = async (reg_status: 'preregistrations' | 'registrations') => {
1323
try {
1424
setIsGettingCsv(true);
15-
const response = await api.execute(getCsvPreRegistrations(eventId));
16-
const data = response.data;
25+
26+
let response: ApiResponse;
27+
28+
if (reg_status === 'preregistrations') {
29+
response = await api.execute(getCsvPreRegistrations(eventId));
30+
} else if (reg_status === 'registrations') {
31+
response = await api.execute(getCsvRegistrations(eventId));
32+
} else {
33+
console.error('Invalid reg_status');
34+
return;
35+
}
1736

1837
if (response.status === 200) {
38+
const { data } = response;
1939
const link = document.createElement('a');
2040
link.href = data.downloadLink;
2141
link.download = data.objectKey;

frontend/src/pages/admin/event/registrations/AdminEventRegistrations.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ const AdminEventRegistrations: FC = () => {
2626
return (
2727
<section>
2828
<Tabs defaultValue={getDefaultValue()} className="flex flex-col items-center">
29-
{isApprovalFlow && (
30-
<div className="self-start flex justify-between w-full items-center">
31-
<TabsList>
32-
<TabsTrigger value="preregistrations">Pre-registrations</TabsTrigger>
33-
<TabsTrigger value="registrations" disabled={status === 'preregistration'}>
34-
Registrations
35-
</TabsTrigger>
36-
</TabsList>
37-
<Button variant="positive" disabled={isGettingCsv} onClick={() => getCsv()}>
38-
Export CSV
39-
</Button>
29+
<div className="self-start flex justify-between w-full items-center">
30+
<div>
31+
{isApprovalFlow && (
32+
<TabsList>
33+
<TabsTrigger value="preregistrations">Pre-registrations</TabsTrigger>
34+
<TabsTrigger value="registrations" disabled={status === 'preregistration'}>
35+
Registrations
36+
</TabsTrigger>
37+
</TabsList>
38+
)}
4039
</div>
41-
)}
40+
<Button variant="positive" disabled={isGettingCsv} onClick={() => getCsv(getDefaultValue())}>
41+
Export CSV
42+
</Button>
43+
</div>
4244
<TabsContent value="preregistrations" className="w-full">
4345
<PreRegistrations />
4446
</TabsContent>

0 commit comments

Comments
 (0)