Advertisement
fabiobiondi

React Pro - Real World App - Ch7. 11. Logout

Mar 17th, 2023
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // shared/components/core/NavBar.tsx
  2. import { useAuth } from '@/services/auth';
  3. import { NavLink, useNavigate } from 'react-router-dom';
  4. import logo from '../../../assets/laptop.png';
  5. import { selectCartIsEmpty, selectTotalCartItems, useCart, useCartPanel } from '@/services/cart';
  6. import { CartPanel } from './CartPanel';
  7.  
  8. const isActive = (obj: { isActive: boolean }) => {
  9.   return obj.isActive ? 'text-xl text-sky-400 font-bold' : 'text-xl text-white'
  10. }
  11.  
  12. export function NavBar() {
  13.   const navigate = useNavigate();
  14.   const logout = useAuth(state => state.logout);
  15.   const isCartPanelOpened = useCartPanel(state => state.open);
  16.   const toggleCartPanel = useCartPanel(state => state.toggle);
  17.   const totalCartItems = useCart(selectTotalCartItems);
  18.   const isEmpty = useCart(selectCartIsEmpty);
  19.  
  20.   function logoutHandler() {
  21.     logout();
  22.     navigate('/login')
  23.   }
  24.  
  25.   return (
  26.     <div className="fixed top-0 left-0 right-0 shadow-2xl z-10">
  27.       <div className="bg-slate-900 flex justify-between items-center h-20 text-white p-3">
  28.  
  29.         {/*Logo*/}
  30.         <div className="flex items-center gap-3">
  31.           <img src={logo} alt="my logo" className="w-16"/>
  32.           <NavLink to="shop" className={isActive}>SHOP</NavLink>
  33.         </div>
  34.  
  35.         {/*Cart button badge*/}
  36.         <div>
  37.           <button disabled={isEmpty} className="btn accent lg" onClick={toggleCartPanel}>
  38.             Cart: {totalCartItems}
  39.           </button>
  40.         </div>
  41.  
  42.         {/*Cart Panel*/}
  43.         { isCartPanelOpened && <CartPanel/>}
  44.  
  45.  
  46.         {/*actions button*/}
  47.         <div className="fixed bottom-2 right-2 p-5">
  48.           <NavLink to="login" className="btn accent lg">login</NavLink>
  49.           <NavLink to="cms" className="btn accent lg">cms</NavLink>
  50.           <button onClick={logoutHandler} className="btn primary lg">logout</button>
  51.         </div>
  52.  
  53.       </div>
  54.     </div>
  55.   )
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement