nav menu refactor + auth check
This commit is contained in:
parent
bdcfe16122
commit
f8c72c622f
2 changed files with 56 additions and 15 deletions
|
|
@ -1,22 +1,37 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { h, onMounted, ref, watch } from 'vue'
|
import { computed, h, onMounted, ref, watch } from 'vue'
|
||||||
import { NMenu, NButton, NDrawer, NIcon } from 'naive-ui'
|
import { NMenu, NButton, NDrawer, NIcon } from 'naive-ui'
|
||||||
import MenuIcon from '@/components/Navbar/MenuIcon.vue'
|
import MenuIcon from '@/components/Navbar/MenuIcon.vue'
|
||||||
import { RouterLink, useRoute } from 'vue-router'
|
import { RouterLink, useRoute } from 'vue-router'
|
||||||
|
import { useAuthStore } from '@/stores/authStore'
|
||||||
|
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
const { isAuthenticated } = authStore
|
||||||
|
|
||||||
const showMobileMenu = ref(false)
|
const showMobileMenu = ref(false)
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const activeKey = ref('collection')
|
const activeKey = ref('collection')
|
||||||
|
|
||||||
const menuOptions = [
|
const mainMenu = computed(() => {
|
||||||
{ label: 'Collection', key: 'collection' },
|
return [
|
||||||
{ label: 'Charts', key: 'charts' },
|
{ label: 'Collection', key: 'collection' },
|
||||||
{ label: 'Parsers', key: 'parsers' },
|
{ label: 'Charts', key: 'charts' },
|
||||||
{ label: 'Personal', key: 'personal' },
|
{ label: 'Parsers', key: 'parsers' },
|
||||||
{ label: 'Login', key: 'login' },
|
]
|
||||||
]
|
})
|
||||||
|
|
||||||
|
const authMenu = computed(() => {
|
||||||
|
if (!isAuthenticated) {
|
||||||
|
return [
|
||||||
|
{ label: 'Login', key: 'login' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{ label: 'Personal', key: 'personal' },
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
const windowWidth = ref(window.innerWidth)
|
const windowWidth = ref(window.innerWidth)
|
||||||
|
|
||||||
|
|
@ -51,16 +66,29 @@ const renderLabel = (option) => {
|
||||||
<div class="logo">Merch tracker</div>
|
<div class="logo">Merch tracker</div>
|
||||||
|
|
||||||
<n-menu
|
<n-menu
|
||||||
class="menu-desktop"
|
class="main-menu"
|
||||||
:options="menuOptions"
|
:options="mainMenu"
|
||||||
mode="horizontal"
|
mode="horizontal"
|
||||||
:collapsed="false"
|
:collapsed="false"
|
||||||
:value="activeKey"
|
:value="activeKey"
|
||||||
@update:value="activeKey = $event"
|
@update:value="activeKey = $event"
|
||||||
v-if="windowWidth > 768"
|
v-if="isAuthenticated && windowWidth > 768"
|
||||||
:render-label="renderLabel"
|
:render-label="renderLabel"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<div class="auth-menu-container">
|
||||||
|
<n-menu
|
||||||
|
class="auth-menu"
|
||||||
|
:options="authMenu"
|
||||||
|
mode="horizontal"
|
||||||
|
:collapsed="false"
|
||||||
|
:value="activeKey"
|
||||||
|
@update:value="activeKey = $event"
|
||||||
|
v-if="windowWidth > 768"
|
||||||
|
:render-label="renderLabel"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<n-button
|
<n-button
|
||||||
class="menu-mobile-button"
|
class="menu-mobile-button"
|
||||||
@click="showMobileMenu = !showMobileMenu"
|
@click="showMobileMenu = !showMobileMenu"
|
||||||
|
|
@ -72,7 +100,7 @@ const renderLabel = (option) => {
|
||||||
|
|
||||||
<n-drawer v-model:show="showMobileMenu" placement="right">
|
<n-drawer v-model:show="showMobileMenu" placement="right">
|
||||||
<n-menu
|
<n-menu
|
||||||
:options="menuOptions"
|
:options="isAuthenticated ? [...mainMenu, ...authMenu] : authMenu"
|
||||||
mode="vertical"
|
mode="vertical"
|
||||||
:value="activeKey"
|
:value="activeKey"
|
||||||
@update:value="activeKey = $event; showMobileMenu = false"
|
@update:value="activeKey = $event; showMobileMenu = false"
|
||||||
|
|
@ -98,12 +126,25 @@ const renderLabel = (option) => {
|
||||||
.logo {
|
.logo {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
|
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-desktop {
|
.main-menu {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
margin-right: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-menu-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-left: auto;
|
||||||
|
min-width: 150px;
|
||||||
|
padding-right: 16px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-menu {
|
||||||
|
margin-left: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-mobile-button {
|
.menu-mobile-button {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||||
setToken(access_token)
|
setToken(access_token)
|
||||||
user.value = userData || null
|
user.value = userData || null
|
||||||
|
|
||||||
router.push({ name: 'startPage'})
|
router.push({ name: 'collection'})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login error:', error)
|
console.error('Login error:', error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue