account.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. <send-account></send-account>
  49. `;
  50. }
  51. const user = this.state.user;
  52. const translate = this.state.translate;
  53. this.setLocal();
  54. if (user.loginRequired && !this.local.loggedIn) {
  55. return html`
  56. <send-account></send-account>
  57. `;
  58. }
  59. if (!this.local.loggedIn) {
  60. return html`
  61. <send-account>
  62. <button
  63. class="px-4 py-2 md:px-8 md:py-4 focus:outline signin border-2 link-primary border-primary hover:border-primary dark:border-primary dark:hover:border-primary"
  64. onclick="${e => this.login(e)}"
  65. title="${translate('signInOnlyButton')}"
  66. >
  67. ${translate('signInOnlyButton')}
  68. </button>
  69. </send-account>
  70. `;
  71. }
  72. return html`
  73. <send-account class="relative h-8">
  74. <input
  75. type="image"
  76. alt="${user.email}"
  77. class="w-8 h-8 rounded-full border-default text-primary md:text-white focus:outline"
  78. src="${user.avatar}"
  79. onclick="${e => this.avatarClick(e)}"
  80. />
  81. <ul
  82. id="accountMenu"
  83. class="invisible absolute top-0 right-0 mt-10 pt-2 pb-2 bg-white shadow-md whitespace-nowrap outline-none z-50 dark:bg-grey-80"
  84. onblur="${e => this.hideMenu(e)}"
  85. >
  86. <li class="p-2 text-grey-60 dark:text-grey-50">${user.email}</li>
  87. <li>
  88. <button
  89. class="block w-full text-left px-4 py-2 text-grey-80 dark:text-grey-30 hover:bg-primary hover:text-white cursor-pointer focus:outline"
  90. onclick="${e => this.logout(e)}"
  91. title="${translate('signOut')}"
  92. >
  93. ${translate('signOut')}
  94. </button>
  95. </li>
  96. </ul>
  97. </send-account>
  98. `;
  99. }
  100. }
  101. module.exports = Account;