Header.tsx 3.75 KB
Newer Older
jang dong hyeok's avatar
jang dong hyeok committed
1
import React, { useEffect, useState } from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Link, useLocation, useNavigate } from "react-router-dom";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
import { useAuth } from "../auth/auth.context";
4
import { UserIcon } from "../icons";
5

Yoon, Daeki's avatar
Yoon, Daeki committed
6
7
export const Header = () => {
  const { user, logout } = useAuth();
8
  const location = useLocation();
Jiwon Yoon's avatar
Jiwon Yoon committed
9
  const navigate = useNavigate();
Yoon, Daeki's avatar
Yoon, Daeki committed
10

jang dong hyeok's avatar
jang dong hyeok committed
11
12
13
14
  const [windowSize, setWindowSize] = useState({
    width: window.innerWidth,
  });

15
16
17
18
19
20
  const [isClicked, setIsClicked] = useState(false);

  const handleHeaderClick = () => {
    setIsClicked(!isClicked);
  };

jang dong hyeok's avatar
jang dong hyeok committed
21
22
23
24
25
26
27
28
29
30
31
32
  const handleResize = () => {
    setWindowSize({
      width: window.innerWidth,
    });
  };
  useEffect(() => {
    window.addEventListener("resize", handleResize);
    return () => {
      window.removeEventListener("resize", handleResize);
    };
  }, []);

Yoon, Daeki's avatar
Yoon, Daeki committed
33
  return (
34
    <div className="bg-white border-b-2 border-b-themeColor px-2 sm:px-4 py-3.5">
jang dong hyeok's avatar
jang dong hyeok committed
35
      <div className="container flex flex-wrap place-content-center mx-auto">
Yoon, Daeki's avatar
Yoon, Daeki committed
36
37
        <Link to="/" className="font-bold text-2xl text-themeColor">
          Simple Survey Form
38
        </Link>
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

        <div className="hidden md:flex items-center justify-end md:flex-1 lg:w-0">
          {user.isLoggedIn ? (
            <div className="pt-2">
              <button
                onClick={() => logout(() => navigate("/"))}
                className="font-bold text-gray-600 hover:text-themeColor mx-1 py-2 px-3 rounded-md"
              >
                로그아웃
              </button>
              <Link to="/profile">
                <button className="font-bold text-gray-600 hover:text-themeColor mx-1 py-2 px-3 rounded-md">
                  프로필
                </button>
              </Link>
            </div>
          ) : (
            <div className="pt-2">
              <Link to="/login">
                <button className="font-bold text-gray-600 hover:text-themeColor mx-1 py-2 px-3 rounded-md">
                  로그인
                </button>
              </Link>
              <Link to="/signup">
                <button className="font-bold text-white hover:bg-blue-500 mx-1 py-2 px-3 bg-themeColor rounded-md ">
                  회원가입
65
                </button>
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
              </Link>
            </div>
          )}
        </div>

        <button
          className="flex md:hidden w-8 h-8 rounded-full absolute top-4 right-5"
          onClick={handleHeaderClick}
        >
          <UserIcon />
          {isClicked ? (
            user.isLoggedIn ? (
              <div className="fixed top-14 right-1 w-20 bg-white flex flex-col border-2 border-themeColor">
                <Link to="/login">
                  <button
                    className="text-gray-600 hover:text-themeColor"
                    onClick={() => logout(() => navigate("/"))}
                  >
                    로그아웃
                  </button>
                </Link>
jang dong hyeok's avatar
jang dong hyeok committed
87
                <Link to="/profile">
88
                  <button className="text-gray-600 hover:text-themeColor">
jang dong hyeok's avatar
jang dong hyeok committed
89
90
91
92
93
                    프로필
                  </button>
                </Link>
              </div>
            ) : (
94
              <div className="fixed top-14 right-1 w-20 bg-white flex flex-col border-2 border-themeColor">
jang dong hyeok's avatar
jang dong hyeok committed
95
                <Link to="/login">
96
97
98
99
                  <button>
                    <div className="text-gray-600 hover:text-themeColor">
                      로그인
                    </div>
jang dong hyeok's avatar
jang dong hyeok committed
100
101
102
                  </button>
                </Link>
                <Link to="/signup">
103
                  <button className="text-gray-600 hover:text-themeColor">
jang dong hyeok's avatar
jang dong hyeok committed
104
105
106
107
                    회원가입
                  </button>
                </Link>
              </div>
108
109
110
111
112
            )
          ) : (
            <></>
          )}
        </button>
113
114
      </div>
    </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
115
116
  );
};