index.jsx 9.14 KB
Newer Older
1
import React from 'react';
Iain Maitland's avatar
Iain Maitland committed
2
import PropTypes from 'prop-types';
3
import { browserHistory } from 'react-router';
4 5 6 7 8 9 10 11
import { Link } from 'react-router';
import { connect } from 'react-redux';
import Icon from 'app/components/elements/Icon';
import resolveRoute from 'app/ResolveRoute';
import tt from 'counterpart';
import { APP_NAME } from 'app/client_config';
import SearchInput from 'app/components/elements/SearchInput';
import IconButton from 'app/components/elements/IconButton';
12
import DropdownMenu from 'app/components/elements/DropdownMenu';
13 14 15 16 17 18 19 20 21
import * as userActions from 'app/redux/UserReducer';
import * as appActions from 'app/redux/AppReducer';
import Userpic from 'app/components/elements/Userpic';
import { SIGNUP_URL } from 'shared/constants';
import SteemLogo from 'app/components/elements/SteemLogo';
import normalizeProfile from 'app/utils/NormalizeProfile';

class Header extends React.Component {
    static propTypes = {
Iain Maitland's avatar
Iain Maitland committed
22 23 24 25 26
        current_account_name: PropTypes.string,
        account_meta: PropTypes.object,
        category: PropTypes.string,
        order: PropTypes.string,
        pathname: PropTypes.string,
27 28 29 30 31 32
    };

    constructor() {
        super();
    }

33 34 35 36 37 38 39 40 41
    componentDidUpdate(prevProps) {
        const { loggedIn } = this.props;
        if (prevProps.loggedIn && !loggedIn) {
            if (process.env.BROWSER) {
                browserHistory.replace(`/`);
            }
        }
    }

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    render() {
        const {
            category,
            order,
            pathname,
            current_account_name,
            username,
            showLogin,
            logout,
            loggedIn,
            vertical,
            nightmodeEnabled,
            toggleNightmode,
            userPath,
            showSidePanel,
            navigate,
58
            account_meta,
59 60 61 62
        } = this.props;

        /*Set the document.title on each header render.*/
        const route = resolveRoute(pathname);
63
        const home_account = false;
64 65
        let page_title = route.page;

66
        const topic = '';
67
        let page_name = null;
Roger Jungemann's avatar
Roger Jungemann committed
68
        if (route.page === 'WalletIndex') {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        } else if (route.page == 'Privacy') {
            page_title = tt('navigation.privacy_policy');
        } else if (route.page == 'Tos') {
            page_title = tt('navigation.terms_of_service');
        } else if (route.page == 'ChangePassword') {
            page_title = tt('header_jsx.change_account_password');
        } else if (route.page == 'CreateAccount') {
            page_title = tt('header_jsx.create_account');
        } else if (route.page == 'Approval') {
            page_title = `Account Confirmation`;
        } else if (
            route.page == 'RecoverAccountStep1' ||
            route.page == 'RecoverAccountStep2'
        ) {
            page_title = tt('header_jsx.stolen_account_recovery');
84 85
        } else if (route.page === 'Proposals') {
            page_title = tt('header_jsx.steem_proposals');
86
        } else if (route.page === 'UserProfile') {
87
            const user_name = route.params[0].slice(1);
roadscape's avatar
roadscape committed
88 89
            const name = account_meta
                ? normalizeProfile(account_meta.toJS()).name
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                : null;
            const user_title = name ? `${name} (@${user_name})` : user_name;
            page_title = user_title;
            if (route.params[1] === 'curation-rewards') {
                page_title = tt('header_jsx.curation_rewards_by', {
                    username: user_title,
                });
            }
            if (route.params[1] === 'author-rewards') {
                page_title = tt('header_jsx.author_rewards_by', {
                    username: user_title,
                });
            }
        } else {
            page_name = ''; //page_title = route.page.replace( /([a-z])([A-Z])/g, '$1 $2' ).toLowerCase();
        }

        // Format first letter of all titles and lowercase user name
        if (route.page !== 'UserProfile') {
            page_title =
                page_title.charAt(0).toUpperCase() + page_title.slice(1);
        }

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        const wallet_link = `/@${username}/transfers`;
        const reset_password_link = `/@${username}/password`;
        const settings_link = `/@${username}/settings`;
        const pathCheck = userPath === '/submit.html' ? true : null;

        const user_menu = [
            {
                link: wallet_link,
                icon: 'wallet',
                value: tt('g.wallet'),
            },
            {
                link: '#',
                icon: 'eye',
                onClick: toggleNightmode,
                value: tt('g.toggle_nightmode'),
            },
            {
                link: reset_password_link,
                icon: 'key',
                value: tt('g.change_password'),
            },
            { link: settings_link, icon: 'cog', value: tt('g.settings') },
            loggedIn
                ? {
                      link: '#',
                      icon: 'enter',
                      onClick: logout,
                      value: tt('g.logout'),
                  }
                : { link: '#', onClick: showLogin, value: tt('g.login') },
        ];

146 147 148
        return (
            <header className="Header">
                <nav className="row Header__nav">
149
                    <div className="small-5 large-6 columns Header__logotype">
150
                        {/*LOGO*/}
Roger Jungemann's avatar
Roger Jungemann committed
151
                        <Link to="/">
152 153 154 155
                            <SteemLogo />
                        </Link>
                    </div>

156
                    <div className="small-7 large-6 columns Header__buttons">
Roger Jungemann's avatar
Roger Jungemann committed
157
                        {/*NOT LOGGED IN SIGN UP LINK*/}
158 159
                        {!loggedIn && (
                            <span className="Header__user-signup show-for-medium">
160 161 162 163 164 165 166
                                <a
                                    className="Header__login-link"
                                    href="/login.html"
                                    onClick={showLogin}
                                >
                                    {tt('g.login')}
                                </a>
167 168 169 170 171 172 173 174
                                <a
                                    className="Header__signup-link"
                                    href={SIGNUP_URL}
                                >
                                    {tt('g.sign_up')}
                                </a>
                            </span>
                        )}
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                        {/*USER AVATAR */}
                        {loggedIn && (
                            <DropdownMenu
                                className={'Header__usermenu'}
                                items={user_menu}
                                title={username}
                                el="span"
                                selected={tt('g.rewards')}
                                position="left"
                            >
                                <li className={'Header__userpic '}>
                                    <span title={username}>
                                        <Userpic account={username} />
                                    </span>
                                </li>
                            </DropdownMenu>
                        )}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
                        {/*HAMBURGER*/}
                        <span
                            onClick={showSidePanel}
                            className="toggle-menu Header__hamburger"
                        >
                            <span className="hamburger" />
                        </span>
                    </div>
                </nav>
            </header>
        );
    }
}

export { Header as _Header_ };

const mapStateToProps = (state, ownProps) => {
    // SSR code split.
    if (!process.env.BROWSER) {
        return {
            username: null,
            loggedIn: false,
        };
    }

roadscape's avatar
roadscape committed
217 218 219 220 221 222 223 224 225
    let user_profile;
    const route = resolveRoute(ownProps.pathname);
    if (route.page === 'UserProfile') {
        user_profile = state.global.getIn([
            'accounts',
            route.params[0].slice(1),
        ]);
    }

Roger Jungemann's avatar
Roger Jungemann committed
226
    // TODO: Cleanup
227 228 229 230 231 232 233 234 235 236 237 238
    const userPath = state.routing.locationBeforeTransitions.pathname;
    const username = state.user.getIn(['current', 'username']);
    const loggedIn = !!username;
    const current_account_name = username
        ? username
        : state.offchain.get('account');

    return {
        username,
        loggedIn,
        userPath,
        nightmodeEnabled: state.user.getIn(['user_preferences', 'nightmode']),
roadscape's avatar
roadscape committed
239
        account_meta: user_profile,
240 241 242 243 244 245 246 247
        current_account_name,
        ...ownProps,
    };
};

const mapDispatchToProps = dispatch => ({
    showLogin: e => {
        if (e) e.preventDefault();
Roger Jungemann's avatar
Roger Jungemann committed
248
        dispatch(userActions.showLogin({ type: 'basic' }));
249 250 251
    },
    logout: e => {
        if (e) e.preventDefault();
Roger Jungemann's avatar
Roger Jungemann committed
252
        dispatch(userActions.logout({ type: 'default' }));
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    },
    toggleNightmode: e => {
        if (e) e.preventDefault();
        dispatch(appActions.toggleNightmode());
    },
    showSidePanel: () => {
        dispatch(userActions.showSidePanel());
    },
    hideSidePanel: () => {
        dispatch(userActions.hideSidePanel());
    },
});

const connectedHeader = connect(mapStateToProps, mapDispatchToProps)(Header);

export default connectedHeader;