-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatisticsPage.tsx
More file actions
182 lines (158 loc) · 6.05 KB
/
StatisticsPage.tsx
File metadata and controls
182 lines (158 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React from "react";
import {
Box,
HStack,
Button,
Heading,
Select,
Spinner,
Stack,
Text,
VStack,
} from "@chakra-ui/react";
import { apiUrl, ErrorScreen, LoadingScreen, Service } from "@hex-labs/core";
import { useParams } from "react-router-dom";
import useAxios from "axios-hooks";
import { DownloadIcon } from "@chakra-ui/icons";
/*
import AccordionSection from "./AccordionSection";
import GraphAccordionSection from "./GraphAccordionSection";
import TreeMapView from "./graphs/TreeMapView";
*/
import { Branch, BranchType } from "../branchSettings/BranchSettingsPage";
import { RenderStatistics } from "./RenderStatistics";
import XLSXExporter from "../../../util/xlsxExport";
import { useCurrentHexathon } from "../../../contexts/CurrentHexathonContext";
const StatisticsPage: React.FC = () => {
const [selectedBranch, setSelectedBranch] = React.useState<Branch | null>(null);
const [selectedStatus, setSelectedStatus] = React.useState<string | null>("CONFIRMED");
const { hexathonId } = useParams();
const { currentHexathon } = useCurrentHexathon();
// Enable manual mode
const [{ data, loading, error }, refetchStatistics] = useAxios({
method: "GET",
url: apiUrl(Service.REGISTRATION, "/statistics"),
params: {
hexathon: hexathonId,
},
});
const exportToXLSX = React.useCallback(() => {
if (!hexathonId) { return; }
// whitespaces bad
const noWhitespaceName = (currentHexathon.name as string).replaceAll(" ", "_");
const exporter = new XLSXExporter({name: noWhitespaceName, id: hexathonId});
exporter.addKeyValueData(data.userStatistics, "Overall User Statistics");
exporter.addTableData(data.applicationStatistics, "Branch", "Application Statistics");
exporter.addTableData(data.confirmationStatistics, "Branch", "Confirmation Statistics");
// eslint-disable-next-line guard-for-in
for (const key in data.applicationDataStatistics) {
const branchData = data.applicationDataStatistics[key];
exporter.addKeyValueData(branchData, `${key}`);
}
const url = exporter.getDownloadURL();
const a = document.createElement("a");
let downloadName = noWhitespaceName;
if (selectedBranch) downloadName = downloadName.concat(`_filterBranch-${selectedBranch.name}`);
if (selectedStatus) downloadName = downloadName.concat(`_filterStatus-${selectedStatus}`);
a.download = downloadName.concat(".xlsx");
a.href = url;
a.click();
exporter.cleanupDownloadURL();
}, [currentHexathon.name, data, hexathonId, selectedBranch, selectedStatus]);
// Fetch statistics whenever selectedBranchId changes
React.useEffect(() => {
if (hexathonId) {
const params: any = { hexathon: hexathonId };
if (selectedBranch) {
const { type, id } = selectedBranch;
if (type === BranchType.CONFIRMATION) {
params.confirmationBranch = id;
} else if (type === BranchType.APPLICATION) {
params.applicationBranch = id;
}
}
if (selectedStatus) {
params.status = selectedStatus;
}
refetchStatistics({ params });
}
}, [selectedBranch, selectedStatus, hexathonId, refetchStatistics]);
const [{ data: branchData, loading: branchLoading, error: branchError }] = useAxios({
method: "GET",
url: apiUrl(Service.REGISTRATION, "/branches"),
params: {
hexathon: hexathonId,
},
});
if (!hexathonId) return <ErrorScreen error={new Error("Hexathon ID invalid!")} />;
if (loading) return <LoadingScreen />;
if (error) return <ErrorScreen error={error} />;
if (branchError) return <ErrorScreen error={branchError} />
return (
<Box w="100%" p={5}>
<Stack>
<VStack mb={8}>
<Heading as="h1">Statistics</Heading>
<Text fontSize="lg" color="grey">
All of the data crunched into this page from all of the applications we recieved.
</Text>
<Box>
<Button colorScheme="blue" onClick={exportToXLSX}><DownloadIcon /> Export to XLSX</Button>
<Text fontSize="sm" textAlign="center" opacity={0.5}>(Filters will be reflected)</Text>
</Box>
{branchLoading? (
<Spinner />
) : (
<HStack>
<Box>
<Text fontSize="lg" color="grey">
Branch
</Text>
<Select
placeholder="All"
value={selectedBranch?.id ?? ""}
onChange={e => {
const selectedBranchId = e.target.value;
const newSelected = branchData.find(
(branch: any) => branch.id === selectedBranchId
);
setSelectedBranch(newSelected);
}}
>
{branchData.map((branch: any) => (
<option key={branch.id} value={branch.id}>
{branch.name}
</option>
))}
</Select>
</Box>
<Box>
<Text fontSize="lg" color="grey">
Status
</Text>
<Select
placeholder="All"
value={selectedStatus ?? ""}
onChange={e => {
const newSelectedStatus = e.target.value;
setSelectedStatus(newSelectedStatus);
}}
>
<option value="DRAFT">Draft</option>
<option value="APPLIED">Applied</option>
<option value="ACCEPTED">Accepted</option>
<option value="WAITLISTED">Waitlisted</option>
<option value="CONFIRMED">Confirmed</option>
<option value="DENIED">Denied</option>
<option value="NOT_ATTENDING">Not Attending</option>
</Select>
</Box>
</HStack>
)}
</VStack>
<RenderStatistics data={data} loading={loading} />
</Stack>
</Box>
);
};
export default StatisticsPage;