akkoma-fe/src/components/user_profile/user_profile.js

172 lines
5 KiB
JavaScript
Raw Normal View History

2019-02-26 17:26:04 +00:00
import get from 'lodash/get'
2019-03-05 19:01:49 +00:00
import UserCard from '../user_card/user_card.vue'
2019-02-26 03:51:04 +00:00
import FollowCard from '../follow_card/follow_card.vue'
2017-06-12 14:20:02 +00:00
import Timeline from '../timeline/timeline.vue'
import Conversation from '../conversation/conversation.vue'
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.js'
2019-04-04 02:28:08 +00:00
import List from '../list/list.vue'
2019-02-25 09:51:23 +00:00
import withLoadMore from '../../hocs/with_load_more/with_load_more'
2020-10-20 21:01:28 +00:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faCircleNotch
} from '@fortawesome/free-solid-svg-icons'
library.add(
faCircleNotch
)
2019-02-25 09:51:23 +00:00
2019-04-04 02:28:08 +00:00
const FollowerList = withLoadMore({
fetch: (props, $store) => $store.dispatch('fetchFollowers', props.userId),
select: (props, $store) => get($store.getters.findUser(props.userId), 'followerIds', []).map(id => $store.getters.findUser(id)),
2019-04-09 18:46:47 +00:00
destroy: (props, $store) => $store.dispatch('clearFollowers', props.userId),
2019-04-04 02:28:08 +00:00
childPropName: 'items',
additionalPropNames: ['userId']
})(List)
2019-02-25 09:51:23 +00:00
2019-04-04 02:28:08 +00:00
const FriendList = withLoadMore({
fetch: (props, $store) => $store.dispatch('fetchFriends', props.userId),
select: (props, $store) => get($store.getters.findUser(props.userId), 'friendIds', []).map(id => $store.getters.findUser(id)),
2019-04-09 18:46:47 +00:00
destroy: (props, $store) => $store.dispatch('clearFriends', props.userId),
2019-04-04 02:28:08 +00:00
childPropName: 'items',
additionalPropNames: ['userId']
})(List)
2016-11-30 22:32:22 +00:00
const defaultTabKey = 'statuses'
2016-11-30 22:32:22 +00:00
const UserProfile = {
data () {
return {
error: false,
userId: null,
tab: defaultTabKey
}
},
2017-06-12 14:00:46 +00:00
created () {
2019-04-15 15:08:28 +00:00
const routeParams = this.$route.params
this.load(routeParams.name || routeParams.id)
this.tab = get(this.$route, 'query.tab', defaultTabKey)
2017-06-12 14:00:46 +00:00
},
destroyed () {
this.stopFetching()
},
2016-11-30 22:32:22 +00:00
computed: {
2018-12-15 03:16:44 +00:00
timeline () {
return this.$store.state.statuses.timelines.user
},
favorites () {
return this.$store.state.statuses.timelines.favorites
},
2019-01-24 10:48:40 +00:00
media () {
return this.$store.state.statuses.timelines.media
},
2019-01-17 19:11:51 +00:00
isUs () {
return this.userId && this.$store.state.users.currentUser.id &&
this.userId === this.$store.state.users.currentUser.id
2019-01-17 19:11:51 +00:00
},
2016-11-30 22:32:22 +00:00
user () {
2019-04-15 15:08:28 +00:00
return this.$store.getters.findUser(this.userId)
2018-12-15 03:16:44 +00:00
},
isExternal () {
return this.$route.name === 'external-user-profile'
2019-02-03 17:52:04 +00:00
},
2019-02-07 15:53:36 +00:00
followsTabVisible () {
return this.isUs || !this.user.hide_follows
2019-02-03 17:52:04 +00:00
},
followersTabVisible () {
return this.isUs || !this.user.hide_followers
2016-11-30 22:32:22 +00:00
}
},
methods: {
2019-04-15 15:08:28 +00:00
load (userNameOrId) {
const startFetchingTimeline = (timeline, userId) => {
// Clear timeline only if load another user's profile
if (userId !== this.$store.state.statuses.timelines[timeline].userId) {
this.$store.commit('clearTimeline', { timeline })
}
this.$store.dispatch('startFetchingTimeline', { timeline, userId })
}
const loadById = (userId) => {
this.userId = userId
startFetchingTimeline('user', userId)
startFetchingTimeline('media', userId)
if (this.isUs) {
startFetchingTimeline('favorites', userId)
}
// Fetch all pinned statuses immediately
this.$store.dispatch('fetchPinnedStatuses', userId)
}
// Reset view
this.userId = null
this.error = false
2019-04-15 15:08:28 +00:00
// Check if user data is already loaded in store
const user = this.$store.getters.findUser(userNameOrId)
if (user) {
loadById(user.id)
} else {
2019-04-15 15:08:28 +00:00
this.$store.dispatch('fetchUser', userNameOrId)
.then(({ id }) => loadById(id))
2019-04-15 15:08:28 +00:00
.catch((reason) => {
const errorMessage = get(reason, 'error.error')
if (errorMessage === 'No user with such user_id') { // Known error
this.error = this.$t('user_profile.profile_does_not_exist')
} else if (errorMessage) {
this.error = errorMessage
} else {
this.error = this.$t('user_profile.profile_loading_error')
}
})
}
},
stopFetching () {
this.$store.dispatch('stopFetchingTimeline', 'user')
this.$store.dispatch('stopFetchingTimeline', 'favorites')
this.$store.dispatch('stopFetchingTimeline', 'media')
},
switchUser (userNameOrId) {
this.stopFetching()
this.load(userNameOrId)
},
onTabSwitch (tab) {
this.tab = tab
this.$router.replace({ query: { tab } })
2019-11-15 18:12:16 +00:00
},
linkClicked ({ target }) {
if (target.tagName === 'SPAN') {
target = target.parentNode
}
if (target.tagName === 'A') {
window.open(target.href, '_blank')
}
}
},
watch: {
2019-04-15 15:08:28 +00:00
'$route.params.id': function (newVal) {
if (newVal) {
this.switchUser(newVal)
}
2019-03-03 18:38:48 +00:00
},
2019-04-15 15:08:28 +00:00
'$route.params.name': function (newVal) {
if (newVal) {
this.switchUser(newVal)
}
},
'$route.query': function (newVal) {
this.tab = newVal.tab || defaultTabKey
}
},
2016-11-30 22:32:22 +00:00
components: {
2019-03-05 19:01:49 +00:00
UserCard,
Timeline,
2019-02-25 09:51:23 +00:00
FollowerList,
2019-02-18 14:49:32 +00:00
FriendList,
FollowCard,
TabSwitcher,
Conversation
2016-11-30 22:32:22 +00:00
}
}
export default UserProfile