import Cookies from "js-cookie";
import Link from "next/link";
import Router from "next/router";

import * as cookieNames from "../lib/cookieNames";

function Navbar(): JSX.Element {
  function handleLogout(event: Event): void {
    event.preventDefault();
    Cookies.remove(cookieNames.TOKEN);
    Router.reload();
  }

  return (
    <nav>
      <ul>
        <li>
          {!!Cookies.get(cookieNames.TOKEN) ? (
            <button onClick={handleLogout as any}>Logout</button>
          ) : (
            <Link href="/login" passHref>
              <button>Login</button>
            </Link>
          )}
        </li>
      </ul>
    </nav>
  );
}

export default Navbar;
