LocChat/client/components/home/_home.jsx

37 lines
848 B
React
Raw Normal View History

2021-11-23 16:04:12 -05:00
import { useContext, useEffect, useState } from 'react';
import { ApiContext } from '../../utils/api_context';
import { AuthContext } from '../../utils/auth_context';
2021-11-22 16:21:53 -05:00
2021-11-20 20:18:58 -05:00
export const Home = () => {
2021-11-23 16:04:12 -05:00
const [, setAuthToken] = useContext(AuthContext);
const api = useContext(ApiContext);
const [loading, setLoading] = useState(true);
const [user, setUser] = useState(null);
useEffect(async () => {
const res = await api.get('/users/me');
setUser(res.user);
setLoading(false);
}, []);
2021-11-22 16:21:53 -05:00
const logout = async () => {
2021-11-23 16:04:12 -05:00
const res = await api.del('/sessions');
if (res.success) {
setAuthToken(null);
2021-11-22 16:21:53 -05:00
}
};
2021-11-23 16:04:12 -05:00
if (loading) {
return <div>Loading...</div>;
}
2021-11-22 16:21:53 -05:00
return (
<div>
2021-11-23 16:04:12 -05:00
<h1>Welcome {user.name}</h1>
2021-11-22 16:21:53 -05:00
<button type="button" onClick={logout}>
Logout
</button>
</div>
);
2021-11-20 20:18:58 -05:00
};