1- import _ from 'lodash' ;
2- import axios from 'axios' ;
1+ import { Redirect } from 'react-router-dom' ;
32import React , { Component } from 'react' ;
43import ReactTable from 'react-table' ;
5- import Cookies from 'universal-cookie' ;
6- import config from 'config/environment' ;
74import MENTOR_REQUEST_COLUMNS from 'shared/constants/table' ;
5+ import * as ApiHelpers from 'shared/utils/apiHelper' ;
86import Heading from 'shared/components/heading/heading' ;
97
108class MentorRequestsTable extends Component {
119 state = {
12- requests : [ ]
10+ requests : [ ] ,
11+ loggedIn : true
1312 }
1413
1514 componentDidMount ( ) {
16- const cookies = new Cookies ( ) ;
17- const token = cookies . get ( 'token' ) ;
18- axios . get ( `${ config . backendUrl } /requests` ,
19- { headers : { Authorization : `bearer ${ token } ` } }
20- ) . then ( ( { data } ) => {
15+ ApiHelpers . getRequests ( )
16+ . then ( ( data ) => {
2117 this . setState ( { requests : data } ) ;
22- } ) . catch ( ( response ) => {
23- const error = _ . get ( response , 'response.data.error' ) ;
24- this . setState ( { error } ) ;
25- } ) ;
18+ } ) . catch ( this . setFetchError ) ;
19+ }
20+
21+ setFetchError = ( { response } ) => {
22+ // The 500 means you the user is not a mentor, should
23+ // update that later
24+ if ( response . status === 401 || response . status === 500 ) {
25+ this . setState ( { loggedIn : false } ) ;
26+ }
2627 }
2728
2829 rowClickHandler = ( state , rowInfo ) => ( {
@@ -32,7 +33,10 @@ class MentorRequestsTable extends Component {
3233 } )
3334
3435 render ( ) {
35- const { requests } = this . state ;
36+ const { loggedIn, requests } = this . state ;
37+ if ( ! loggedIn ) {
38+ return < Redirect to = "/login" /> ;
39+ }
3640 return (
3741 < div style = { { width : '100%' } } >
3842 < Heading text = "Pending Requests" style = { { margin : '4rem auto' , lineHeight : 0 } } />
0 commit comments