xssbook/public/js/people.js
Tyler Murphy 3d71da4909 refactor
2023-02-12 14:11:50 -05:00

86 lines
No EOL
1.8 KiB
JavaScript

import { div, body, a, parse } from './main.js'
import { loadself, loaduserspage } from './api.js'
import { header, parseUser } from './components.js'
function render() {
let new_body =
body({},
...header(false, true, data.self.user_id),
div({id: 'users'},
...data.users.map(u => parseUser(u))
),
div({id: 'load'},
a({class: 'bold gtext', onclick: async () => {
let users = await load()
let el = document.getElementById("users")
for (const user of users) {
el.appendChild(parseUser(user))
}
}},
parse("Load more users")
)
)
)
document.body.replaceWith(new_body)
}
var page = 0
const data = {
users: [],
self: {}
}
async function load() {
let request = await loadself()
if (request.status === 429) {
let new_body =
body({},
...header(false, true)
)
document.body.replaceWith(new_body)
throw new Error("Rate limited");
}
const self = request.json
header(false, true, self.user_id)
const users = (await loaduserspage(page)).json
if (users.length === 0) {
document.getElementById('load').remove()
return []
} else {
page++
}
return users
}
async function init() {
let request = (await loadself());
if (request.status === 429) {
let new_body =
body({},
...header(true, false, data.self.user_id)
)
document.body.replaceWith(new_body)
throw new Error("Rate limited");
}
data.self = request.json
const users = await load()
data.users.push(... users)
render()
}
init()