import { h, Component } from 'preact'; import { PROVIDER_NAMES } from 'common/constants'; export default class AuthPanel extends Component { constructor(props) { super(props); this.toggleUserId = this.toggleUserId.bind(this); this.toggleBlockedVisibility = this.toggleBlockedVisibility.bind(this); this.onSortChange = this.onSortChange.bind(this); } onSortChange(e) { if (this.props.onSortChange) { this.props.onSortChange(e.target.value); } } toggleUserId() { this.setState({ isUserIdVisible: !this.state.isUserIdVisible }); } toggleBlockedVisibility() { if (!this.state.isBlockedVisible) { if (this.props.onBlockedUsersShow) this.props.onBlockedUsersShow(); } else { if (this.props.onBlockedUsersHide) this.props.onBlockedUsersHide(); } this.setState({ isBlockedVisible: !this.state.isBlockedVisible }); } render(props, { isUserIdVisible, isBlockedVisible }) { const { user, providers = [], sort } = props; const sortArray = getSortArray(sort); return (
{ !!user.id && (
You signed in as {' '} {user.name} {isUserIdVisible && ({user.id})}. {' '} Sign out?
) } { !user.id && (
Sign in to comment using {' '} { providers.map((provider, i) => { const comma = i === 0 ? '' : (i === providers.length - 1 ? ' or ' : ', '); return ( {comma} props.onSignIn(provider)} >{PROVIDER_NAMES[provider]} ) }) } {'.'}
) }
{ user.admin && ( {isBlockedVisible ? 'Hide' : 'Show'} blocked ) } {user.admin && ' • '} Sort by {' '}
); } } function getSortArray(currentSort) { const sortArray = [ { value: '-score', label: 'Best', }, { value: '+score', label: 'Worst', }, { value: '-time', label: 'Newest', }, { value: '+time', label: 'Oldest', }, { value: '-active', label: 'Recently updated', }, { value: '+active', label: 'Least recently updated', }, ]; return sortArray.map(sort => { if (sort.value === currentSort) { sort.selected = true; } return sort; }); }