76 lines
1.9 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)
}
2019-08-29 05:08:32 +00:00
}
// Delete anime list
export async function deleteAnimeList(arn: AnimeNotifier, button: HTMLElement) {
if(!confirm("Last confirmation: Are you sure you want to delete your entire anime list?")) {
return
}
button.textContent = "Deleting..."
let {returnPath} = button.dataset
if(!returnPath) {
console.error("Button without data-return-path:", button)
return
}
try {
await arn.post("/api/delete/animelist")
await arn.app.load(returnPath)
arn.statusMessage.showInfo("Your anime list has been deleted.")
} catch(err) {
arn.statusMessage.showError(err)
}
}