account.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const html = require('choo/html');
  2. const Component = require('choo/component');
  3. class Account extends Component {
  4. constructor(name, state, emit) {
  5. super(name);
  6. this.state = state;
  7. this.emit = emit;
  8. this.enabled = state.capabilities.account;
  9. this.local = state.components[name] = {};
  10. this.buttonClass = '';
  11. this.setLocal();
  12. }
  13. avatarClick(event) {
  14. event.preventDefault();
  15. const menu = document.getElementById('accountMenu');
  16. menu.classList.toggle('invisible');
  17. menu.focus();
  18. }
  19. hideMenu(event) {
  20. event.stopPropagation();
  21. const menu = document.getElementById('accountMenu');
  22. menu.classList.add('invisible');
  23. }
  24. login(event) {
  25. event.preventDefault();
  26. this.emit('signup-cta', 'button');
  27. }
  28. logout(event) {
  29. event.preventDefault();
  30. this.emit('logout');
  31. }
  32. changed() {
  33. return this.local.loggedIn !== this.state.user.loggedIn;
  34. }
  35. setLocal() {
  36. const changed = this.changed();
  37. if (changed) {
  38. this.local.loggedIn = this.state.user.loggedIn;
  39. }
  40. return changed;
  41. }
  42. update() {
  43. return this.setLocal();
  44. }
  45. createElement() {
  46. if (!this.enabled) {
  47. return html`
  48. <div></div>
  49. `;
  50. }
  51. const user = this.state.user;
  52. const translate = this.state.translate;
  53. this.setLocal();
  54. if (!this.local.loggedIn) {
  55. return html`
  56. <send-account>
  57. <button
  58. 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"
  59. onclick="${e => this.login(e)}"
  60. title="${translate('signInOnlyButton')}"
  61. >
  62. ${translate('signInOnlyButton')}
  63. </button>
  64. </send-account>
  65. `;
  66. }
  67. return html`
  68. <send-account class="relative h-8">
  69. <input
  70. type="image"
  71. alt="${user.email}"
  72. class="w-8 h-8 rounded-full border text-blue-50 md:text-white focus:outline"
  73. src="${user.avatar}"
  74. onclick="${e => this.avatarClick(e)}"
  75. />
  76. <ul
  77. id="accountMenu"
  78. 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"
  79. onblur="${e => this.hideMenu(e)}"
  80. >
  81. <li class="p-2 text-grey-60 dark:text-grey-50">${user.email}</li>
  82. <li>
  83. <button
  84. 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"
  85. onclick="${e => this.logout(e)}"
  86. title="${translate('signOut')}"
  87. >
  88. ${translate('signOut')}
  89. </button>
  90. </li>
  91. </ul>
  92. </send-account>
  93. `;
  94. }
  95. }
  96. module.exports = Account;