55 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-06-20 10:41:26 +00:00
import { Application } from "./Application"
import { AnimeNotifier } from "./AnimeNotifier"
2017-06-20 13:46:49 +00:00
import { Diff } from "./Diff"
2017-06-20 10:41:26 +00:00
// Add anime to collection
export function addAnimeToCollection(arn: AnimeNotifier, button: HTMLElement) {
button.innerText = "Adding..."
arn.loading(true)
let {animeId, userId, userNick} = button.dataset
fetch("/api/animelist/" + userId + "/add", {
method: "POST",
2017-06-20 11:02:20 +00:00
body: animeId,
2017-06-20 12:19:35 +00:00
credentials: "same-origin"
2017-06-20 10:41:26 +00:00
})
.then(response => response.text())
.then(body => {
if(body !== "ok") {
throw body
}
2017-06-20 13:46:49 +00:00
return fetch("/_" + arn.app.currentPath, {
credentials: "same-origin"
})
.then(response => response.text())
.then(html => Diff.update(arn.app.content, html))
2017-06-20 10:41:26 +00:00
})
.catch(console.error)
.then(() => arn.loading(false))
}
// Remove anime from collection
export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElement) {
button.innerText = "Removing..."
arn.loading(true)
let {animeId, userId, userNick} = button.dataset
fetch("/api/animelist/" + userId + "/remove", {
method: "POST",
2017-06-20 11:02:20 +00:00
body: animeId,
2017-06-20 12:19:35 +00:00
credentials: "same-origin"
2017-06-20 10:41:26 +00:00
})
.then(response => response.text())
.then(body => {
if(body !== "ok") {
throw body
}
2017-06-20 13:46:49 +00:00
return arn.app.load("/+" + userNick + "/animelist")
2017-06-20 10:41:26 +00:00
})
.catch(console.error)
.then(() => arn.loading(false))
}