Fixes to the followers list view

This commit is contained in:
Paul Frazee 2023-01-19 16:38:12 -06:00
parent 88ab1f171a
commit aec0f1c3ba
3 changed files with 74 additions and 61 deletions

View file

@ -5,9 +5,9 @@ import {
} from '@atproto/api'
import {RootStoreModel} from './root-store'
export type FollowerItem = GetFollowers.Follower & {
_reactKey: string
}
const PAGE_SIZE = 30
export type FollowerItem = GetFollowers.Follower
export class UserFollowersViewModel {
// state
@ -16,6 +16,9 @@ export class UserFollowersViewModel {
hasLoaded = false
error = ''
params: GetFollowers.QueryParams
hasMore = true
loadMoreCursor?: string
private _loadMorePromise: Promise<void> | undefined
// data
subject: ActorRef.WithInfo = {
@ -55,16 +58,17 @@ export class UserFollowersViewModel {
// public api
// =
async setup() {
await this._fetch()
}
async refresh() {
await this._fetch(true)
return this.loadMore(true)
}
async loadMore() {
// TODO
async loadMore(isRefreshing = false) {
if (this._loadMorePromise) {
return this._loadMorePromise
}
this._loadMorePromise = this._loadMore(isRefreshing)
await this._loadMorePromise
this._loadMorePromise = undefined
}
// state transitions
@ -89,32 +93,30 @@ export class UserFollowersViewModel {
// loader functions
// =
private async _fetch(isRefreshing = false) {
private async _loadMore(isRefreshing = false) {
if (!this.hasMore) {
return
}
this._xLoading(isRefreshing)
try {
const res = await this.rootStore.api.app.bsky.graph.getFollowers(
this.params,
)
this._replaceAll(res)
const params = Object.assign({}, this.params, {
limit: PAGE_SIZE,
before: this.loadMoreCursor,
})
if (this.isRefreshing) {
this.followers = []
}
const res = await this.rootStore.api.app.bsky.graph.getFollowers(params)
await this._appendAll(res)
this._xIdle()
} catch (e: any) {
this._xIdle(`Failed to load feed: ${e.toString()}`)
this._xIdle(e)
}
}
private _replaceAll(res: GetFollowers.Response) {
this.subject.did = res.data.subject.did
this.subject.handle = res.data.subject.handle
this.subject.displayName = res.data.subject.displayName
this.subject.avatar = res.data.subject.avatar
this.followers.length = 0
let counter = 0
for (const item of res.data.followers) {
this._append({_reactKey: `item-${counter++}`, ...item})
}
}
private _append(item: FollowerItem) {
this.followers.push(item)
private async _appendAll(res: GetFollowers.Response) {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
this.followers = this.followers.concat(res.data.followers)
}
}