aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/Login.tsx
blob: 1858c48d8c85c6947975eaeaaaa42b8498eea238 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React from "react";
import { Link, useNavigate } from "react-router-dom";

import { ExitIcon, UserIcon, LoginIcon } from "@images/Images";
import { UserProfile } from "@customTypes/Profile";
import { API } from "@api/Api";
import "@css/Login.css";

interface LoginProps {
  setToken: React.Dispatch<React.SetStateAction<string | undefined>>;
  profile?: UserProfile;
  setProfile: React.Dispatch<React.SetStateAction<UserProfile | undefined>>;
}

const Login: React.FC<LoginProps> = ({ setToken, profile, setProfile }) => {
  const navigate = useNavigate();

  const _login = () => {
    window.location.href = "/api/v1/login";
  };

  const _logout = () => {
    setProfile(undefined);
    setToken(undefined);
    API.delete_token();
    navigate("/");
  };

  return (
    <>
      {profile ? (
        <>
          {profile.profile ? (
            <>
              <Link to="/profile" tabIndex={-1} className="login">
                <button className="sidebar-button">
                  <img
                    className="avatar-img"
                    src={profile.avatar_link}
                    alt=""
                  />
                  <span>{profile.user_name}</span>
                </button>
                <button className="logout-button" onClick={_logout}>
                  <img src={ExitIcon} alt="" />
                  <span />
                </button>
              </Link>
            </>
          ) : (
            <>
              <Link to="/" tabIndex={-1} className="login">
                <button className="sidebar-button">
                  <img
                    className="avatar-img"
                    src={profile.avatar_link}
                    alt=""
                  />
                  <span>Loading Profile...</span>
                </button>
                <button disabled className="logout-button" onClick={_logout}>
                  <img src={ExitIcon} alt="" />
                  <span />
                </button>
              </Link>
            </>
          )}
        </>
      ) : (
        <Link to="/api/v1/login" tabIndex={-1} className="login">
          <button className="sidebar-button" onClick={_login}>
            <img className="avatar-img" src={UserIcon} alt="" />
            <span>
              <img src={LoginIcon} alt="Sign in through Steam" />
            </span>
          </button>
        </Link>
      )}
    </>
  );
};

export default Login;