added notification cound badge in navbar
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
<router-link to="/notifications" class="nav-item">
|
<router-link to="/notifications" class="nav-item">
|
||||||
<i class="fa-solid fa-bell"></i>
|
<i class="fa-solid fa-bell"></i>
|
||||||
|
<span v-if="totalCount > 0" class="badge">{{ totalCount }}</span>
|
||||||
</router-link>
|
</router-link>
|
||||||
|
|
||||||
<button class="nav-item logout" @click="logout">
|
<button class="nav-item logout" @click="logout">
|
||||||
@@ -22,27 +23,32 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
import { onMounted } from 'vue'
|
||||||
import { logout as authLogout } from '../store.ts'
|
import { logout as authLogout } from '../store.ts'
|
||||||
|
import { useNotifications } from '../store'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const { totalCount, refreshNotifications } = useNotifications()
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
authLogout()
|
authLogout()
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
refreshNotifications()
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
#bottom-nav {
|
#bottom-nav {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 28px;
|
gap: 28px;
|
||||||
|
|
||||||
padding: 5px 22px;
|
padding: 5px 22px;
|
||||||
background: var(--panel);
|
background: var(--panel);
|
||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
border-radius: 100vh;
|
border-radius: 100vh;
|
||||||
z-index: 50;
|
z-index: 50;
|
||||||
|
|
||||||
user-select: none;
|
user-select: none;
|
||||||
-webkit-user-select: none;
|
-webkit-user-select: none;
|
||||||
|
|
||||||
@@ -50,18 +56,16 @@ function logout() {
|
|||||||
|
|
||||||
.nav-item {
|
.nav-item {
|
||||||
all: unset;
|
all: unset;
|
||||||
cursor: pointer;
|
|
||||||
|
|
||||||
|
position: relative;
|
||||||
|
cursor: pointer;
|
||||||
width: 44px;
|
width: 44px;
|
||||||
height: 44px;
|
height: 44px;
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
|
||||||
font-size: 1.25rem;
|
font-size: 1.25rem;
|
||||||
border-radius: 50%;
|
border-radius: 100vh;
|
||||||
|
|
||||||
transition:
|
transition:
|
||||||
color 0.2s ease,
|
color 0.2s ease,
|
||||||
background-color 0.2s ease,
|
background-color 0.2s ease,
|
||||||
@@ -75,6 +79,25 @@ function logout() {
|
|||||||
transform 0.15s ease;
|
transform 0.15s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.badge {
|
||||||
|
position: absolute;
|
||||||
|
top: 4px;
|
||||||
|
right: 4px;
|
||||||
|
background-color: var(--accent);
|
||||||
|
color: black;
|
||||||
|
font-size: 0.65rem;
|
||||||
|
font-weight: bold;
|
||||||
|
min-width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
border-radius: 50%;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 2px;
|
||||||
|
border: 2px solid var(--panel);
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
@media (hover: hover) {
|
@media (hover: hover) {
|
||||||
.nav-item:hover {
|
.nav-item:hover {
|
||||||
background: rgba(255, 255, 255, 0.04);
|
background: rgba(255, 255, 255, 0.04);
|
||||||
|
|||||||
@@ -35,13 +35,13 @@
|
|||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import { fetchFriendRequests, acceptFriendRequest } from '../api/friends'
|
import { fetchFriendRequests, acceptFriendRequest } from '../api/friends'
|
||||||
import { fetchRoomInvites, acceptRoomInvite } from '../api/rooms.ts'
|
import { fetchRoomInvites, acceptRoomInvite } from '../api/rooms.ts'
|
||||||
import type { FriendRequest, RoomInvite } from '../types'
|
import { useNotifications } from '../store'
|
||||||
|
|
||||||
const requests = ref<FriendRequest[]>([])
|
|
||||||
const invites = ref<RoomInvite[]>([])
|
|
||||||
const errorMessage = ref('')
|
const errorMessage = ref('')
|
||||||
|
const { requests, invites, refreshNotifications } = useNotifications()
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
await refreshNotifications()
|
||||||
requests.value = await fetchFriendRequests()
|
requests.value = await fetchFriendRequests()
|
||||||
invites.value = await fetchRoomInvites()
|
invites.value = await fetchRoomInvites()
|
||||||
})
|
})
|
||||||
|
|||||||
31
src/store.ts
31
src/store.ts
@@ -1,6 +1,10 @@
|
|||||||
import { apiFetch } from './api/client'
|
import { apiFetch } from './api/client'
|
||||||
import type { LoginResponse, User } from './types'
|
import type { LoginResponse, User } from './types'
|
||||||
import * as authStore from './authStore'
|
import * as authStore from './authStore'
|
||||||
|
import { ref, computed } from 'vue'
|
||||||
|
import { fetchFriendRequests } from './api/friends'
|
||||||
|
import { fetchRoomInvites } from './api/rooms'
|
||||||
|
import type { FriendRequest, RoomInvite } from './types'
|
||||||
|
|
||||||
export const initAuth = authStore.getAuthData
|
export const initAuth = authStore.getAuthData
|
||||||
export const getLastRoom = authStore.getLastRoom
|
export const getLastRoom = authStore.getLastRoom
|
||||||
@@ -36,3 +40,30 @@ export async function validateToken(): Promise<boolean> {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requests = ref<FriendRequest[]>([])
|
||||||
|
const invites = ref<RoomInvite[]>([])
|
||||||
|
|
||||||
|
export function useNotifications() {
|
||||||
|
const totalCount = computed(() => requests.value.length + invites.value.length)
|
||||||
|
|
||||||
|
async function refreshNotifications() {
|
||||||
|
try {
|
||||||
|
const [fReqs, rInvs] = await Promise.all([
|
||||||
|
fetchFriendRequests(),
|
||||||
|
fetchRoomInvites()
|
||||||
|
])
|
||||||
|
requests.value = fReqs
|
||||||
|
invites.value = rInvs
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to fetch notifications", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
requests,
|
||||||
|
invites,
|
||||||
|
totalCount,
|
||||||
|
refreshNotifications
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user