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
2019-11-17 09:25:14 +00:00
const {animeId} = button.dataset
2019-04-22 06:02:51 +00:00
if(!animeId) {
console.error("Button without anime ID:", button)
return
}
2019-11-17 09:25:14 +00:00
const apiEndpoint = arn.findAPIEndpoint(button)
2017-10-17 09:27:15 +00:00
try {
2018-04-25 16:59:23 +00:00
await arn.post(apiEndpoint + "/add/" + animeId)
arn.reloadContent()
// Show status message
2019-11-17 09:25:14 +00:00
const response = await fetch("/api/anime/" + animeId)
const 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..."
2019-11-17 09:25:14 +00:00
const {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
}
2019-11-17 09:25:14 +00:00
const apiEndpoint = arn.findAPIEndpoint(button)
const 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..."
2019-11-17 09:25:14 +00:00
const {returnPath} = button.dataset
2019-08-29 05:08:32 +00:00
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)
}
}