import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import Link from 'shared/components/link';
import UserMenu from 'authentication/components/user_menu';

import './user_section.scss';

class UserSection extends PureComponent {
  static propTypes = {
    user: PropTypes.object,
    onLogin: PropTypes.func.isRequired
  };

  onLogin = event => {
    event && event.preventDefault();
    this.props.onLogin();
  };

  signInLink = () => {
    const useNewLogin = false; // TODO: control this with a feature flag
    return useNewLogin ? (
      <Link href="/register" theme="gold" onClick={this.onLogin}>
        Sign in
      </Link>
    ) : (
      <Link
        href="/register"
        theme="gold joinLink sign-in-link"
        onClick={() => {}}
      >
        Sign in
      </Link>
    );
  };
  render() {
    const {user} = this.props;

    return (
      <div className="mo-web-user-section">
        {user ? (
          <>
            <Link href="/account" theme="gold">
              HI, {user.attributes.first_name.toUpperCase()}
            </Link>
            <UserMenu />
          </>
        ) : (
          this.signInLink()
        )}
      </div>
    );
  }
}

export default UserSection;
