import { NavLink, withRouter } from 'react-router-dom' import { Button, Image, Modal, Navbar, ButtonToolbar, Dropdown, Glyphicon, MenuItem, Overlay, Tooltip } from 'react-bootstrap'; import React, { Component } from 'react'; import DoTweet from './DoTweet'; import Search from './Search'; import { limitLength, limitAddressLength } from '../utils'; import Spinner from 'react-spinkit'; import FieldGroup from './FieldGroup'; import imgAvatar from '../../img/avatar-default.png'; /** * Class representing the header of the page that handles * commone functions such as navigation, searching of users, * link to create account, and modal to tweet * * @extends React.Component */ class Header extends Component { //#region Constructor constructor(props, context) { super(props, context); this.state = { showModal: false, showTooltip: false }; } //#endregion //#region Component events /** * Hides the tweet modal */ _handleClose() { this.setState({ showModal: false }); } /** * Shows the tweet modal */ _handleShow() { this.setState({ showModal: true }); } /** * Toggles the current account address tooltip */ _handleToggle() { this.setState({ showTooltip: !this.state.showTooltip }); } /** * Handles the event when the user selects a different account from * the dropdown * @param {Event} e - the DOM event fired when the account was changed */ _handleAcctChange(e) { if (e.target.tagName !== 'A') e.target = e.target.parentElement; web3.eth.defaultAccount = e.target.attributes.value.value; this.props.onAfterUserUpdate(); if (e.target.attributes.username.value) { this.props.history.push('/update/@' + e.target.attributes.username.value); } else { this.props.history.push('/create'); } } /** * Formats an ethereum balance for display * @param {*} balance to be formatted */ _formatBalance(balance) { return 'Ξ' + limitLength( parseFloat( balance ).toFixed(4), 6, '', true ); } //#endregion //#region React lifecycle events render() { const { picture, username, description } = this.props.user; const isEditable = Boolean(username); const isError = this.props.error && this.props.error.message; const isLoading = !Boolean(this.props.account) && !isError; const tooltipProps = { container: this, target: this.tooltipTarget, show: this.state.showTooltip }; let navClasses = []; if (isError) navClasses.push('error'); if (!isEditable) navClasses.push('logged-out'); // generate the menuitems for the accounts to populate // the accounts dropdown const accts = this.props.userAccounts.map((userAccount, index) => { const isCurrUser = userAccount.address === this.props.account; const hasUser = Boolean(userAccount.user.username); return this._handleAcctChange(e, key)} > {hasUser ? {userAccount.user.username} {userAccount.user.username} : {limitAddressLength(userAccount.address, 4)} } {this._formatBalance(userAccount.balance)} }); let states = {}; // state when we are waiting for the App component to finish loading // the current account (address) from web3.eth.getAccounts() states.isLoading = ; states.isError = ERROR!; // state when our account has loaded, and it was determined that that // account (address) has not been mapped to an owner/user in the contract // (This happens in the App component) states.isNotEditable = this._handleToggle(e)} onMouseLeave={(e) => this._handleToggle(e)} className='username' ref={(span) => this.tooltipTarget = span} >{limitAddressLength(this.props.account, 4)} {this._formatBalance(this.props.balance)} {this.props.account} ; // state when our account has loaded, and it was determined that the // account (address) has been mapped to an owner/user in the contract // (This happens in the App component) states.isEditable = {username} {username} {this._formatBalance(this.props.balance)} ; // state for showing the tweet button and associated modal states.tweet = this._handleClose(e)}> New tweet this._handleClose()}> ; return ( dTwitter embark by Status
{isLoading ? states.isLoading : isError ? states.isError : {isEditable ? states.isEditable : states.isNotEditable } {accts} {isEditable ? states.tweet : ''} }
); } //#endregion } export default withRouter(Header)