const html = require('choo/html');
const Component = require('choo/component');
class Account extends Component {
constructor(name, state, emit) {
super(name);
this.state = state;
this.emit = emit;
this.enabled = state.capabilities.account;
this.local = state.components[name] = {};
this.buttonClass = '';
this.setLocal();
}
avatarClick(event) {
event.preventDefault();
const menu = document.getElementById('accountMenu');
menu.classList.toggle('invisible');
menu.focus();
}
hideMenu(event) {
event.stopPropagation();
const menu = document.getElementById('accountMenu');
menu.classList.add('invisible');
}
login(event) {
event.preventDefault();
this.emit('signup-cta', 'button');
}
logout(event) {
event.preventDefault();
this.emit('logout');
}
changed() {
return this.local.loggedIn !== this.state.user.loggedIn;
}
setLocal() {
const changed = this.changed();
if (changed) {
this.local.loggedIn = this.state.user.loggedIn;
}
return changed;
}
update() {
return this.setLocal();
}
createElement() {
if (!this.enabled) {
return html`
`;
}
const user = this.state.user;
const translate = this.state.translate;
this.setLocal();
if (user.loginRequired && !this.local.loggedIn) {
return html`
`;
}
if (!this.local.loggedIn) {
return html`
`;
}
return html`
- ${user.email}
-
`;
}
}
module.exports = Account;