import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { keyCodes, bytesToSize } from './utils'; import editorUtilities from '../utils/editor-utilties'; const siteRoot = window.app.config.siteRoot; const gettext = window.gettext; class Account extends Component { constructor(props) { super(props); this.state = { showInfo: false, userName: '', contactEmail: '', quotaUsage: '', quotaTotal: '', isStaff: false, usageRate: '', avatarURL: '', } } componentDidMount(){ this.getAccountInfo(); } componentDidUpdate(prevProps) { this.handleProps(); } getContainer = () => { return ReactDOM.findDOMNode(this); } handleProps = () => { if (this.state.showInfo) { this.addEvents(); } else { this.removeEvents(); } } addEvents = () => { ['click', 'touchstart', 'keyup'].forEach(event => document.addEventListener(event, this.handleDocumentClick, true) ); } removeEvents = () => { ['click', 'touchstart', 'keyup'].forEach(event => document.removeEventListener(event, this.handleDocumentClick, true) ); } handleDocumentClick = (e) => { if (e && (e.which === 3 || (e.type === 'keyup' && e.which !== keyCodes.tab))) return; const container = this.getContainer(); if (container.contains(e.target) && container !== e.target && (e.type !== 'keyup' || e.which === keyCodes.tab)) { return; } this.setState({ showInfo: !this.state.showInfo, }) } onClickAccount = () => { this.setState({ showInfo: !this.state.showInfo, }) } getAccountInfo = () => { editorUtilities.getAccountInfo().then(resp => { this.setState({ userName: resp.data.name, contactEmail: resp.data.email, usageRate: resp.data.space_usage, quotaUsage: bytesToSize(resp.data.usage), quotaTotal: bytesToSize(resp.data.total), isStaff: resp.data.is_staff, avatarURL: resp.data.avatar_url }) }) } renderMenu = () => { if(this.state.isStaff){ return ( {gettext("System Admin")} ) } } renderAvatar = () => { if (this.state.avatarURL) { return ( ) } return ( ) } render() { return (
{this.renderAvatar()}
{this.state.userName}

{gettext("Used")}: {this.state.quotaUsage} / {this.state.quotaTotal}

{gettext("Settings")} {this.renderMenu()} {gettext("Log out")}
) } } export default Account;