Files
nocodb/packages/nc-gui/composables/useProfile.ts
Mert E 89f0895de3 fix: experimental frontend optimizations (#8427)
* fix: get rid of barrel files

* chore: get rid of explicit imports

* fix: use explicit import for classes and enums

* fix: use explicit import for enum & class & aliases

* fix: build issues
2024-05-08 15:55:09 +05:30

69 lines
1.6 KiB
TypeScript

export function useProfile() {
const { user } = useGlobal()
const { $api } = useNuxtApp()
const router = useRouter()
const profile = ref()
const isFollowing = ref()
const loadIsFollowingUser = async (userId: string) => {
if (!userId) return
try {
isFollowing.value = await $api.orgUsers.isFollowing(user.value!.id, userId)
} catch (e: any) {
message.error(await extractSdkResponseErrorMsg(e))
}
}
const loadProfile = async (username: string) => {
if (!username) return
try {
const user = await $api.orgUsers.getByUsername(username)
profile.value = await $api.orgUsers.profileGet(user.id)
await loadIsFollowingUser(user.id)
} catch (e: any) {
if (e?.response?.status === 404) {
return router.push('/error/404')
}
throw e
}
}
const followUser = async (userId: string) => {
if (!userId) return
try {
$api.orgUsers.followerCreate(user.value!.id, {
fk_follower_id: userId,
})
await loadProfile(profile.value.user_name)
} catch (e: any) {
message.error(await extractSdkResponseErrorMsg(e))
}
}
const unfollowUser = async (userId: string) => {
if (!userId) return
try {
$api.orgUsers.followerDelete(user.value!.id, {
fk_follower_id: userId,
})
await loadProfile(profile.value.user_name)
} catch (e: any) {
message.error(await extractSdkResponseErrorMsg(e))
}
}
return {
loadProfile,
profile,
followUser,
unfollowUser,
loadIsFollowingUser,
isFollowing,
}
}