Skip to content

Commit c3ea454

Browse files
committed
feat: username skeleton in navbar
1 parent 022f82c commit c3ea454

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

resources/js/components/panels/NavBar.vue

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<script setup lang="ts">
2-
import type { UserResource } from '@/types/resources';
3-
4-
import { ref, onMounted, watch } from 'vue';
2+
import { ref, onMounted } from 'vue';
53
import { useAuthStore } from '@/stores/AuthStore';
64
import { useAppStore } from '@/stores/AppStore';
75
import { storeToRefs } from 'pinia';
@@ -20,24 +18,18 @@ import ProiconsMenu from '~icons/proicons/menu';
2018
const showDropdown = ref(false);
2119
const username = ref('');
2220
21+
const { userData, isLoadingUserData } = storeToRefs(useAuthStore());
2322
const { pageTitle, selectedSideBar } = storeToRefs(useAppStore());
2423
const { cycleSideBar } = useAppStore();
25-
const { userData } = storeToRefs(useAuthStore());
2624
const { auth } = useAuthStore();
2725
2826
const toggleDropdown = () => {
2927
showDropdown.value = !showDropdown.value;
3028
};
3129
32-
const handleAuthEvent = (newUserData: UserResource | null) => {
33-
username.value = newUserData?.name ?? '';
34-
};
35-
3630
onMounted(async () => {
37-
if (await auth()) handleAuthEvent(userData.value);
31+
await auth();
3832
});
39-
40-
watch(userData, handleAuthEvent, { immediate: false });
4133
</script>
4234

4335
<template>
@@ -55,8 +47,13 @@ watch(userData, handleAuthEvent, { immediate: false });
5547
:aria-expanded="showDropdown"
5648
aria-controls="user-dropdown"
5749
>
58-
<h2 id="user-name" class="hidden sm:block truncate" v-if="username">{{ username }}</h2>
59-
<h2 id="user-name-unauth" v-else class="text-right hidden sm:block">Guest</h2>
50+
<h2
51+
id="user-name"
52+
class="hidden sm:block truncate"
53+
:class="[{ 'bg-neutral-200 dark:bg-neutral-800 rounded-full w-32 h-5 my-auto animate-pulse': isLoadingUserData }]"
54+
>
55+
{{ isLoadingUserData ? '' : userData?.name || 'Guest' }}
56+
</h2>
6057

6158
<img
6259
:src="userData?.avatar ?? '/storage/avatars/default.jpg'"
@@ -69,7 +66,7 @@ watch(userData, handleAuthEvent, { immediate: false });
6966
</span>
7067
<span class="flex flex-wrap sm:flex-nowrap sm:max-w-sm items-center gap-1 sm:shrink-0 justify-end sm:justify-normal sm:w-auto ml-auto">
7168
<section id="video-navbar" class="flex items-center gap-1 antialiased">
72-
<NavButton v-if="username" @click="cycleSideBar('notifications')" :label="'notifications'" class="hidden">
69+
<NavButton v-if="userData" @click="cycleSideBar('notifications')" :label="'notifications'" class="hidden">
7370
<template #icon>
7471
<CircumInboxIn height="24" width="24" />
7572
</template>

resources/js/stores/AuthStore.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ref } from 'vue';
99
// This code is not good
1010
export const useAuthStore = defineStore('Auth', () => {
1111
const userData = ref<null | UserResource>(null);
12+
const isLoadingUserData = ref<boolean>(false);
1213

1314
const auth = async (): Promise<boolean> => {
1415
/*
@@ -33,6 +34,8 @@ export const useAuthStore = defineStore('Auth', () => {
3334
return false;
3435
}
3536

37+
isLoadingUserData.value = true;
38+
3639
const { data, status } = await authenticate(localToken);
3740

3841
if (status !== 200) {
@@ -41,6 +44,7 @@ export const useAuthStore = defineStore('Auth', () => {
4144
}
4245

4346
userData.value = data.data.user;
47+
isLoadingUserData.value = false;
4448
return true;
4549
} catch (error: unknown) {
4650
console.error('Authentication failed:', error);
@@ -57,17 +61,20 @@ export const useAuthStore = defineStore('Auth', () => {
5761

5862
toast.add('Session Expired', { type: 'warning', description: `Please log in again.` });
5963
clearAuthState();
64+
isLoadingUserData.value = false;
6065
return false;
6166
}
6267
};
6368

6469
const clearAuthState = (): void => {
6570
userData.value = null;
71+
isLoadingUserData.value = false;
6672
localStorage.removeItem('auth-token');
6773
};
6874

6975
return {
7076
userData,
77+
isLoadingUserData,
7178
auth,
7279
clearAuthState,
7380
};

0 commit comments

Comments
 (0)