Skip to content

Commit

Permalink
fix useUser always in loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
Likqez committed Dec 9, 2024
1 parent 4a50299 commit e6b8fb7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion components/profile/ProfileInformation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {user, loading, error, fetchUser} = useUser()
onMounted(async () => {
if (session.value) {
await fetchUser()
console.log(user.value)
}
})
Expand Down
26 changes: 13 additions & 13 deletions composables/useUser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type {GetUserResponse} from "@/types/api/users";

export const useUser = () => {
const user = useState<GetUserResponse|null>('user', () => null)
const user = useState<GetUserResponse | null>('user', () => null)
const loading = useState<boolean>('userLoading', () => false)
const error = useState<string|null>('userError', () => null)
const error = useState<string | null>('userError', () => null)

const fetchUser = async () => {
// Only fetch if we don't have user data
if (!user.value) {
loading.value = true
error.value = null
try {
user.value = await useProfileInformation()
} catch (err) {
error.value = 'Failed to fetch user'
console.error(err)
} finally {
loading.value = false
try {
// Only set loading on initial fetch
if (!user.value) {
loading.value = true
}

user.value = await useProfileInformation()
} catch (err) {
error.value = 'Failed to fetch user'
console.error(err)
} finally {
loading.value = false
}
}

Expand Down

0 comments on commit e6b8fb7

Please sign in to comment.