Add 'Show more profiles' button to follow list

This commit is contained in:
silverpill 2022-08-07 18:25:08 +00:00
parent 64d85c6baa
commit acf5116093
2 changed files with 68 additions and 8 deletions

View file

@ -70,22 +70,49 @@ export async function unfollow(
} }
} }
interface ProfileListPage {
profiles: Profile[];
nextPageUrl: string | null;
}
function getNextPageUrl(response: Response): string | null {
const linkHeader = response.headers.get("Link")
if (!linkHeader) {
return null
}
// TODO: use advanced Link header parser
const link = linkHeader.split(";")[0]
return link.slice(1, link.length - 1)
}
export async function getFollowers( export async function getFollowers(
authToken: string, authToken: string,
accountId: string, accountId: string,
): Promise<Profile[]> { url?: string,
const url = `${BACKEND_URL}/api/v1/accounts/${accountId}/followers` ): Promise<ProfileListPage> {
if (!url) {
url = `${BACKEND_URL}/api/v1/accounts/${accountId}/followers`
}
const response = await http(url, { authToken }) const response = await http(url, { authToken })
const data = await response.json() const data = await response.json()
return data return {
profiles: data,
nextPageUrl: getNextPageUrl(response),
}
} }
export async function getFollowing( export async function getFollowing(
authToken: string, authToken: string,
accountId: string, accountId: string,
): Promise<Profile[]> { url?: string,
const url = `${BACKEND_URL}/api/v1/accounts/${accountId}/following` ): Promise<ProfileListPage> {
if (!url) {
url = `${BACKEND_URL}/api/v1/accounts/${accountId}/following`
}
const response = await http(url, { authToken }) const response = await http(url, { authToken })
const data = await response.json() const data = await response.json()
return data return {
profiles: data,
nextPageUrl: getNextPageUrl(response),
}
} }

View file

@ -179,6 +179,13 @@
:profile="profile" :profile="profile"
:key="profile.id" :key="profile.id"
></profile-list-item> ></profile-list-item>
<button
v-if="followListNextPageUrl"
class="btn secondary next-btn"
@click="loadFollowListNextPage()"
>
Show more profiles
</button>
</template> </template>
</template> </template>
</sidebar-layout> </sidebar-layout>
@ -232,6 +239,7 @@ let profileMenuVisible = $ref(false)
let tabName = $ref("posts") let tabName = $ref("posts")
let posts = $ref<Post[]>([]) let posts = $ref<Post[]>([])
let followList = $ref<Profile[]>([]) let followList = $ref<Profile[]>([])
let followListNextPageUrl = $ref<string | null>(null)
onMounted(async () => { onMounted(async () => {
profile = await getProfile( profile = await getProfile(
@ -258,15 +266,19 @@ onMounted(async () => {
false, false,
) )
} else if (tabName === "followers" && isCurrentUser()) { } else if (tabName === "followers" && isCurrentUser()) {
followList = await getFollowers( const page = await getFollowers(
ensureAuthToken(), ensureAuthToken(),
profile.id, profile.id,
) )
followList = page.profiles
followListNextPageUrl = page.nextPageUrl
} else if (tabName === "following" && isCurrentUser()) { } else if (tabName === "following" && isCurrentUser()) {
followList = await getFollowing( const page = await getFollowing(
ensureAuthToken(), ensureAuthToken(),
profile.id, profile.id,
) )
followList = page.profiles
followListNextPageUrl = page.nextPageUrl
} }
}) })
@ -457,6 +469,27 @@ async function loadNextPage(maxId: string) {
) )
posts.push(...nextPage) posts.push(...nextPage)
} }
async function loadFollowListNextPage() {
if (!profile || !followListNextPageUrl) {
return
}
let loadFollowList
if (tabName === "followers") {
loadFollowList = getFollowers
} else if (tabName === "following") {
loadFollowList = getFollowing
} else {
throw new Error("wrong tab")
}
const page = await loadFollowList(
ensureAuthToken(),
profile.id,
followListNextPageUrl,
)
followList.push(...page.profiles)
followListNextPageUrl = page.nextPageUrl
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">