-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathHeader.js
More file actions
68 lines (61 loc) · 1.7 KB
/
Copy pathHeader.js
File metadata and controls
68 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import styled from 'styled-components';
import logo from 'url:../img/logo.svg';
import { useQuery } from '@apollo/client';
import { Link, withRouter } from 'react-router-dom';
import ButtonAsLink from './ButtonAsLink';
import { IS_LOGGED_IN } from '../gql/query';
const HeaderBar = styled.header`
width: 100%;
padding: 0.5em 1em;
display: flex;
height: 64px;
position: fixed;
align-items: center;
background-color: #fff;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.25);
z-index: 1;
`;
const LogoText = styled.h1`
margin: 0;
padding: 0;
display: inline;
`;
const UserState = styled.div`
margin-left: auto;
`;
const Header = props => {
// query hook for user logged in state
const { data, client } = useQuery(IS_LOGGED_IN);
return (
<HeaderBar>
<img src={logo} alt="Notedly Logo" height="40" />
<LogoText>Notedly</LogoText>
{/* If logged in display a log out link, else display sign in options */}
<UserState>
{data.isLoggedIn ? (
<ButtonAsLink
onClick={() => {
// remove the token
localStorage.removeItem('token');
// clear the application's cache
client.resetStore();
// update local state
client.writeData({ data: { isLoggedIn: false } });
// redirect the user to the homepage
props.history.push('/');
}}
>
Logout
</ButtonAsLink>
) : (
<p>
<Link to={'/signin'}>Sign In</Link> or{' '}
<Link to={'/signup'}>Sign Up</Link>
</p>
)}
</UserState>
</HeaderBar>
);
};
export default withRouter(Header);