UserMenu.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div>
  3. <Button
  4. v-if="isSignedIn"
  5. class="border-none px-6! py-2.75"
  6. variant="text"
  7. aria-haspopup="true"
  8. aria-controls="user_menu"
  9. @click="toggleMenu"
  10. >
  11. <span class="font-semibold">
  12. {{ user.username }}
  13. </span>
  14. <Icon
  15. name="ph:user-circle-fill"
  16. size="1.25rem"
  17. />
  18. </Button>
  19. <Button
  20. v-else
  21. class="border-none px-6! py-2.75"
  22. variant="text"
  23. @click="showSignInDialog"
  24. >
  25. <span class="font-semibold">Sign In</span>
  26. <Icon
  27. name="ph:user-circle-light"
  28. size="1.25rem"
  29. />
  30. </Button>
  31. <Menu
  32. class="mt-0"
  33. id="user_menu"
  34. :model="model"
  35. :popup="true"
  36. ref="menu"
  37. >
  38. <template #item="{ item, props }">
  39. <!--suppress HtmlUnknownTarget -->
  40. <NuxtLink
  41. v-if="item.route"
  42. v-slot="{ href, navigate }"
  43. :to="item.route"
  44. custom
  45. >
  46. <a
  47. :href="href"
  48. v-bind="props.action"
  49. @click="navigate"
  50. >
  51. {{ item.label }}
  52. </a>
  53. </NuxtLink>
  54. <a
  55. v-else
  56. :href="item?.url"
  57. :target="item?.target"
  58. v-bind="props.action"
  59. >
  60. {{ item.label }}
  61. </a>
  62. </template>
  63. </Menu>
  64. </div>
  65. </template>
  66. <script setup>
  67. // const SignInDialog = defineLazyHydrationComponent(
  68. // "visible",
  69. // () => import("~/components/SignInDialog.vue")
  70. // )
  71. // const ChangePasswordDialog = defineLazyHydrationComponent(
  72. // "visible",
  73. // () => import("~/components/ChangePasswordDialog.vue")
  74. // )
  75. const { isSignedIn, signOut, user } = useAuthClient()
  76. // const dialog = useDialog()
  77. // const toast = useToast()
  78. const menu = useTemplateRef("menu")
  79. const model = computed(() => {
  80. const menu = [
  81. { label: "Change Password", command: showChangePasswordDialog },
  82. { label: "Sign Out", command: doSignOut }
  83. ]
  84. // noinspection JSUnresolvedReference
  85. if (isSignedIn.value && user.value.role === "admin") {
  86. menu.unshift({
  87. label: "Dashboard",
  88. command: async () => await navigateTo({ name: "admin:users" })
  89. })
  90. }
  91. return menu
  92. })
  93. async function doSignOut() {
  94. await signOut()
  95. closeMenu()
  96. // toast.add({
  97. // severity: "success",
  98. // summary: "Signed Out.",
  99. // detail: "You've been signed out.",
  100. // life: 3000
  101. // })
  102. }
  103. function toggleMenu(event) {
  104. menu.value.toggle(event)
  105. }
  106. function closeMenu(_) {
  107. menu.value.hide()
  108. }
  109. function showSignInDialog() {
  110. closeMenu()
  111. // useOpenDialog(dialog, SignInDialog)
  112. }
  113. function showChangePasswordDialog() {
  114. closeMenu()
  115. // useOpenDialog(dialog, ChangePasswordDialog)
  116. }
  117. </script>
  118. <style scoped></style>