forked from AkkomaGang/akkoma-fe
Rewrite FollowList using hocs
This commit is contained in:
parent
cb383df517
commit
080786c945
6 changed files with 45 additions and 116 deletions
|
@ -1,68 +0,0 @@
|
||||||
import UserCard from '../user_card/user_card.vue'
|
|
||||||
|
|
||||||
const FollowList = {
|
|
||||||
data () {
|
|
||||||
return {
|
|
||||||
loading: false,
|
|
||||||
bottomedOut: false,
|
|
||||||
error: false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
props: ['userId', 'showFollowers'],
|
|
||||||
created () {
|
|
||||||
window.addEventListener('scroll', this.scrollLoad)
|
|
||||||
if (this.entries.length === 0) {
|
|
||||||
this.fetchEntries()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
destroyed () {
|
|
||||||
window.removeEventListener('scroll', this.scrollLoad)
|
|
||||||
this.$store.dispatch('clearFriendsAndFollowers', this.userId)
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
user () {
|
|
||||||
return this.$store.getters.userById(this.userId)
|
|
||||||
},
|
|
||||||
entries () {
|
|
||||||
return this.showFollowers ? this.user.followers : this.user.friends
|
|
||||||
},
|
|
||||||
showFollowsYou () {
|
|
||||||
return !this.showFollowers || (this.showFollowers && this.userId !== this.$store.state.users.currentUser.id)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
fetchEntries () {
|
|
||||||
if (!this.loading) {
|
|
||||||
const command = this.showFollowers ? 'addFollowers' : 'addFriends'
|
|
||||||
this.loading = true
|
|
||||||
this.$store.dispatch(command, this.userId).then(entries => {
|
|
||||||
this.error = false
|
|
||||||
this.loading = false
|
|
||||||
this.bottomedOut = entries.length === 0
|
|
||||||
}).catch(() => {
|
|
||||||
this.error = true
|
|
||||||
this.loading = false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
scrollLoad (e) {
|
|
||||||
const bodyBRect = document.body.getBoundingClientRect()
|
|
||||||
const height = Math.max(bodyBRect.height, -(bodyBRect.y))
|
|
||||||
if (this.loading === false &&
|
|
||||||
this.bottomedOut === false &&
|
|
||||||
this.$el.offsetHeight > 0 &&
|
|
||||||
(window.innerHeight + window.pageYOffset) >= (height - 750)
|
|
||||||
) {
|
|
||||||
this.fetchEntries()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
'user': 'fetchEntries'
|
|
||||||
},
|
|
||||||
components: {
|
|
||||||
UserCard
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default FollowList
|
|
|
@ -1,33 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="follow-list">
|
|
||||||
<user-card
|
|
||||||
v-for="entry in entries"
|
|
||||||
:key="entry.id" :user="entry"
|
|
||||||
:noFollowsYou="!showFollowsYou"
|
|
||||||
/>
|
|
||||||
<div class="text-center panel-footer">
|
|
||||||
<a v-if="error" @click="fetchEntries" class="alert error">
|
|
||||||
{{$t('general.generic_error')}}
|
|
||||||
</a>
|
|
||||||
<i v-else-if="loading" class="icon-spin3 animate-spin"/>
|
|
||||||
<span v-else-if="bottomedOut"></span>
|
|
||||||
<a v-else @click="fetchEntries">{{$t('general.more')}}</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script src="./follow_list.js"></script>
|
|
||||||
|
|
||||||
<style lang="scss">
|
|
||||||
|
|
||||||
.follow-list {
|
|
||||||
.panel-footer {
|
|
||||||
padding: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.error {
|
|
||||||
font-size: 14px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
</style>
|
|
|
@ -1,8 +1,32 @@
|
||||||
|
import { compose } from 'vue-compose'
|
||||||
import get from 'lodash/get'
|
import get from 'lodash/get'
|
||||||
import UserCardContent from '../user_card_content/user_card_content.vue'
|
import UserCardContent from '../user_card_content/user_card_content.vue'
|
||||||
import UserCard from '../user_card/user_card.vue'
|
import UserCard from '../user_card/user_card.vue'
|
||||||
import Timeline from '../timeline/timeline.vue'
|
import Timeline from '../timeline/timeline.vue'
|
||||||
import FollowList from '../follow_list/follow_list.vue'
|
import withLoadMore from '../../hocs/with_load_more/with_load_more'
|
||||||
|
import withList from '../../hocs/with_list/with_list'
|
||||||
|
|
||||||
|
const FollowerList = compose(
|
||||||
|
withLoadMore({
|
||||||
|
fetch: (props, $store) => $store.dispatch('addFollowers', props.userId),
|
||||||
|
select: (props, $store) => get($store.getters.userById(props.userId), 'followers', []),
|
||||||
|
destory: (props, $store) => $store.dispatch('clearFollowers', props.userId),
|
||||||
|
childPropName: 'entries',
|
||||||
|
additionalPropNames: ['userId']
|
||||||
|
}),
|
||||||
|
withList({ getEntryProps: user => ({ user }) })
|
||||||
|
)(UserCard)
|
||||||
|
|
||||||
|
const FriendList = compose(
|
||||||
|
withLoadMore({
|
||||||
|
fetch: (props, $store) => $store.dispatch('addFriends', props.userId),
|
||||||
|
select: (props, $store) => get($store.getters.userById(props.userId), 'friends', []),
|
||||||
|
destory: (props, $store) => $store.dispatch('clearFriends', props.userId),
|
||||||
|
childPropName: 'entries',
|
||||||
|
additionalPropNames: ['userId']
|
||||||
|
}),
|
||||||
|
withList({ getEntryProps: user => ({ user }) })
|
||||||
|
)(UserCard)
|
||||||
|
|
||||||
const UserProfile = {
|
const UserProfile = {
|
||||||
data () {
|
data () {
|
||||||
|
@ -123,7 +147,8 @@ const UserProfile = {
|
||||||
UserCardContent,
|
UserCardContent,
|
||||||
UserCard,
|
UserCard,
|
||||||
Timeline,
|
Timeline,
|
||||||
FollowList
|
FollowerList,
|
||||||
|
FriendList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,16 +18,10 @@
|
||||||
:user-id="fetchBy"
|
:user-id="fetchBy"
|
||||||
/>
|
/>
|
||||||
<div :label="$t('user_card.followees')" v-if="followsTabVisible" :disabled="!user.friends_count">
|
<div :label="$t('user_card.followees')" v-if="followsTabVisible" :disabled="!user.friends_count">
|
||||||
<FollowList v-if="user.friends_count > 0" :userId="userId" :showFollowers="false" />
|
<FriendList :userId="userId" />
|
||||||
<div class="userlist-placeholder" v-else>
|
|
||||||
<i class="icon-spin3 animate-spin"></i>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<div :label="$t('user_card.followers')" v-if="followersTabVisible" :disabled="!user.followers_count">
|
<div :label="$t('user_card.followers')" v-if="followersTabVisible" :disabled="!user.followers_count">
|
||||||
<FollowList v-if="user.followers_count > 0" :userId="userId" :showFollowers="true" />
|
<FollowerList :userId="userId" :entryProps="{noFollowsYou: isUs}" />
|
||||||
<div class="userlist-placeholder" v-else>
|
|
||||||
<i class="icon-spin3 animate-spin"></i>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
<Timeline
|
<Timeline
|
||||||
:label="$t('user_card.media')"
|
:label="$t('user_card.media')"
|
||||||
|
|
|
@ -7,6 +7,7 @@ import './with_load_more.scss'
|
||||||
const withLoadMore = ({
|
const withLoadMore = ({
|
||||||
fetch, // function to fetch entries and return a promise
|
fetch, // function to fetch entries and return a promise
|
||||||
select, // function to select data from store
|
select, // function to select data from store
|
||||||
|
destroy, // function called at "destroyed" lifecycle
|
||||||
childPropName = 'entries', // name of the prop to be passed into the wrapped component
|
childPropName = 'entries', // name of the prop to be passed into the wrapped component
|
||||||
additionalPropNames = [] // additional prop name list of the wrapper component
|
additionalPropNames = [] // additional prop name list of the wrapper component
|
||||||
}) => (WrappedComponent) => {
|
}) => (WrappedComponent) => {
|
||||||
|
@ -58,6 +59,7 @@ const withLoadMore = ({
|
||||||
},
|
},
|
||||||
destroyed () {
|
destroyed () {
|
||||||
window.removeEventListener('scroll', this.scrollLoad)
|
window.removeEventListener('scroll', this.scrollLoad)
|
||||||
|
destroy && destroy(this.$props, this.$store)
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
fetchEntries () {
|
fetchEntries () {
|
||||||
|
|
|
@ -72,14 +72,20 @@ export const mutations = {
|
||||||
},
|
},
|
||||||
// Because frontend doesn't have a reason to keep these stuff in memory
|
// Because frontend doesn't have a reason to keep these stuff in memory
|
||||||
// outside of viewing someones user profile.
|
// outside of viewing someones user profile.
|
||||||
clearFriendsAndFollowers (state, userKey) {
|
clearFriends (state, userId) {
|
||||||
const user = state.usersObject[userKey]
|
const user = state.usersObject[userId]
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.friends = []
|
user.friends = []
|
||||||
user.followers = []
|
|
||||||
user.friendsPage = 0
|
user.friendsPage = 0
|
||||||
|
},
|
||||||
|
clearFollowers (state, userId) {
|
||||||
|
const user = state.usersObject[userId]
|
||||||
|
if (!user) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user.followers = []
|
||||||
user.followersPage = 0
|
user.followersPage = 0
|
||||||
},
|
},
|
||||||
addNewUsers (state, users) {
|
addNewUsers (state, users) {
|
||||||
|
@ -197,8 +203,11 @@ const users = {
|
||||||
return followers
|
return followers
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
clearFriendsAndFollowers ({ commit }, userKey) {
|
clearFriends ({ commit }, userId) {
|
||||||
commit('clearFriendsAndFollowers', userKey)
|
commit('clearFriends', userId)
|
||||||
|
},
|
||||||
|
clearFollowers ({ commit }, userId) {
|
||||||
|
commit('clearFollowers', userId)
|
||||||
},
|
},
|
||||||
registerPushNotifications (store) {
|
registerPushNotifications (store) {
|
||||||
const token = store.state.currentUser.credentials
|
const token = store.state.currentUser.credentials
|
||||||
|
|
Loading…
Reference in a new issue