1
- import { useState , useEffect } from 'react' ;
2
- import axios from 'axios' ;
1
+ import React from 'react'
2
+ import { useSession } from 'next-auth/react'
3
+ import { useRouter } from 'next/router'
4
+ import {
5
+ LinkedButton ,
6
+ Loading ,
7
+ Notice ,
8
+ RequestList ,
9
+ } from '@scientist-softserv/webstore-component-library'
10
+ import {
11
+ buttonBg ,
12
+ configureErrors ,
13
+ requestListBg ,
14
+ useDefaultWare ,
15
+ useAllRequests
16
+ } from '../../utils'
3
17
4
- const useAllRequests = ( accessToken ) => {
5
- const [ requests , setRequests ] = useState ( [ ] ) ;
6
- const [ isLoadingAllRequests , setIsLoadingAllRequests ] = useState ( false ) ;
7
- const [ isAllRequestsError , setIsAllRequestsError ] = useState ( false ) ;
18
+ const Requests = ( ) => {
19
+ const router = useRouter ( )
20
+ const { data : session } = useSession ( )
21
+ const { requests, isLoadingAllRequests, isAllRequestsError } = useAllRequests ( session ?. accessToken )
22
+ const { defaultWareID, isLoadingDefaultWare, isDefaultWareError } = useDefaultWare ( session ?. accessToken )
23
+ const isError = isAllRequestsError || isDefaultWareError
24
+ const isLoading = isLoadingAllRequests || isLoadingDefaultWare
8
25
9
- useEffect ( ( ) => {
10
- setIsLoadingAllRequests ( true ) ;
11
- axios . get ( '/api/requests' , { headers : { Authorization : `Bearer ${ accessToken } ` } } )
12
- . then ( response => {
13
- setRequests ( response . data ) ;
14
- setIsLoadingAllRequests ( false ) ;
15
- } )
16
- . catch ( error => {
17
- setIsAllRequestsError ( true ) ; // intentionally throwing an error
18
- setIsLoadingAllRequests ( false ) ;
19
- } ) ;
20
- } , [ accessToken ] ) ;
26
+ // Check whether the user is authenticated first. If it does, we can return the API errors if applicable.
21
27
22
- return { requests, isLoadingAllRequests, isAllRequestsError } ;
23
- } ;
28
+ if ( isLoading ) return < Loading wrapperClass = 'mt-5' />
24
29
25
- export default useAllRequests ;
30
+ if ( ! session ) {
31
+ return (
32
+ < Notice
33
+ addClass = 'my-5'
34
+ alert = { {
35
+ body : [ 'Please log in to make new requests or view existing ones.' ] ,
36
+ title : 'Unauthorized' ,
37
+ variant : 'info'
38
+ } }
39
+ dismissible = { false }
40
+ />
41
+ )
42
+ }
43
+
44
+ if ( isError ) {
45
+ return (
46
+ < Notice
47
+ addClass = 'my-5'
48
+ alert = { configureErrors ( [ isAllRequestsError , isDefaultWareError ] ) }
49
+ dismissible = { false }
50
+ withBackButton = { true }
51
+ buttonProps = { {
52
+ onClick : ( ) => router . back ( ) ,
53
+ text : 'Click to return to the previous page.' ,
54
+ } }
55
+ />
56
+ )
57
+ }
58
+
59
+ return (
60
+ < div className = 'container' >
61
+ < div className = 'text-end d-block mt-4 mb-2' >
62
+ < LinkedButton
63
+ buttonProps = { {
64
+ backgroundColor : buttonBg ,
65
+ label : 'Initiate a New Request' ,
66
+ size : 'large' ,
67
+ } }
68
+ path = { { pathname : `/requests/new/make-a-request` , query : { id : defaultWareID } } }
69
+ />
70
+ </ div >
71
+ < RequestList backgroundColor = { requestListBg } requests = { requests } />
72
+ </ div >
73
+ )
74
+ }
75
+
76
+ export default Requests
0 commit comments