refactor: change profile type

This commit is contained in:
Sándor Levcsák 2020-11-07 06:17:30 +02:00 committed by plumrx
parent 2f0a9680e3
commit e3e3d8f00e
3 changed files with 10 additions and 14 deletions

View File

@ -42,7 +42,7 @@ import { useProfile } from '../composable/useProfile'
import { postComment } from '../services/comment/postComment'
import * as userStore from '../store/user'
import { user, checkAuthorization } from '../store/user'
export default defineComponent({
name: 'ArticleDetailCommentsForm',
@ -53,13 +53,8 @@ export default defineComponent({
'add-comment': (comment: ArticleComment) => !!comment.id,
},
setup (props, { emit }) {
const { user, checkAuthorization } = userStore
let profile
if (checkAuthorization(user)) {
const username = computed(() => user.value.username)
profile = useProfile({ username }).profile
}
const username = computed(() => checkAuthorization(user) ? user.value.username : '')
const { profile } = useProfile({ username })
const comment = ref('')

View File

@ -1,4 +1,4 @@
import { ComputedRef, reactive, watch } from 'vue'
import { ComputedRef, ref, watch } from 'vue'
import { getProfile } from '../services/profile/getProfile'
@ -7,15 +7,16 @@ interface UseProfileProps {
}
export function useProfile ({ username }: UseProfileProps) {
const profile = reactive<Profile>({} as Profile)
const profile = ref<Profile | null>(null)
async function fetchProfile () {
updateProfile(null)
const profileData = await getProfile(username.value)
updateProfile(profileData)
}
async function updateProfile (profileData: Profile) {
Object.assign(profile, profileData)
async function updateProfile (profileData: Profile | null) {
profile.value = profileData
}
watch(username, fetchProfile, { immediate: true })

View File

@ -5,7 +5,7 @@
<div class="row">
<div class="col-xs-12 col-md-10 offset-md-1">
<div
v-if="!profile.username"
v-if="!profile"
class="align-left"
>
Profile is downloading...
@ -89,7 +89,7 @@ export default defineComponent({
const { profile, updateProfile } = useProfile({ username })
const { followProcessGoing, toggleFollow } = useFollow({
following: computed<boolean>(() => profile.following),
following: computed<boolean>(() => profile.value?.following ?? false),
username,
onUpdate: (newProfileData: Profile) => updateProfile(newProfileData),
})