11import React , { useEffect , useState } from 'react'
2- import './Home.css'
32import config from '../config' ;
4-
5- import '../components/Info'
3+ import './Home.css'
64import InfoPanel from '../components/Info'
5+ < % if eq ( index . Params `userAuth` ) "yes" % > import Card from '../components/Card' < % end % >
76
87function Home ( ) {
98 const [ data , setData ] = useState ( {
@@ -14,32 +13,51 @@ function Home() {
1413 const [ status , setStatus ] = useState ( {
1514 code : 'Checking...' ,
1615 } )
16+ < % if eq ( index . Params `userAuth` ) "yes" % >
17+ const [ privateState , setPrivateData ] = useState ( {
18+ isLoading : true ,
19+ data : { } ,
20+ } ) ;
21+
22+ const fetchPrivateData = async ( data ) => {
23+ const uri = `${ config . backendURL } /auth/userInfo`
24+ const resp = await fetch ( uri , { credentials : "include" } ) ;
25+ return {
26+ data : await resp . json ( ) ,
27+ }
28+ } < % end % >
29+
30+ const fetchInfoPanel = async ( ) => {
31+ const resp = await fetch ( `${ config . backendURL } /status/about` , { credentials : "include" } ) ;
32+ const code = resp . status
33+ const info = await resp . json ( )
34+ return { code, info } ;
35+ } ;
36+
1737
1838useEffect ( ( ) => {
19- fetch ( `${ config . backendURL } /status/about` , { credentials : "include" } )
20- . then ( ( result ) => {
21- setStatus ( {
22- code : result . status ,
23- } )
24- return result . json ( )
25- } )
26- . then ( ( data ) => {
27- setData ( {
28- info : data ,
29- error : null ,
30- } )
31- } )
32- . catch ( ( error ) => {
33- setData ( {
34- info : { } ,
35- error : error ,
36- } )
37- } )
38- } , [ ] )
39+ fetchInfoPanel ( ) . then ( ( { code, info} ) => {
40+ setStatus ( { code } ) ;
41+ setData ( { info, error : null } )
42+ } )
43+ . catch ( ( error ) => {
44+ setData ( { info : { } , error : error } )
45+ } ) ;
46+ < % if eq ( index . Params `userAuth` ) "yes" % >
47+ fetchPrivateData ( ) . then ( setPrivateData ) ; < % end % >
48+ } , [ ] )
3949
4050 return (
4151 < main className = "Home" >
4252 < InfoPanel data = { data } status = { status } config = { config } />
53+ < % if eq ( index . Params `userAuth `) "yes " % >
54+ < Card header = "Authenticated Data" >
55+ {
56+ privateState . isLoading ?
57+ < div > Fetching data...</ div > :
58+ < pre > { JSON . stringify ( privateState . data , null , 2 ) } </ pre >
59+ }
60+ </ Card > < % end % >
4361 </ main >
4462 )
4563}
0 commit comments