-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUserInfoHelperFunctions.tsx
More file actions
339 lines (286 loc) · 9.16 KB
/
UserInfoHelperFunctions.tsx
File metadata and controls
339 lines (286 loc) · 9.16 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import jsPDF from 'jspdf';
import * as QRCode from 'qrcode';
import { Account, Hacker, Settings, Sponsor } from '../api';
import {
FrontendRoute,
HackerStatus,
IAccount,
IHacker,
ISetting,
ISponsor,
UserType,
} from '../config';
import addBrownBoldFont from './jsPDF-brown-bold-font';
import addBrownFont from './jsPDF-brown-font';
import addHindFont from './jsPDF-hind-font';
export function userCanAccessCreateApplicationPage(user: IAccount) {
return user.confirmed && user.accountType === UserType.HACKER;
}
export function userCanAccessHackerPage(user: IAccount) {
return (
user.confirmed &&
(user.accountType === UserType.STAFF ||
user.accountType === UserType.HACKBOARD ||
user.accountType === UserType.VOLUNTEER ||
isSponsor(user))
);
}
export function isSponsor(user: IAccount) {
return (
[
UserType.SPONSOR_T1,
UserType.SPONSOR_T2,
UserType.SPONSOR_T3,
UserType.SPONSOR_T4,
UserType.SPONSOR_T5,
].indexOf(user.accountType) !== -1
);
}
export async function isLoggedIn(): Promise<boolean> {
try {
const userInfo = await getUserInfo();
return Boolean(userInfo);
} catch (error) {
return false;
}
}
/**
* Returns whether the current user is confirmed
*/
export async function isConfirmed(): Promise<boolean> {
try {
const response = await Account.getSelf();
const user = response.data.data;
return Boolean(user) && user.confirmed;
} catch (error) {
return false;
}
}
export async function getUserInfo(): Promise<IAccount | null> {
try {
const response = await Account.getSelf();
return response.data.data;
} catch (error) {
return null;
}
}
export async function getHackerInfo(): Promise<IHacker | null> {
try {
const response = await Hacker.getSelf();
return response.data.data;
} catch (error) {
return null;
}
}
export async function getSettings(): Promise<ISetting | null> {
try {
const response = await Settings.get();
return response.data.data;
} catch (error) {
return null;
}
}
export function isAppOpen(settings?: ISetting): boolean {
if (!settings) {
return false;
}
const now = new Date();
const open = new Date(settings.openTime);
const close = new Date(settings.closeTime);
return now > open && now < close;
}
export function isConfirmationOpen(): boolean {
return false;
}
export async function getSponsorInfo(): Promise<ISponsor | null> {
try {
const response = await Sponsor.getSelf();
return response.data.data;
} catch (error) {
return null;
}
}
export function canAccessApplication(
hacker?: { status: HackerStatus }, // Only outline as much as we need here.
settings?: ISetting
): boolean {
const status = hacker ? hacker.status : HackerStatus.HACKER_STATUS_NONE;
// If applications are open and a user has not yet sent in an app, let them do so.
// Hackers who have submitted an application AND is not waitlisted should be able to see their app.
return status === HackerStatus.HACKER_STATUS_NONE
? isAppOpen(settings)
: status !== HackerStatus.HACKER_STATUS_WAITLISTED;
}
export function canAccessTeam(hacker?: IHacker): boolean {
const status = hacker ? hacker.status : HackerStatus.HACKER_STATUS_NONE;
return (
status === HackerStatus.HACKER_STATUS_APPLIED ||
status === HackerStatus.HACKER_STATUS_ACCEPTED ||
status === HackerStatus.HACKER_STATUS_CONFIRMED ||
status === HackerStatus.HACKER_STATUS_CHECKED_IN
);
}
export function canAccessTravel(hacker?: IHacker): boolean {
const status = hacker ? hacker.status : HackerStatus.HACKER_STATUS_NONE;
if (
status === HackerStatus.HACKER_STATUS_APPLIED ||
status === HackerStatus.HACKER_STATUS_ACCEPTED ||
status === HackerStatus.HACKER_STATUS_CONFIRMED ||
status === HackerStatus.HACKER_STATUS_CHECKED_IN
) {
return !!(hacker && hacker.application && hacker.application.accommodation);
}
return false;
}
export function canAccessBus(hacker?: IHacker): boolean {
const status = hacker ? hacker.status : HackerStatus.HACKER_STATUS_NONE;
return hacker
? Boolean(hacker.travel) &&
(status === HackerStatus.HACKER_STATUS_APPLIED ||
status === HackerStatus.HACKER_STATUS_ACCEPTED ||
status === HackerStatus.HACKER_STATUS_CONFIRMED ||
status === HackerStatus.HACKER_STATUS_CHECKED_IN)
: false;
}
export function canAccessHackerPass(hacker?: IHacker): boolean {
const status = hacker ? hacker.status : HackerStatus.HACKER_STATUS_NONE;
return (
status === HackerStatus.HACKER_STATUS_ACCEPTED ||
status === HackerStatus.HACKER_STATUS_CONFIRMED ||
status === HackerStatus.HACKER_STATUS_WITHDRAWN ||
status === HackerStatus.HACKER_STATUS_CHECKED_IN
);
}
/**
* Generate a QR code for a given hacker.
* @param hacker The hacker you wanna generate the code for
* @returns an svg string.
*/
export async function generateHackerQRCode(hacker: IHacker): Promise<string> {
const hackerPage = `
${window.location.protocol}//${window.location.hostname}${
window.location.port ? ':' + window.location.port : ''
}${FrontendRoute.VIEW_HACKER_PAGE.replace(':id', hacker.id)}`;
const response = await QRCode.toDataURL(hackerPage, { scale: 10 });
return response;
}
/**
* Generate a QR code for a given hacker.
* @param hacker The hacker you wanna generate the code for
* @returns an svg string.
*/
export async function generateHackPass(
account: IAccount,
hacker: IHacker
): Promise<jsPDF> {
// Initialize label sized document
const [height, width] = [21, 36];
const doc = new jsPDF({
orientation: 'landscape',
unit: 'mm',
format: [height, width],
});
// Load custom fonts
addBrownFont(doc);
addBrownBoldFont(doc);
await addHindFont(doc);
/*doc.setDrawColor(0)
doc.setFillColor(230)
doc.rect(0, 9.5, 22, 40, 'F')
let writeCenteredText = (text: string, y: number) => {
var textWidth = doc.getStringUnitWidth(text) * doc.internal.getFontSize() / doc.internal.scaleFactor;
var textOffset = (doc.internal.pageSize.width - textWidth) / 2;
doc.text(textOffset, y, text);
}
// Write the hacker's name and pronouns
doc.setFont('brown', 'bold');
doc.setFontSize(4);
writeCenteredText(`${account.firstName} ${account.lastName}`, 22.5);
doc.setFontSize(3);
writeCenteredText(account.accountType.toUpperCase(), 2);
doc.setFont('hind', 'normal');
// Write the school the hacker is from
doc.setFont('hind', 'normal');
doc.setFontSize(3);
doc.setTextColor(0);
doc.setDrawColor(200);
doc.setLineWidth(0.1);
doc.line(4, 23.2, 17, 23.2);
doc.line(4, 26.7, 17, 26.7);
doc.setFontSize(3);
writeCenteredText(account.pronoun, 24.5);
writeCenteredText(hacker.application.general.school, 26);
writeCenteredText('McHacks 2020', 34.5);
const qrData = await generateHackerQRCode(hacker);
doc.addImage(qrData, 'png', 4, 2.5, 13, 13);*/
// Add red bar for design purposes
/*doc.setDrawColor(0)
doc.setFillColor(242, 70, 58)
doc.rect(0, 0, 36, 9, 'F')*/
// Write the hacker's name and pronouns
// doc.setFont('brown', 'bold');
// doc.setFontSize(5);
// doc.setTextColor(0);
// doc.text(`${account.firstName} ${account.lastName}`, 3, 6);
// doc.setFont('hind', 'normal');
// doc.setFontSize(4);
// doc.setTextColor(0);
// doc.text('', 3, 8);
// doc.setFont('hind', 'normal');
// doc.setFontSize(3);
// doc.setTextColor(0);
// doc.text(account.pronoun, 3, 8);
// // Write the school the hacker is from
// doc.setFont('hind', 'normal');
// doc.setFontSize(4);
// doc.setTextColor(0);
// doc.text(account.accountType, 3, 13.5);
// doc.text(hacker.application.general.school, 3, 15.8);
// const qrData = await generateHackerQRCode(hacker);
// doc.addImage(qrData, 'png', 20, 3.5, 14, 14);
// Write the hacker's name and pronouns
const writeCenteredText = (text: string, y: number) => {
const xOffset = (width - doc.getTextWidth(text)) / 2;
doc.text(text, xOffset, y);
};
doc.setFont('brown', 'bold');
doc.setFontSize(10);
doc.setTextColor(0);
writeCenteredText(account.firstName, 5);
writeCenteredText(account.lastName, 8.5);
// Write pronouns
doc.setFontSize(5.5);
doc.setFont('brown', 'normal');
if (account.pronoun) {
doc.text('Hacker', 8, 15);
doc.text(account.pronoun.join(", "), 8, 17);
} else {
doc.text('Hacker', 8, 16);
}
// Draw QR code
const qrData = await generateHackerQRCode(hacker);
doc.addImage(qrData, 'png', width - 16, 11, 9, 9);
// Draw lines
doc.setLineWidth(0.15);
doc.line(4, 1, 4, height - 1);
doc.line(4, height / 3, 1.5, height / 3);
doc.line(4, (2 * height) / 3, 1.5, (2 * height) / 3);
doc.line(width - 4, 1, width - 4, height - 1);
doc.line(width - 4, height / 3, width - 1.5, height / 3);
doc.line(width - 4, (2 * height) / 3, width - 1.5, (2 * height) / 3);
doc.line(10, 10.5, width - 10, 10.5);
// Write days
doc.setFontSize(2);
doc.text('SUN', 1.5, 2);
doc.text('SAT', width - 2.6, 2);
// Draw letters
doc.setFontSize(4);
doc.text('B', 1.5, 5);
doc.text('L', 1.5, 11.25);
doc.text('L', width - 2.5, 5);
doc.text('D', width - 2.5, 11.25);
doc.text('S', width - 2.5, 17.75);
doc.autoPrint();
doc.save(`hackPass_${hacker.id}.pdf`);
return doc;
}