2018-10-25 02:07:10 +00:00
|
|
|
const html = require('choo/html');
|
2018-11-12 19:13:31 +00:00
|
|
|
const Component = require('choo/component');
|
2018-10-25 02:07:10 +00:00
|
|
|
|
2018-11-12 19:13:31 +00:00
|
|
|
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.setState();
|
|
|
|
}
|
|
|
|
|
|
|
|
avatarClick(event) {
|
2018-10-25 02:07:10 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const menu = document.getElementById('accountMenu');
|
|
|
|
menu.classList.toggle('invisible');
|
|
|
|
menu.focus();
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:31 +00:00
|
|
|
hideMenu(event) {
|
2018-10-25 02:07:10 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
const menu = document.getElementById('accountMenu');
|
|
|
|
menu.classList.add('invisible');
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:31 +00:00
|
|
|
login(event) {
|
2018-10-25 02:07:10 +00:00
|
|
|
event.preventDefault();
|
2018-11-12 19:13:31 +00:00
|
|
|
this.emit('login');
|
2018-10-25 02:07:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:31 +00:00
|
|
|
logout(event) {
|
2018-10-25 02:07:10 +00:00
|
|
|
event.preventDefault();
|
2018-11-12 19:13:31 +00:00
|
|
|
this.emit('logout');
|
|
|
|
}
|
|
|
|
|
|
|
|
changed() {
|
|
|
|
return this.local.loggedIn !== this.state.user.loggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState() {
|
|
|
|
const changed = this.changed();
|
|
|
|
if (changed) {
|
|
|
|
this.local.loggedIn = this.state.user.loggedIn;
|
|
|
|
}
|
|
|
|
return changed;
|
2018-10-25 02:07:10 +00:00
|
|
|
}
|
2018-11-12 19:13:31 +00:00
|
|
|
|
|
|
|
update() {
|
|
|
|
return this.setState();
|
|
|
|
}
|
|
|
|
|
|
|
|
createElement() {
|
|
|
|
if (!this.enabled) {
|
2018-11-16 18:01:53 +00:00
|
|
|
return html`
|
|
|
|
<div></div>
|
|
|
|
`;
|
2018-11-12 19:13:31 +00:00
|
|
|
}
|
|
|
|
const user = this.state.user;
|
|
|
|
const translate = this.state.translate;
|
|
|
|
if (!this.local.loggedIn) {
|
|
|
|
return html`
|
2018-11-16 18:01:53 +00:00
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
class="p-2 border rounded border-white text-white hover:bg-white hover:text-blue md:text-blue md:border-blue md:hover:text-white md:hover:bg-blue"
|
|
|
|
onclick="${e => this.login(e)}"
|
|
|
|
>
|
|
|
|
${translate('signInMenuOption')}
|
|
|
|
</button>
|
|
|
|
</div>
|
2018-11-12 19:13:31 +00:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
return html`
|
|
|
|
<div class="relative h-8">
|
|
|
|
<input
|
|
|
|
type="image"
|
|
|
|
alt="${user.email}"
|
|
|
|
class="w-8 h-8 rounded-full text-white"
|
|
|
|
src="${user.avatar}"
|
|
|
|
onclick="${e => this.avatarClick(e)}"
|
|
|
|
/>
|
|
|
|
<ul
|
|
|
|
id="accountMenu"
|
|
|
|
class="invisible list-reset absolute pin-t pin-r mt-10 pt-2 pb-2 bg-white shadow-md whitespace-no-wrap outline-none z-50"
|
|
|
|
onblur="${e => this.hideMenu(e)}"
|
|
|
|
tabindex="-1"
|
|
|
|
>
|
|
|
|
<li class="p-2 text-grey-dark">${user.email}</li>
|
|
|
|
<li>
|
|
|
|
<a
|
|
|
|
class="block px-4 py-2 text-grey-darkest hover:bg-blue hover:text-white cursor-pointer"
|
|
|
|
onclick="${e => this.logout(e)}"
|
|
|
|
>
|
|
|
|
${translate('logOut')}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Account;
|