follows/followers pagination ready for review

This commit is contained in:
shpuld 2019-02-02 22:29:10 +02:00
parent 8ce513ed09
commit dbb16d56e2
9 changed files with 229 additions and 76 deletions

View file

@ -0,0 +1,61 @@
import UserCard from '../user_card/user_card.vue'
const FollowersList = {
data () {
return {
loading: false,
bottomedOut: false,
error: false
}
},
props: ['userId'],
created () {
window.addEventListener('scroll', this.scrollLoad)
if (this.user.followers.length === 0) {
this.fetchFollowers()
}
},
destroyed () {
window.removeEventListener('scroll', this.scrollLoad)
this.$store.dispatch('clearFriendsAndFollowers', this.userId)
},
computed: {
user () {
return this.$store.getters.userById(this.userId)
},
followers () {
return this.user.followers
}
},
methods: {
fetchFollowers () {
if (!this.loading) {
this.loading = true
this.$store.dispatch('addFollowers', this.userId).then(followers => {
this.error = false
this.loading = false
this.bottomedOut = followers.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.fetchFollowers()
}
}
},
components: {
UserCard
}
}
export default FollowersList

View file

@ -0,0 +1,12 @@
<template>
<div>
<user-card v-for="follower in followers" :key="follower.id" :user="follower" :showFollows="true"></user-card>
<div @click="fetchFollowers" class="new-status-notification text-center panel-footer">
<span v-if="error" class="alert error">Error loading followers</span>
<i v-else-if="loading" class="icon-spin3 animate-spin"/>
<span v-else-if="bottomedOut"></span>
</div>
</div>
</template>
<script src="./followers_list.js" />

View file

@ -0,0 +1,61 @@
import UserCard from '../user_card/user_card.vue'
const FriendsList = {
data () {
return {
loading: false,
bottomedOut: false,
error: false
}
},
props: ['userId'],
created () {
window.addEventListener('scroll', this.scrollLoad)
if (this.user.followers.length === 0) {
this.fetchFriends()
}
},
destroyed () {
window.removeEventListener('scroll', this.scrollLoad)
this.$store.dispatch('clearFriendsAndFollowers', this.userId)
},
computed: {
user () {
return this.$store.getters.userById(this.userId)
},
friends () {
return this.user.friends
}
},
methods: {
fetchFriends () {
if (!this.loading) {
this.loading = true
this.$store.dispatch('addFriends', this.userId).then(friends => {
this.error = false
this.loading = false
this.bottomedOut = friends.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.fetchFriends()
}
}
},
components: {
UserCard
}
}
export default FriendsList

View file

@ -0,0 +1,12 @@
<template>
<div>
<user-card v-for="friend in friends" :key="friend.id" :user="friend" :showFollows="true"></user-card>
<div @click="fetchFriends" class="new-status-notification text-center panel-footer">
<span v-if="error" class="alert error">Error loading follows</span>
<i v-else-if="loading" class="icon-spin3 animate-spin"/>
<span v-else-if="bottomedOut"></span>
</div>
</div>
</template>
<script src="./friends_list.js" />

View file

@ -1,7 +1,7 @@
<template> <template>
<div class="card"> <div class="card">
<a href="#"> <a href="#">
<StillImage @click.prevent="toggleUserExpanded" class="avatar" :src="user.profile_image_url"/> <StillImage @click.prevent.native="toggleUserExpanded" class="avatar" :src="user.profile_image_url"/>
</a> </a>
<div class="usercard" v-if="userExpanded"> <div class="usercard" v-if="userExpanded">
<user-card-content :user="user" :switcher="false"></user-card-content> <user-card-content :user="user" :switcher="false"></user-card-content>
@ -79,7 +79,7 @@
.usercard { .usercard {
width: fill-available; width: fill-available;
margin: 0.2em 0 0.7em 0; margin: 0.2em 0 0 0.7em;
border-radius: $fallback--panelRadius; border-radius: $fallback--panelRadius;
border-radius: var(--panelRadius, $fallback--panelRadius); border-radius: var(--panelRadius, $fallback--panelRadius);
border-style: solid; border-style: solid;

View file

@ -1,6 +1,8 @@
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 FriendsList from '../friends_list/friends_list.vue'
import FollowersList from '../followers_list/followers_list.vue'
const UserProfile = { const UserProfile = {
created () { created () {
@ -15,15 +17,7 @@ const UserProfile = {
} }
}, },
destroyed () { destroyed () {
this.$store.dispatch('stopFetching', 'user') this.cleanUp(this.userId)
this.$store.dispatch('stopFetching', 'favorites')
this.$store.dispatch('stopFetching', 'media')
},
data () {
return {
followsPage: 0,
followersPage: 0
}
}, },
computed: { computed: {
timeline () { timeline () {
@ -45,12 +39,6 @@ const UserProfile = {
return this.userId && this.$store.state.users.currentUser.id && return this.userId && this.$store.state.users.currentUser.id &&
this.userId === this.$store.state.users.currentUser.id this.userId === this.$store.state.users.currentUser.id
}, },
friends () {
return this.user.friends
},
followers () {
return this.user.followers
},
userInStore () { userInStore () {
if (this.isExternal) { if (this.isExternal) {
return this.$store.getters.userById(this.userId) return this.$store.getters.userById(this.userId)
@ -74,66 +62,48 @@ const UserProfile = {
} }
}, },
methods: { methods: {
fetchFollowers () {
const id = this.userId
this.$store.dispatch('addFollowers', { id })
},
fetchFriends () {
const id = this.userId
this.$store.dispatch('addFriends', { id })
},
startFetchFavorites () { startFetchFavorites () {
if (this.isUs) { if (this.isUs) {
this.$store.dispatch('startFetching', ['favorites', this.fetchBy]) this.$store.dispatch('startFetching', ['favorites', this.fetchBy])
} }
}, },
nextFollowsPage () { startUp () {
this.followsPage += 1 this.$store.dispatch('startFetching', ['user', this.fetchBy])
this.$store.dispatch('addFriends', { id: this.userId, page: this.followsPage }) this.$store.dispatch('startFetching', ['media', this.fetchBy])
console.log(this.user.friends)
} this.startFetchFavorites()
}, },
watch: { cleanUp () {
// TODO get rid of this copypasta
userName () {
if (this.isExternal) {
return
}
this.$store.dispatch('stopFetching', 'user') this.$store.dispatch('stopFetching', 'user')
this.$store.dispatch('stopFetching', 'favorites') this.$store.dispatch('stopFetching', 'favorites')
this.$store.dispatch('stopFetching', 'media') this.$store.dispatch('stopFetching', 'media')
this.$store.commit('clearTimeline', { timeline: 'user' }) this.$store.commit('clearTimeline', { timeline: 'user' })
this.$store.commit('clearTimeline', { timeline: 'favorites' }) this.$store.commit('clearTimeline', { timeline: 'favorites' })
this.$store.commit('clearTimeline', { timeline: 'media' }) this.$store.commit('clearTimeline', { timeline: 'media' })
this.$store.dispatch('startFetching', ['user', this.fetchBy]) }
this.$store.dispatch('startFetching', ['media', this.fetchBy]) },
this.startFetchFavorites() watch: {
userName () {
if (this.isExternal) {
return
}
this.cleanUp()
this.startUp()
}, },
userId () { userId () {
if (!this.isExternal) { if (!this.isExternal) {
return return
} }
this.$store.dispatch('stopFetching', 'user') this.cleanUp()
this.$store.dispatch('stopFetching', 'favorites') this.startUp()
this.$store.dispatch('stopFetching', 'media')
this.$store.commit('clearTimeline', { timeline: 'user' })
this.$store.commit('clearTimeline', { timeline: 'favorites' })
this.$store.commit('clearTimeline', { timeline: 'media' })
this.$store.dispatch('startFetching', ['user', this.fetchBy])
this.$store.dispatch('startFetching', ['media', this.fetchBy])
this.startFetchFavorites()
},
user () {
if (this.user.id && !this.user.followers) {
this.fetchFollowers()
this.fetchFriends()
}
} }
}, },
components: { components: {
UserCardContent, UserCardContent,
UserCard, UserCard,
Timeline Timeline,
FriendsList,
FollowersList
} }
} }

View file

@ -5,18 +5,13 @@
<tab-switcher> <tab-switcher>
<Timeline :label="$t('user_card.statuses')" :embedded="true" :title="$t('user_profile.timeline_title')" :timeline="timeline" :timeline-name="'user'" :user-id="fetchBy"/> <Timeline :label="$t('user_card.statuses')" :embedded="true" :title="$t('user_profile.timeline_title')" :timeline="timeline" :timeline-name="'user'" :user-id="fetchBy"/>
<div :label="$t('user_card.followees')"> <div :label="$t('user_card.followees')">
<div v-if="friends"> <FriendsList v-if="user.friends_count > 0" :userId="userId" />
<user-card v-for="friend in friends" :key="friend.id" :user="friend" :showFollows="true"></user-card>
<div @click="nextFollowsPage" class="panel-footer">MORE</div>
</div>
<div class="userlist-placeholder" v-else> <div class="userlist-placeholder" v-else>
<i class="icon-spin3 animate-spin"></i> <i class="icon-spin3 animate-spin"></i>
</div> </div>
</div> </div>
<div :label="$t('user_card.followers')"> <div :label="$t('user_card.followers')">
<div v-if="followers"> <FollowersList v-if="user.followers_count > 0" :userId="userId" />
<user-card v-for="follower in followers" :key="follower.id" :user="follower" :showFollows="false"></user-card>
</div>
<div class="userlist-placeholder" v-else> <div class="userlist-placeholder" v-else>
<i class="icon-spin3 animate-spin"></i> <i class="icon-spin3 animate-spin"></i>
</div> </div>

View file

@ -1,5 +1,5 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import { compact, map, each, merge, concat } from 'lodash' import { compact, map, each, merge, find } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js' import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js'
import oauthApi from '../services/new_api/oauth' import oauthApi from '../services/new_api/oauth'
@ -52,14 +52,35 @@ export const mutations = {
state.loggingIn = false state.loggingIn = false
}, },
// TODO Clean after ourselves? // TODO Clean after ourselves?
addFriends (state, { id, friends }) { addFriends (state, { id, friends, page }) {
const user = state.usersObject[id] const user = state.usersObject[id]
console.log(user.friends) each(friends, friend => {
user.friends = concat(user.friends, friends) if (!find(user.friends, { id: friend.id })) {
user.friends.push(friend)
}
})
user.friendsPage = page + 1
}, },
addFollowers (state, { id, followers }) { addFollowers (state, { id, followers, page }) {
const user = state.usersObject[id] const user = state.usersObject[id]
user.followers = followers each(followers, follower => {
if (!find(user.followers, { id: follower.id })) {
user.followers.push(follower)
}
})
user.followersPage = page + 1
},
// Because frontend doesn't have a reason to keep these stuff in memory
// outside of viewing someones user profile.
clearFriendsAndFollowers (state, userKey) {
const user = state.usersObject[userKey]
if (!user) {
return
}
user.friends = []
user.followers = []
user.friendsPage = 0
user.followersPage = 0
}, },
addNewUsers (state, users) { addNewUsers (state, users) {
each(users, (user) => mergeOrAdd(state.users, state.usersObject, user)) each(users, (user) => mergeOrAdd(state.users, state.usersObject, user))
@ -116,13 +137,34 @@ const users = {
store.rootState.api.backendInteractor.fetchUser({ id }) store.rootState.api.backendInteractor.fetchUser({ id })
.then((user) => store.commit('addNewUsers', [user])) .then((user) => store.commit('addNewUsers', [user]))
}, },
addFriends ({ rootState, commit }, { id, page }) { addFriends ({ rootState, commit }, fetchBy) {
rootState.api.backendInteractor.fetchFriends({ id, page }) return new Promise((resolve, reject) => {
.then((friends) => commit('addFriends', { id, friends })) const user = rootState.users.usersObject[fetchBy]
const page = user.friendsPage || 1
rootState.api.backendInteractor.fetchFriends({ id: user.id, page })
.then((friends) => {
commit('addFriends', { id: user.id, friends, page })
resolve(friends)
}).catch(() => {
reject()
})
})
}, },
addFollowers ({ rootState, commit }, { id, page }) { addFollowers ({ rootState, commit }, fetchBy) {
rootState.api.backendInteractor.fetchFollowers({ id, page }) return new Promise((resolve, reject) => {
.then((followers) => commit('addFollowers', { id, followers })) const user = rootState.users.usersObject[fetchBy]
const page = user.followersPage || 1
rootState.api.backendInteractor.fetchFollowers({ id: user.id, page })
.then((followers) => {
commit('addFollowers', { id: user.id, followers, page })
resolve(followers)
}).catch(() => {
reject()
})
})
},
clearFriendsAndFollowers ({ commit }, userKey) {
commit('clearFriendsAndFollowers', userKey)
}, },
registerPushNotifications (store) { registerPushNotifications (store) {
const token = store.state.currentUser.credentials const token = store.state.currentUser.credentials

View file

@ -8,6 +8,6 @@ const generateProfileLink = (id, screenName, restrictedNicknames) => {
} }
} }
const isExternal = screenName => screenName.includes('@') const isExternal = screenName => screenName && screenName.includes('@')
export default generateProfileLink export default generateProfileLink