-
Notifications
You must be signed in to change notification settings - Fork 0
/
Header.jsx
47 lines (44 loc) · 1.5 KB
/
Header.jsx
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
import { useDispatch, useSelector } from 'react-redux';
import { Link, useNavigate } from 'react-router-dom';
import { clearCart } from '../features/cart/cartSlice';
import { logoutUser } from '../features/user/userSlice';
import { useQueryClient } from '@tanstack/react-query';
const Header = () => {
const navigate = useNavigate();
const dispatch = useDispatch();
const queryClient = useQueryClient();
const user = useSelector((state) => state.userState.user);
const handleLogout = () => {
navigate('/');
dispatch(clearCart());
dispatch(logoutUser());
queryClient.removeQueries();
};
return (
<header className='bg-neutral py-2 text-neutral-content'>
<div className='align-element flex justify-center sm:justify-end'>
{user ? (
<div className='flex gap-x-2 sm:gap-x-8 items-center'>
<p className='text-xs sm:text-sm'>Hello, {user.username}</p>
<button
className='btn btn-xs btn-outline btn-primary'
onClick={handleLogout}
>
logout
</button>
</div>
) : (
<div className='flex gap-x-6 justify-center items-center'>
<Link to='/login' className='link link-hover text-xs sm:text-sm'>
Sign in / Guest
</Link>
<Link to='/register' className='link link-hover text-xs sm:text-sm'>
Create Account
</Link>
</div>
)}
</div>
</header>
);
};
export default Header;