@@ -10,6 +10,7 @@ import { SEO } from '../../../components';
10
10
11
11
const Student = ( props ) => {
12
12
const getStudentsApi = `${ process . env . GATSBY_WORKSHOPCHALLENGE_API_ENDPOINT } /api/students` ;
13
+ const getCustomerApi = `${ process . env . GATSBY_WORKSHOPCHALLENGE_API_ENDPOINT } /api/customers` ;
13
14
const [ students , setstudents ] = useState ( [ ] ) ;
14
15
const [ error , setError ] = useState ( '' ) ;
15
16
const arr = [ ] ;
@@ -20,7 +21,7 @@ const Student = (props) => {
20
21
const getToken = ( ) => {
21
22
AuthService . login ( ) . then (
22
23
( ) => {
23
- getStudents ( AuthService . getCurrentUser ( ) . accessToken ) ;
24
+ getCustomers ( AuthService . getCurrentUser ( ) . accessToken ) ;
24
25
} ,
25
26
( err ) => {
26
27
console . log ( 'Error: ' , err ) ;
@@ -31,18 +32,46 @@ const Student = (props) => {
31
32
) ;
32
33
} ;
33
34
34
- const getStudents = ( token ) => {
35
+ // get the details from customers table where lastEmailSent = 'credentials' or 'expiring'
36
+ // this customer data will have a studentId
37
+ // Make getStudents API call by studentId and display the username and password.
38
+ const getCustomers = ( token ) => {
35
39
axios ( {
36
40
method : 'GET' ,
37
- url : getStudentsApi ,
41
+ url : getCustomerApi ,
38
42
headers : { 'x-access-token' : token } ,
39
43
} )
40
44
. then ( ( response ) => {
41
- // Map created
42
- response . data . forEach ( ( student ) => {
43
- // Check is student is assigned
44
- if ( student . assigned ) arr . push ( { ...student } ) ;
45
- } ) ;
45
+ if ( response . data . length ) {
46
+ response . data . forEach ( ( item ) => {
47
+ if (
48
+ item . lastEmailSent === 'credentials' ||
49
+ item . lastEmailSent === 'expiring'
50
+ ) {
51
+ getStudents ( token , item . studentId ) ;
52
+ }
53
+ } ) ;
54
+ }
55
+ } )
56
+ . catch ( ( err ) => {
57
+ if ( err . response . status === 401 ) {
58
+ AuthService . login ( ) . then ( ( ) => getToken ( ) ) ;
59
+ } else {
60
+ setError (
61
+ 'Oops..something went wrong. The HPE Developer team is addressing the problem. Please try again later!' ,
62
+ ) ;
63
+ }
64
+ } ) ;
65
+ } ;
66
+
67
+ const getStudents = ( token , studentId ) => {
68
+ axios ( {
69
+ method : 'GET' ,
70
+ url : `${ getStudentsApi } /${ studentId } ` ,
71
+ headers : { 'x-access-token' : token } ,
72
+ } )
73
+ . then ( ( response ) => {
74
+ arr . push ( { ...response . data } ) ;
46
75
if ( arr . length <= 0 )
47
76
setError ( 'There are currently no active students. Stay tuned!' ) ;
48
77
setstudents ( arr ) ;
@@ -51,7 +80,6 @@ const Student = (props) => {
51
80
if ( err . response . status === 401 ) {
52
81
AuthService . login ( ) . then ( ( ) => getToken ( ) ) ;
53
82
} else {
54
- console . log ( 'catch error' , err ) ;
55
83
setError (
56
84
'Oops..something went wrong. The HPE Developer team is addressing the problem. Please try again later!' ,
57
85
) ;
0 commit comments