|
| 1 | +/* eslint-disable max-len */ |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { Heading, Text, Box, Image, DataTable, Button } from 'grommet'; |
| 4 | +import axios from 'axios'; |
| 5 | +import PropTypes from 'prop-types'; |
| 6 | +import { Layout, ScheduleCard, CardGrid } from '../../../components/hackshack'; |
| 7 | +import { MainTitle } from '../../../components/hackshack/StyledComponents'; |
| 8 | +import AuthService from '../../../services/auth.service'; |
| 9 | +import { SEO } from '../../../components'; |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +const Student = (props) => { |
| 14 | + const getStudentsApi = `${process.env.GATSBY_WORKSHOPCHALLENGE_API_ENDPOINT}/api/students`; |
| 15 | + const [students, setstudents] = useState([]); |
| 16 | + const [error, setError] = useState(''); |
| 17 | + const arr = []; |
| 18 | + const [index, setIndex] = useState(0); |
| 19 | + const onActive = (nextIndex) => setIndex(nextIndex); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + const getToken = () => { |
| 23 | + AuthService.login().then( |
| 24 | + () => { |
| 25 | + getStudents(AuthService.getCurrentUser().accessToken); |
| 26 | + }, |
| 27 | + (err) => { |
| 28 | + console.log('Error: ', err); |
| 29 | + setError( |
| 30 | + 'Oops..something went wrong. The HPE Developer team is addressing the problem. Please try again later!', |
| 31 | + ); |
| 32 | + }, |
| 33 | + ); |
| 34 | + }; |
| 35 | + |
| 36 | + const getStudents = (token) => { |
| 37 | + axios({ |
| 38 | + method: 'GET', |
| 39 | + url: getStudentsApi, |
| 40 | + headers: { 'x-access-token': token }, |
| 41 | + }) |
| 42 | + .then((response) => { |
| 43 | + console.log('response ++++', response.data.length); |
| 44 | + // Map created |
| 45 | + response.data.forEach((student) => { |
| 46 | + // Check is student is assigned |
| 47 | + if (student.assigned) |
| 48 | + arr.push({ ...student }); |
| 49 | + }); |
| 50 | + console.log('students ++++', arr.length, arr); |
| 51 | + if (arr.length <= 0) |
| 52 | + setError( |
| 53 | + 'There are currently no active students. Stay tuned!', |
| 54 | + ); |
| 55 | + setstudents(arr); |
| 56 | + }) |
| 57 | + .catch((err) => { |
| 58 | + if (err.response.status === 401) { |
| 59 | + AuthService.login().then(() => getToken()); |
| 60 | + } else { |
| 61 | + console.log('catch error', err); |
| 62 | + setError( |
| 63 | + 'Oops..something went wrong. The HPE Developer team is addressing the problem. Please try again later!', |
| 64 | + ); |
| 65 | + } |
| 66 | + }); |
| 67 | + }; |
| 68 | + getToken(); |
| 69 | + |
| 70 | + }, []); |
| 71 | + |
| 72 | + console.log('students in students ', students); |
| 73 | + |
| 74 | + const { title, description, badgeImg } = props.pageContext; |
| 75 | + |
| 76 | + return ( |
| 77 | + <Layout background="/img/hackshack/BackgroundImages/schedule-background.png"> |
| 78 | + <SEO title={title} description={description} image={badgeImg} /> |
| 79 | + <MainTitle> |
| 80 | + <Heading color="text-strong" margin={{ top: 'none', bottom: 'small' }}> |
| 81 | + Active Participants |
| 82 | + </Heading> |
| 83 | + </MainTitle> |
| 84 | + {} |
| 85 | + {students.length >= 0 ? ( |
| 86 | + <tab> |
| 87 | + <DataTable |
| 88 | + columns={[ |
| 89 | + { |
| 90 | + property: 'username', |
| 91 | + header: <Text>Username</Text>, |
| 92 | + primary: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + property: 'password', |
| 96 | + header: <Text>Password</Text>, |
| 97 | + }, |
| 98 | + { |
| 99 | + property: 'url', |
| 100 | + header: <Text>URL</Text>, |
| 101 | + render: datum => ( |
| 102 | + <Button |
| 103 | + href= {datum.url} |
| 104 | + target= "_blank" |
| 105 | + label="Connect" |
| 106 | + />), |
| 107 | + }, |
| 108 | + ]} |
| 109 | + data={students} |
| 110 | +/* data={[ |
| 111 | + { username: 'Alan', password: '20' }, |
| 112 | + { username: 'Bryan', password: '30' }, |
| 113 | + { username: 'Chris', password: '40' }, |
| 114 | + { username: 'Eric', password: '80' }, |
| 115 | + ] } */ |
| 116 | + /> |
| 117 | + </tab> |
| 118 | + ) : ( |
| 119 | + <Box |
| 120 | + pad="small" |
| 121 | + justify="center" |
| 122 | + margin={{ top: 'medium' }} |
| 123 | + direction="column" |
| 124 | + // background="status-critical" |
| 125 | + > |
| 126 | + {error ? ( |
| 127 | + <> |
| 128 | + <Text size="large" color="status-critical" alignSelf="center"> |
| 129 | + {error} |
| 130 | + </Text> |
| 131 | + <Image |
| 132 | + alt="gremlin rockin" |
| 133 | + src="/img/hackshack/gremlin-rockin.svg" |
| 134 | + /> |
| 135 | + </> |
| 136 | + ) : ( |
| 137 | + <Box height="medium" /> |
| 138 | + )} |
| 139 | + </Box> |
| 140 | + )} |
| 141 | + </Layout> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +Student.propTypes = { |
| 146 | + pageContext: PropTypes.shape({ |
| 147 | + username: PropTypes.string, |
| 148 | + password: PropTypes.string, |
| 149 | + }), |
| 150 | +}; |
| 151 | + |
| 152 | +export default Student; |
0 commit comments