-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectTableContainer.tsx
More file actions
235 lines (208 loc) · 6.93 KB
/
ProjectTableContainer.tsx
File metadata and controls
235 lines (208 loc) · 6.93 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { useState } from "react";
import { Typography, Button, Modal, message } from "antd";
import useAxios from "axios-hooks";
import { ExclamationCircleOutlined } from "@ant-design/icons";
import axios from "axios";
import { apiUrl, Service } from "@hex-labs/core";
import ProjectTable from "./ProjectTable";
import { Project } from "../../types/Project";
import { Ballot } from "../../types/Ballot";
import { ModalState } from "../../util/FormModalProps";
import BallotEditFormModal from "./BallotEditFormModal";
import ErrorDisplay from "../../displays/ErrorDisplay";
import LoadingDisplay from "../../displays/LoadingDisplay";
import { TableGroup } from "../../types/TableGroup";
import { Category } from "../../types/Category";
import { useCurrentHexathon } from "../../contexts/CurrentHexathonContext";
const { Title } = Typography;
interface Props {
projects: Project[];
isSponsor: boolean;
refetch?: any;
}
const ProjectTableContainer: React.FC<Props> = props => {
const CurrentHexathonContext = useCurrentHexathon();
const { currentHexathon } = CurrentHexathonContext;
const [modalState, setModalState] = useState({
visible: false,
initialValues: null,
} as ModalState);
const [{ data: criteriaData, loading, error }, refetch] = useAxios(
apiUrl(Service.EXPO, "/criterias")
);
const [{ data: categoriesData, loading: categoriesLoading, error: categoriesError }] = useAxios({
method: "GET",
url: apiUrl(Service.EXPO, "/categories"),
params: {
hexathon: currentHexathon.id,
},
});
if (categoriesLoading || loading) {
return <LoadingDisplay />;
}
if (categoriesError || error) {
return <ErrorDisplay error={error} />;
}
const defaultCategories = categoriesData.filter((category: Category) => category.isDefault);
const deleteScores = async (values: any) => {
const ballotIds = values.scores.map((ballot: any) => ballot.id);
const ids = {
ids: ballotIds,
};
try {
axios
.delete(apiUrl(Service.EXPO, `/ballots/batch/delete`), { data: ids })
.then(res => {
if (res.data.error) {
message.error(res.data.message, 2);
} else {
message.success("Success!", 2);
props.refetch();
}
})
.catch(err => {
message.error("Error: Please ask for help", 2);
console.log(err);
});
} catch (info) {
console.log("Validate Failed:", info);
}
};
const deleteJudge = async (judgeName: string, project: Project) => {
const ballotIds = project.ballots
.filter((ballot: Ballot) => ballot.user.name === judgeName)
.map((ballot: Ballot) => ballot.id);
const ids = { ids: ballotIds };
try {
const response = await axios.delete(apiUrl(Service.EXPO, `/ballots/batch/delete`), { data: ids });
if (response.data.error) {
message.error(response.data.message, 2);
} else {
message.success(`Successfully deleted scores for judge ${judgeName}`, 2);
props.refetch();
}
} catch (error) {
console.error("Error deleting judge:", error);
message.error("Error: Please ask for help", 2);
}
};
const openModal = (values: any) => {
setModalState({
visible: true,
initialValues: { ...values },
});
};
const { confirm } = Modal;
const BallotModal = BallotEditFormModal;
function showConfirm(values: any) {
const ballotIds = values.scores.map((ballot: any) => ballot.id);
confirm({
title: "Do you want to delete these scores?",
icon: <ExclamationCircleOutlined />,
content: "This cannot be undone",
onOk() {
deleteScores(values);
},
});
}
return (
<div>
{props.projects?.map((project: any) => {
const generateData = (categoryId: number) => {
const data: any = [];
const judgeBallots: any = [];
let total = 0;
const ballotScores: any = [];
project.ballots.forEach((ballot: Ballot) => {
if (ballot.criteria.categoryId === categoryId) {
data[ballot.user.name] = (data[ballot.user.name] || 0) + ballot.score;
if (judgeBallots[ballot.user.name]) {
judgeBallots[ballot.user.name].push(ballot);
} else {
judgeBallots[ballot.user.name] = [];
judgeBallots[ballot.user.name].push(ballot);
}
total += ballot.score;
ballotScores.push(ballot);
}
});
const newData = Object.entries(data).map((e: any) => {
const editButton = (
<Button
type="primary"
onClick={() => {
openModal({ scores: judgeBallots[e[0]] });
}}
>
Edit
</Button>
);
const deleteButton = (
<Button
type="primary"
onClick={() => {
showConfirm({ scores: judgeBallots[e[0]] });
}}
>
Delete
</Button>
);
const deleteJudgeButton = (
<Button
type="primary"
danger
onClick={() => {
deleteJudge(e[0], project);
}}
>
Delete Judge
</Button>
);
return {
judge: e[0],
total: e[1],
editScore: editButton,
deleteScore: deleteButton,
deleteJudge: deleteJudgeButton,
};
});
newData.push({
judge: "Average",
total: Math.round((total / newData.length) * 10) / 10,
editScore: <></>,
deleteScore: <></>,
deleteJudge: <></>,
});
return newData;
};
const allCategories = [...project.categories, ...defaultCategories];
return (
<div key={project.id}>
<Title key={project.id} level={4}>
<a href={project.devpostUrl} target="_blank">
{props.isSponsor
? `Project Name: ${project.name}`
: `${project.id} - ${project.name}`}
</a>
</Title>
<p>
{project.tableGroup !== undefined ? project.tableGroup.name : "N/A"}, Table #
{project.table}, Expo #{project.expo}
</p>
{allCategories.map((category: Category) => (
<>
<Title level={5} key={category.id}>
{category.name}
</Title>
<ProjectTable data={generateData(category.id)} />
</>
))}
<br />
</div>
);
})}
<BallotModal modalState={modalState} setModalState={setModalState} refetch={props.refetch} />
</div>
);
};
export default ProjectTableContainer;