xssbook/public/js/people.js

76 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-02-14 03:41:09 +00:00
import { div, body, span, parse } from './main.js'
2023-02-12 19:11:50 +00:00
import { loadself, loaduserspage } from './api.js'
import { header, parseUser } from './components.js'
2023-01-26 22:29:16 +00:00
function render() {
2023-02-12 19:11:50 +00:00
let new_body =
body({},
...header(false, true, data.self.user_id),
div({id: 'users'},
...data.users.map(u => parseUser(u))
),
div({id: 'load'},
2023-02-14 03:41:09 +00:00
span({class: 'bold gtext', onclick: async () => {
2023-02-12 19:11:50 +00:00
let users = await load()
2023-01-26 22:29:16 +00:00
2023-02-12 19:11:50 +00:00
let el = document.getElementById("users")
for (const user of users) {
el.appendChild(parseUser(user))
}
}},
parse("Load more users")
)
)
)
2023-01-26 22:29:16 +00:00
2023-02-12 19:11:50 +00:00
document.body.replaceWith(new_body)
2023-01-26 22:29:16 +00:00
}
var page = 0
2023-02-12 19:11:50 +00:00
const data = {
users: [],
self: {}
}
2023-01-26 22:29:16 +00:00
async function load() {
2023-02-12 19:11:50 +00:00
2023-01-26 22:29:16 +00:00
const users = (await loaduserspage(page)).json
2023-02-12 19:11:50 +00:00
2023-01-26 22:29:16 +00:00
if (users.length === 0) {
2023-02-12 19:11:50 +00:00
document.getElementById('load').remove()
return []
2023-01-26 22:29:16 +00:00
} else {
page++
}
return users
}
async function init() {
2023-02-12 19:11:50 +00:00
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");
}
2023-02-13 16:59:00 +00:00
if (request.json == undefined) {
location.href = '/login'
return
}
2023-02-12 19:11:50 +00:00
data.self = request.json
const users = await load()
2023-01-26 22:29:16 +00:00
data.users.push(... users)
2023-02-12 19:11:50 +00:00
2023-01-26 22:29:16 +00:00
render()
}
init()