Repository URL to install this package:
|
Version:
4.0.0-alpha.2 ▾
|
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import commonMessages from '@doodle/common-messages';
import UserMenu from '../UserMenu';
import Button, { buttonVariants } from '../../controls/Button/Button';
import { translate } from '../../utils/translate';
/**
* The HeaderWidget (or user area), is a component representing the current user
* state. It changes appearance depending on whether there's a logged-in user or
* not. If a user is logged in, it displays the `UserMenu` for that user. If no
* user is logged in, it displays a sign up and a log in button.
*
* It's an independent component that can be used anywhere but is mainly
* intended to be placed inside an instance of the `Header` component (usually
* on the right-hand side).
*
* This previously lived in the lib-users-api-connector but was moved here.
*/
class HeaderWidget extends Component {
static propTypes = {
isLoggedIn: PropTypes.bool.isRequired,
onClickLogin: PropTypes.func.isRequired,
onClickLogout: PropTypes.func.isRequired,
onClickSignup: PropTypes.func.isRequired,
username: PropTypes.string,
userAvatar: PropTypes.string,
userMenuItems: PropTypes.array,
shouldHideAuthDialog: PropTypes.bool,
buttonVariant: PropTypes.oneOf(buttonVariants),
loginSignupButtonsVariant: PropTypes.oneOf(buttonVariants),
hideName: PropTypes.bool,
intl: PropTypes.object,
};
static defaultProps = {
username: null,
userAvatar: null,
userMenuItems: [],
shouldHideAuthDialog: false,
buttonVariant: 'linkDark',
loginSignupButtonsVariant: 'whiteWithBorder',
hideName: false,
intl: null,
};
render() {
const {
isLoggedIn,
onClickLogin,
onClickLogout,
onClickSignup,
username,
userAvatar,
userMenuItems,
shouldHideAuthDialog,
buttonVariant,
loginSignupButtonsVariant,
hideName,
intl,
} = this.props;
const elementsArray = [];
if (isLoggedIn) {
elementsArray.push(
<UserMenu
key="user-menu"
name={hideName ? null : username}
avatar={userAvatar}
onClickLogout={onClickLogout}
customItems={userMenuItems}
variant={buttonVariant}
/>
);
} else if (!shouldHideAuthDialog) {
elementsArray.push(
<Button
className="u-padRight"
variant={loginSignupButtonsVariant}
dimension="compact"
onClick={onClickSignup}
key="signup-button"
>
{translate(commonMessages.signup, intl)}
</Button>,
<Button variant={loginSignupButtonsVariant} dimension="compact" onClick={onClickLogin} key="login-button">
{translate(commonMessages.login, intl)}
</Button>
);
}
return elementsArray;
}
}
export default HeaderWidget;