-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathttlTraineeDashboard.tsx
516 lines (492 loc) · 17 KB
/
ttlTraineeDashboard.tsx
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* istanbul ignore file */
/* eslint-disable */
import React, { useState, useEffect, useContext } from 'react';
import { Icon } from '@iconify/react';
import { useTranslation } from 'react-i18next';
import DataTable from '../components/DataTable';
import Sidebar from '../components/Sidebar';
import useDocumentTitle from '../hook/useDocumentTitle';
import Draggable from 'react-draggable';
import Dialog from '@mui/material/Dialog';
import Paper, { PaperProps } from '@mui/material/Paper';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import { toast } from 'react-toastify';
import { useLazyQuery } from '@apollo/client';
import { GET_TTL_TRAINEES } from '../queries/user.queries';
import { GET_GITHUB_STATISTICS } from '../queries/manageStudent.queries';
import Button from '../components/Buttons';
import Avatar from '../assets/avatar.png';
import { MdCancel } from 'react-icons/md';
import Spinner from '../components/Spinner';
import GitHubActivityChart from '../components/chartGitHub';
import ViewSprintRatings from '../components/ratings/ViewWeeklyRatings';
import ViewWeeklyRatings from '../components/ratings/ViewWeeklyRatings';
import { useTraineesContext } from '../hook/useTraineesData';
import { XIcon } from '@heroicons/react/solid';
import { FaTimes } from 'react-icons/fa';
import { log } from 'console';
import { FaEye } from 'react-icons/fa';
import { handleError } from '../components/ErrorHandle';
import BulkRatingModal from '../components/BulkRatingModal';
const organizationToken = localStorage.getItem('orgToken');
``;
/* istanbul ignore next */
const TtlTraineeDashboard = () => {
useDocumentTitle('Trainees');
const { t }: any = useTranslation();
function PaperComponent(props: PaperProps) {
return (
<Draggable
handle="#draggable-dialog-title"
cancel={'[class*="MuiDialogContent-root"]'}
>
<Paper {...props} />
</Draggable>
);
}
// const { traineeData, setTraineeData } = useTraineesContext();
const [traineeData, setTraineeData] = useState<any[]>([]);
const [traineeLoading, setTraineeLoading] = useState<boolean>(true);
const [nav, setNav] = useState(false);
const [fetchError, setFetchError] = useState(false); // New state variable to track the fetch error
const [SprintRating, setSprintRating] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
const [traineeDetails, setTraineeDetails] = useState<any>({});
const [gitHubStatistics, setGitHubStatistics] = useState<any>({});
const [hasData, setHasData] = useState(false);
const [open, setOpen] = React.useState(false);
const [open2, setOpen2] = React.useState(false);
const [traineesWithAttendance, setTraineesWithAttendance] = useState<any[]>(
[],
);
const traineeAttendancOrTickets = (
email: string,
type: 'attendance' | 'tickets',
) => {
if (traineesWithAttendance) {
for (let i = 0; i < traineesWithAttendance.length; i++) {
if (traineesWithAttendance[i].traineeInfo.email === email) {
return type === 'attendance'
? traineesWithAttendance[i].attendance
: traineesWithAttendance[i].numOfTickets;
}
}
}
return 0;
};
const [selectedTraineeId, setSelectedTraineeId] = useState<string[]>();
const [bulkRateModal, setBulkRateModal] = useState<boolean>(false);
const handleClickOpen2 = async () => {
setIsLoaded(true);
setOpen2(true);
};
const handleClose2 = () => {
setOpen2(false);
setOpen(false);
};
const handleClick = () => setNav(!nav);
const [getGitHubStatistics] = useLazyQuery(GET_GITHUB_STATISTICS, {
onCompleted: (data) => {
setGitHubStatistics(data.gitHubActivity);
setIsLoaded(false);
},
onError: (error) => {
setIsLoaded(false);
},
});
const handleSprintRating = async (rowData: any) => {
const filteredUser = traineeData.filter(
(item: { email: any }) => item.email == rowData,
);
setTraineeDetails(filteredUser[0]);
setSprintRating(true);
};
const handleCloseSprintRating = () => {
setSprintRating(false); // Close the modal
};
const handleClickOpen = async (rowData: any) => {
setIsLoaded(true);
const filteredUser = traineeData.filter(
(item: { email: any }) => item.email == rowData,
);
setTraineeDetails(filteredUser[0]);
setOpen(true);
getGitHubStatistics({
variables: {
organisation: localStorage.getItem('orgName')?.split('.')[0],
username: filteredUser[0].profile?.githubUsername,
},
});
};
/* istanbul ignore next */
const columns = [
{ Header: t('name'), accessor: 'name' },
{ Header: t('email'), accessor: 'email' },
{ Header: t('Team'), accessor: 'team' },
{
Header: t('Attendance'),
accessor: '',
Cell: ({ row }: any) =>
hasData && (
<div className="flex ml-[2.5rem] items-center">
<div>
{traineeAttendancOrTickets(row.original?.email, 'attendance')}
</div>
</div>
),
},
{
Header: t('Tickets'),
accessor: '',
Cell: ({ row }: any) =>
hasData && (
<div className="flex ml-[1.5rem] items-center">
<div>
{traineeAttendancOrTickets(row.original?.email, 'tickets')}
</div>
</div>
),
},
{
Header: t('Status'),
accessor: 'status',
Cell: ({ row }: any) => {
return (
<div
className={
'font-serif items-center' +
(traineeData?.length > 0 ? ' flex' : ' hidden')
}
>
<button
className={`${
row.original?.Status?.status === 'drop'
? 'bg-gray-500'
: 'bg-black'
} text-white rounded-xl px-3`}
onClick={() => {
setSelectedTraineeId(row.original?.email);
handleClickOpen2();
}}
>
{row.original?.Status?.status === 'drop' ? 'Dropped' : 'View'}
</button>
</div>
);
},
},
{
Header: t('action'),
accessor: '',
Cell: ({ row }: any) =>
hasData && ( // Only render the button if there is data
<div className="flex ml-5 items-center">
<button
onClick={() => {
handleClickOpen(row.original?.email);
}}
className="text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-600"
aria-label="View Details"
>
<FaEye className="text-2xl text-[#9e85f5]" />
</button>
</div>
),
},
];
let datum: any = [];
const [getTTLTrainees] = useLazyQuery(GET_TTL_TRAINEES, {
variables: {
orgToken: organizationToken,
},
});
const handleClose = () => {
setOpen(false);
};
/* istanbul ignore if */
if (traineeData && traineeData.length > 0) {
traineeData?.map((data: any, index: number) => {
datum[index] = {};
datum[index].name = data.profile ? data.profile.name : 'undefined';
datum[index].email = data.email;
datum[index].rating = '2';
datum[index].team = data.team?.name;
datum[index].program = data.team?.cohort?.program?.name;
});
}
/* istanbul ignore next */
useEffect(() => {
getTTLTrainees({
fetchPolicy: 'network-only',
onCompleted: (data) => {
console.log(data);
const traineeDatas = data.getTTLTrainees.map(
(traineeData: any) => traineeData.traineeInfo,
);
setTraineesWithAttendance(data.getTTLTrainees);
setTraineeLoading(false);
setTraineeData(traineeDatas);
setHasData(traineeDatas.length > 0);
setFetchError(false); // Reset the fetch error state on success
},
onError: (error) => {
setTraineeLoading(false);
setFetchError(true);
toast.error(handleError(error));
},
});
}, []);
return (
<>
{/* Get Trainee user details */}
<div className="rounded-lg dark:bg-dark-bg font-serif">
<Dialog
open={open2}
onClose={handleClose2}
PaperComponent={PaperComponent}
aria-labelledby="draggable-dialog-title"
className="rounded-lg relative"
fullWidth
>
{traineeData.map((data) => {
if (data.email === selectedTraineeId) {
return (
<ViewWeeklyRatings
traineeName={data?.profile?.name || 'Unknown Name'}
traineeEmail={data?.email || 'Unknown Email'}
traineeId={data?.profile?.user?.id || 'Unknown ID'}
traineeCohort={data?.team?.cohort?.id || 'Unknown Cohort'}
traineeStatus={
data?.profile?.user?.status || 'Status Unavailable'
}
/>
);
}
})}
<FaTimes
size={24}
color="red"
className="absolute right-5 top-2 cursor-pointer"
onClick={() => handleClose2()}
/>
</Dialog>
<Dialog
open={open}
onClose={handleClose}
PaperComponent={PaperComponent}
aria-labelledby="draggable-dialog-title"
className="rounded-lg font-serif"
fullWidth
>
<DialogContent className="font-serif dark:bg-dark-bg">
<DialogContentText className="font-serif dark:bg-dark-bg">
<div className="font-serif text-sm font-bold text-center dark:text-white dark:bg-dark-bg">
<div className="bg-[#4aa5be] h-[150px]">
<img
className="absolute top-[80px] left-[40px] border-4 border-white font-sans"
style={{
margin: '0 auto',
borderRadius: '50%',
marginBottom: '20px',
width: '150px',
height: '150px',
}}
src={
traineeData &&
traineeDetails.profile &&
traineeDetails.profile.avatar
? traineeDetails.profile.avatar
: Avatar
}
alt="Logo"
/>
</div>
<h2
className="font-bold text-[18px] capitalize pt-5 dark:text-white text-right dark:bg-dark-bg text-sm font-serif"
style={{ cursor: 'move', fontWeight: 'bold' }}
id="draggable-dialog-title"
>
{traineeDetails && traineeDetails.profile
? traineeDetails.profile.name
: 'Unavailable'}
</h2>
<div
className="font-serif text-sm"
style={{
display: 'flex',
gap: '50px',
justifyContent: 'space-between',
paddingBlock: '10px',
marginTop: '30px',
borderBottom: '0.5px solid #EAECEE',
}}
>
{' '}
<h3>
<b>EMAIL</b>{' '}
</h3>
<p>
<i>
{' '}
{traineeDetails.email
? traineeDetails.email
: 'Unavailable'}
</i>
</p>
</div>
<div
className="font-serif text-sm"
style={{
display: 'flex',
gap: '50px',
justifyContent: 'space-between',
paddingBlock: '10px',
marginTop: '30px',
borderBottom: '0.5px solid #EAECEE',
}}
>
{' '}
<h3>
<b>Github username</b>{' '}
</h3>
<p>
<i>
{' '}
{traineeDetails.profile
? traineeDetails.profile?.githubUsername
: 'Unavailable'}
</i>
</p>
</div>
<div
className="font-serif text-sm"
style={{
display: 'flex',
gap: '50px',
justifyContent: 'space-between',
paddingBlock: '10px',
marginBottom: '20px',
borderBottom: '0.5px solid #EAECEE',
}}
>
{' '}
<h3>
<b>COHORT</b>{' '}
</h3>
<p>
<i>
{' '}
{traineeDetails.team
? traineeDetails.team.cohort.name
: 'Unavailable'}
</i>
</p>
</div>
<div
className={'text-sm font-serif'}
style={{
display: 'flex',
gap: '50px',
justifyContent: 'space-between',
paddingBlock: '10px',
marginBottom: '20px',
}}
>
{isLoaded ? (
<p>
<div className="flex items-center justify-center h-48">
<i>Loading gitHub statistics...</i>
<Spinner />
<div className="spinner" />
</div>
</p>
) : (
<div
className={
traineeDetails?.profile &&
traineeDetails?.profile?.githubUsername
? 'flex'
: 'hidden '
}
>
<div className="flex flex-col">
<i className="text-2xl ">
{gitHubStatistics?.totalCommits} total commits
</i>
</div>
<div className="flex flex-col">
<div>
{traineeDetails?.profile &&
traineeDetails?.profile?.githubUsername ? (
<GitHubActivityChart data={gitHubStatistics} />
) : (
<></>
)}
</div>
</div>
</div>
)}
</div>
<Button
data-testid="removeInviteModel"
variant="info"
size="sm"
style="w-[20%] md:w-1/4 text-sm font-serif"
onClick={() => handleClose()}
>
{t('Close')}
</Button>
</div>
</DialogContentText>
</DialogContent>
</Dialog>
</div>
<div className="flex flex-col h-screen">
<div className="flex flex-row">
{/* <Sidebar toggle={handleClick} style="hidden lg:flex" /> */}
<div className="w-full">
<div>
<div className="min-h-screen overflow-x-hidden overflow-y-auto bg-light-bg dark:bg-dark-frame-bg">
<div className="">
<div className="my-5">
<Button
variant="primary"
size="lg"
data-testid="registerModel"
style="m-0"
onClick={() => setBulkRateModal(true)}
>
{t('Bulk Rate')}
</Button>
</div>
{fetchError || traineeData?.length === 0 ? ( // Check both fetchError and traineeData length
<DataTable
data={[]} // Pass an empty array as data
columns={columns}
loading={traineeLoading}
title={t('Trainees list')}
/>
) : (
<DataTable
data={traineeData?.length > 0 ? datum : [{}]}
columns={columns}
loading={traineeLoading}
title={t('Trainees list')}
/>
)}
</div>
</div>
{bulkRateModal ? (
<BulkRatingModal setBulkRateModal={setBulkRateModal} />
) : (
''
)}
</div>
</div>
</div>
</div>
</>
);
};
export default TtlTraineeDashboard;