summaryrefslogtreecommitdiff
path: root/moneyjsx/src/header.tsx
blob: e79e25636fbee25c7f0796b81a45d7efbe043c8f (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import $ from 'jquery';
import * as JSX from './jsx';
import * as user from './user';
import * as settings from './settings';
import { Dropdown, DropdownItem, Spinner, addPopup } from './components';


async function logout() {
  await user.logout();
}

/**
  * NavItem component
  * takes in title and route, optionally "disabled"
**/
function NavItem( props: any ) {
  const url = new URL( window.location.href );
  var style = `height: 45px;`;
  var onclick = () => JSX.navigate( props.route );
  if( props.disabled || url.pathname == props.route ) {
    style += "color: #888;pointer-events: none;";
    onclick = null;
  }

  return <DropdownItem onclick={ onclick } style={ style }>
    <a>[ { props.title } ]</a>
  </DropdownItem>
}

function SettingsButton( props: any ) {
  let openSettings = settings.openPopup;
  let style = `height: 45px;`;

  if( props.disabled ) {
    openSettings = null;
    style += "color: #888; pointer-events: none;";
  }

  return <DropdownItem onclick={ openSettings } style={ style }>
    <a>[ Settings ]</a>
  </DropdownItem>
}

function EmailInput() {
  const sendLink = async () => {
    const email = $( "#login-email" ).val().toString();
    $( "#login-btn-wrapper" ).append( <Spinner /> );

    const res = await user.sendLoginLink( email );
    if( res === true ) {
      $( "#header-login-dropdown" ).replaceWith(
        <div style="padding: 5px">Success! Check your inbox for a login link.</div>
      );
    } else {
      $( "#header-login-dropdown" ).replaceWith(
        <div style="padding: 5px">Error sending email: { res }</div>
      );
    }
  };

  return <div id="header-dropdown">
    <div id="header-login-dropdown">
      Log in
      <input type="text" placeholder="email" id="login-email" style="margin-bottom: 5px" />
      <div id="login-btn-wrapper" style="display: flex; justify-content: space-between; margin-top: 5px;">
        <button onclick={ sendLink }>
          [ get code ]
        </button>
      </div>
    </div>
  </div>
}

function showLoginPopup() {
  addPopup( $( <EmailInput /> ) );
}

export function Header() {
  return <header id="header">
    <div id="header-user">
      { user.is_loggedin && <><b>User: </b><b id="username">{ user.settings.nickname }</b>&nbsp;</> }
      { user.is_loggedin && <button onclick={ logout }>[ Log out ]</button> }
      { !user.is_loggedin && <button onclick={ showLoginPopup }>[ Log in ]</button> }
    </div>
    <div id="header-ctrls">
      <a style="padding-right: 10px" onclick={ () => JSX.navigate( "/support" ) }>Site Map</a>
      <Dropdown title="≡" inline innerStyle="top: 24px; margin-left: 0" style="width: 48px" id="header-dropdown-btn">
        {
          user.is_loggedin ? <NavItem title="Terminal" route="/terminal" /> : <NavItem title="Terminal" route="/" disabled />
        }
        {
          user.is_loggedin ? <SettingsButton /> : <SettingsButton disabled />
        }
      <NavItem title="Home" route="/" />
      </Dropdown>
    </div>
  </header>
}