Commit bf958a48 authored by insert's avatar insert

Save user info to context.

parent e5badedb
import React, { useState, Fragment } from 'react';
import axios from 'axios';
import React, { useState, Fragment, createContext } from 'react';
import Login from './pages/Login';
import { Typography, CssBaseline, Container, makeStyles } from '@material-ui/core';
import { fetchUserInfo, UserInfo } from './api/info';
export const useStyles = makeStyles(theme => (
export const UserContext = createContext<UserInfo>({} as any),
useStyles = makeStyles(theme => (
{
'@global': {
body: {
......@@ -34,12 +35,14 @@ export const useStyles = makeStyles(theme => (
const App = () => {
const classes = useStyles();
let [ userInfo, setUserInfo ] = useState<UserInfo>({} as any);
let [ status, setStatus ] = useState(0);
if (status === 0) {
setStatus(1);
axios.get('https://edupack.insrt.uk/api/info/@me')
fetchUserInfo()
.then(res => {
setUserInfo(res);
setStatus(3);
})
.catch(err => setStatus(2));
......@@ -64,7 +67,15 @@ const App = () => {
}
return (
<h1>app!</h1>
<UserContext.Provider value={userInfo}>
<Container component="main" maxWidth="xs">
<div className={classes.paper}>
<Typography component="h1" variant="h5">
Hello, {userInfo.givenName}!
</Typography>
</div>
</Container>
</UserContext.Provider>
);
}
......
import axios from 'axios';
export interface UserInfo {
givenName: string,
name: string,
email: string,
}
export async function fetchUserInfo(): Promise<UserInfo> {
return (await axios.get('https://edupack.insrt.uk/api/info/@me')).data;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment