2018-10-25 04:07:10 +02:00
|
|
|
const html = require('choo/html');
|
2018-11-12 20:13:31 +01:00
|
|
|
const Component = require('choo/component');
|
2018-10-25 04:07:10 +02:00
|
|
|
|
2018-11-12 20:13:31 +01: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] = {};
|
2019-05-03 19:10:56 +02:00
|
|
|
this.buttonClass = '';
|
2019-05-28 23:54:58 +02:00
|
|
|
this.setLocal();
|
2018-11-12 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
avatarClick(event) {
|
2018-10-25 04:07:10 +02:00
|
|
|
event.preventDefault();
|
|
|
|
const menu = document.getElementById('accountMenu');
|
|
|
|
menu.classList.toggle('invisible');
|
|
|
|
menu.focus();
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:13:31 +01:00
|
|
|
hideMenu(event) {
|
2018-10-25 04:07:10 +02:00
|
|
|
event.stopPropagation();
|
|
|
|
const menu = document.getElementById('accountMenu');
|
|
|
|
menu.classList.add('invisible');
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:13:31 +01:00
|
|
|
login(event) {
|
2018-10-25 04:07:10 +02:00
|
|
|
event.preventDefault();
|
2019-02-12 20:50:06 +01:00
|
|
|
this.emit('signup-cta', 'button');
|
2018-10-25 04:07:10 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 20:13:31 +01:00
|
|
|
logout(event) {
|
2018-10-25 04:07:10 +02:00
|
|
|
event.preventDefault();
|
2018-11-12 20:13:31 +01:00
|
|
|
this.emit('logout');
|
|
|
|
}
|
|
|
|
|
|
|
|
changed() {
|
|
|
|
return this.local.loggedIn !== this.state.user.loggedIn;
|
|
|
|
}
|
|
|
|
|
2019-05-28 23:54:58 +02:00
|
|
|
setLocal() {
|
2018-11-12 20:13:31 +01:00
|
|
|
const changed = this.changed();
|
|
|
|
if (changed) {
|
|
|
|
this.local.loggedIn = this.state.user.loggedIn;
|
|
|
|
}
|
|
|
|
return changed;
|
2018-10-25 04:07:10 +02:00
|
|
|
}
|
2018-11-12 20:13:31 +01:00
|
|
|
|
|
|
|
update() {
|
2019-05-28 23:54:58 +02:00
|
|
|
return this.setLocal();
|
2018-11-12 20:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createElement() {
|
|
|
|
if (!this.enabled) {
|
2018-11-16 19:01:53 +01:00
|
|
|
return html`
|
2020-07-25 03:11:50 +02:00
|
|
|
<send-account></send-account>
|
2018-11-16 19:01:53 +01:00
|
|
|
`;
|
2018-11-12 20:13:31 +01:00
|
|
|
}
|
|
|
|
const user = this.state.user;
|
|
|
|
const translate = this.state.translate;
|
2019-05-28 23:54:58 +02:00
|
|
|
this.setLocal();
|
2020-07-25 03:11:50 +02:00
|
|
|
if (user.loginRequired && !this.local.loggedIn) {
|
|
|
|
return html`
|
|
|
|
<send-account></send-account>
|
|
|
|
`;
|
|
|
|
}
|
2018-11-12 20:13:31 +01:00
|
|
|
if (!this.local.loggedIn) {
|
|
|
|
return html`
|
2019-01-11 01:22:40 +01:00
|
|
|
<send-account>
|
2018-11-16 19:01:53 +01:00
|
|
|
<button
|
2019-09-09 19:34:55 +02:00
|
|
|
class="px-4 py-2 md:px-8 md:py-4 focus:outline signin border-2 link-blue border-blue-60 hover:border-blue-70 dark:border-blue-40 dark:hover:border-blue-50"
|
2018-11-16 19:01:53 +01:00
|
|
|
onclick="${e => this.login(e)}"
|
2019-05-01 18:39:29 +02:00
|
|
|
title="${translate('signInOnlyButton')}"
|
2018-11-16 19:01:53 +01:00
|
|
|
>
|
2019-05-01 18:39:29 +02:00
|
|
|
${translate('signInOnlyButton')}
|
2018-11-16 19:01:53 +01:00
|
|
|
</button>
|
2019-01-11 01:22:40 +01:00
|
|
|
</send-account>
|
2018-11-12 20:13:31 +01:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
return html`
|
2019-01-11 01:22:40 +01:00
|
|
|
<send-account class="relative h-8">
|
2018-11-12 20:13:31 +01:00
|
|
|
<input
|
|
|
|
type="image"
|
|
|
|
alt="${user.email}"
|
2019-09-09 19:34:55 +02:00
|
|
|
class="w-8 h-8 rounded-full border text-blue-50 md:text-white focus:outline"
|
2018-11-12 20:13:31 +01:00
|
|
|
src="${user.avatar}"
|
|
|
|
onclick="${e => this.avatarClick(e)}"
|
|
|
|
/>
|
|
|
|
<ul
|
|
|
|
id="accountMenu"
|
2019-09-09 19:34:55 +02:00
|
|
|
class="invisible absolute top-0 right-0 mt-10 pt-2 pb-2 bg-white shadow-md whitespace-no-wrap outline-none z-50 dark:bg-grey-80"
|
2018-11-12 20:13:31 +01:00
|
|
|
onblur="${e => this.hideMenu(e)}"
|
|
|
|
>
|
2019-09-09 19:34:55 +02:00
|
|
|
<li class="p-2 text-grey-60 dark:text-grey-50">${user.email}</li>
|
2018-11-12 20:13:31 +01:00
|
|
|
<li>
|
2019-02-21 01:11:54 +01:00
|
|
|
<button
|
2019-09-09 19:34:55 +02:00
|
|
|
class="block w-full text-left px-4 py-2 text-grey-80 dark:text-grey-30 hover:bg-blue-50 hover:text-white cursor-pointer focus:outline"
|
2018-11-12 20:13:31 +01:00
|
|
|
onclick="${e => this.logout(e)}"
|
2019-03-04 23:13:18 +01:00
|
|
|
title="${translate('signOut')}"
|
2018-11-12 20:13:31 +01:00
|
|
|
>
|
2019-03-04 23:13:18 +01:00
|
|
|
${translate('signOut')}
|
2019-02-21 01:11:54 +01:00
|
|
|
</button>
|
2018-11-12 20:13:31 +01:00
|
|
|
</li>
|
|
|
|
</ul>
|
2019-01-11 01:22:40 +01:00
|
|
|
</send-account>
|
2018-11-12 20:13:31 +01:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Account;
|