53 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
2017-10-17 09:27:15 +00:00
// Add anime to collection
export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButtonElement) {
button.disabled = true
2017-10-17 09:27:15 +00:00
let {animeId} = button.dataset
2019-04-22 06:02:51 +00:00
if(!animeId) {
console.error("Button without anime ID:", button)
return
}
2017-10-17 09:27:15 +00:00
let apiEndpoint = arn.findAPIEndpoint(button)
try {
2018-04-25 16:59:23 +00:00
await arn.post(apiEndpoint + "/add/" + animeId)
arn.reloadContent()
// Show status message
let response = await fetch("/api/anime/" + animeId)
let anime = await response.json()
arn.statusMessage.showInfo(`Added ${anime.title.canonical} to your collection.`)
} catch(err) {
arn.statusMessage.showError(err)
}
2017-10-17 09:27:15 +00:00
}
// Remove anime from collection
2019-04-22 06:02:51 +00:00
export async function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElement) {
2018-03-14 21:52:56 +00:00
if(!confirm("Are you sure you want to remove it from your collection?")) {
return
}
2018-06-28 06:30:24 +00:00
button.textContent = "Removing..."
2017-10-17 09:27:15 +00:00
let {animeId, nick} = button.dataset
2019-04-22 06:02:51 +00:00
if(!animeId || !nick) {
console.error("Button without nick or anime ID:", button)
return
}
2017-10-17 09:27:15 +00:00
let apiEndpoint = arn.findAPIEndpoint(button)
2019-04-22 06:02:51 +00:00
let status = document.getElementById("Status") as HTMLSelectElement
2017-10-17 09:27:15 +00:00
2019-04-22 06:02:51 +00:00
try {
await arn.post(apiEndpoint + "/remove/" + animeId)
await arn.app.load(`/+${nick}/animelist/` + status.value)
} catch(err) {
arn.statusMessage.showError(err)
}
2017-10-17 09:27:15 +00:00
}