56 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
import Diff from "../Diff"
2017-10-17 09:27:15 +00:00
// Load more
2018-03-29 10:14:46 +00:00
export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
2017-10-17 09:27:15 +00:00
// Prevent firing this event multiple times
if(arn.isLoading || button.disabled || button.classList.contains("hidden")) {
2017-10-17 09:27:15 +00:00
return
}
const target = document.getElementById("load-more-target")
if(!target) {
return
}
2017-10-17 09:27:15 +00:00
arn.loading(true)
button.disabled = true
2019-11-17 09:25:14 +00:00
const index = button.dataset.index
2018-03-29 10:14:46 +00:00
try {
2019-11-17 09:25:14 +00:00
const response = await fetch("/_" + arn.app.currentPath + "/from/" + index, {
2018-03-29 10:14:46 +00:00
credentials: "same-origin"
})
if(!response.ok) {
throw response.statusText
}
2019-11-17 09:25:14 +00:00
const newIndex = response.headers.get("X-LoadMore-Index")
2017-10-17 09:27:15 +00:00
// End of data?
if(!newIndex || newIndex === "-1") {
button.disabled = true
2017-10-17 09:27:15 +00:00
button.classList.add("hidden")
} else {
button.dataset.index = newIndex
}
// Get the HTML response
2019-11-17 09:25:14 +00:00
const html = await response.text()
2017-10-17 09:27:15 +00:00
// Add the HTML to the existing target
2018-03-29 10:14:46 +00:00
Diff.mutations.queue(() => {
target.insertAdjacentHTML("beforeend", html)
2017-10-17 09:27:15 +00:00
arn.app.emit("DOMContentLoaded")
})
2018-03-29 10:14:46 +00:00
} catch(err) {
arn.statusMessage.showError(err)
} finally {
2017-10-17 09:27:15 +00:00
arn.loading(false)
button.disabled = false
2018-03-29 10:14:46 +00:00
}
2019-11-17 09:44:30 +00:00
}