Added null checks and async functions

This commit is contained in:
Eduard Urbach 2019-04-22 15:02:51 +09:00
parent b1442858fe
commit 06daacbbf6
9 changed files with 158 additions and 75 deletions

View File

@ -5,6 +5,12 @@ export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButto
button.disabled = true button.disabled = true
let {animeId} = button.dataset let {animeId} = button.dataset
if(!animeId) {
console.error("Button without anime ID:", button)
return
}
let apiEndpoint = arn.findAPIEndpoint(button) let apiEndpoint = arn.findAPIEndpoint(button)
try { try {
@ -21,7 +27,7 @@ export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButto
} }
// Remove anime from collection // Remove anime from collection
export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElement) { export async function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElement) {
if(!confirm("Are you sure you want to remove it from your collection?")) { if(!confirm("Are you sure you want to remove it from your collection?")) {
return return
} }
@ -29,9 +35,19 @@ export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElemen
button.textContent = "Removing..." button.textContent = "Removing..."
let {animeId, nick} = button.dataset let {animeId, nick} = button.dataset
let apiEndpoint = arn.findAPIEndpoint(button)
arn.post(apiEndpoint + "/remove/" + animeId) if(!animeId || !nick) {
.then(() => arn.app.load(`/+${nick}/animelist/` + (document.getElementById("Status") as HTMLSelectElement).value)) console.error("Button without nick or anime ID:", button)
.catch(err => arn.statusMessage.showError(err)) return
}
let apiEndpoint = arn.findAPIEndpoint(button)
let status = document.getElementById("Status") as HTMLSelectElement
try {
await arn.post(apiEndpoint + "/remove/" + animeId)
await arn.app.load(`/+${nick}/animelist/` + status.value)
} catch(err) {
arn.statusMessage.showError(err)
}
} }

View File

@ -2,7 +2,14 @@ import AnimeNotifier from "../AnimeNotifier"
// Play audio // Play audio
export function playAudio(arn: AnimeNotifier, element: HTMLElement) { export function playAudio(arn: AnimeNotifier, element: HTMLElement) {
arn.audioPlayer.play(element.dataset.mediaId, element.dataset.audioSrc) let {mediaId, audioSrc} = element.dataset
if(!mediaId || !audioSrc) {
console.error("Invalid media ID or audio source:", element)
return
}
arn.audioPlayer.play(mediaId, audioSrc)
} }
// Pause audio // Pause audio

View File

@ -3,18 +3,20 @@ import { requestIdleCallback } from "../Utils"
// Load // Load
export function load(arn: AnimeNotifier, element: HTMLElement) { export function load(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href") let url = element.dataset.url || (element as HTMLAnchorElement).href
arn.app.load(url) arn.app.load(url)
} }
// Diff // Diff
export function diff(arn: AnimeNotifier, element: HTMLElement) { export async function diff(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href") let url = element.dataset.url || (element as HTMLAnchorElement).href
try {
await arn.diff(url)
arn.diff(url)
.then(() => {
// Avoid instant layout thrashing // Avoid instant layout thrashing
requestIdleCallback(() => arn.scrollTo(element)) requestIdleCallback(() => arn.scrollTo(element))
}) } catch(err) {
.catch(console.error) arn.statusMessage.showError(err)
}
} }

View File

@ -1,7 +1,7 @@
import AnimeNotifier from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
// newAnimeDiffIgnore // newAnimeDiffIgnore
export function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement) { export async function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement) {
if(!confirm("Are you sure you want to permanently ignore this difference?")) { if(!confirm("Are you sure you want to permanently ignore this difference?")) {
return return
} }
@ -9,14 +9,16 @@ export function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement
let id = button.dataset.id let id = button.dataset.id
let hash = button.dataset.hash let hash = button.dataset.hash
arn.post(`/api/new/ignoreanimedifference`, { try {
id, await arn.post(`/api/new/ignoreanimedifference`, {
hash id,
}) hash
.then(() => { })
arn.reloadContent() arn.reloadContent()
}) } catch(err) {
.catch(err => arn.statusMessage.showError(err)) arn.statusMessage.showError(err)
}
} }
// Import Kitsu anime // Import Kitsu anime
@ -26,6 +28,12 @@ export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonEle
} }
let newTab = window.open() let newTab = window.open()
if(!newTab) {
arn.statusMessage.showError("Error opening new tab")
return
}
let animeId = button.dataset.id let animeId = button.dataset.id
let response = await fetch(`/api/import/kitsu/anime/${animeId}`, { let response = await fetch(`/api/import/kitsu/anime/${animeId}`, {
method: "POST", method: "POST",

View File

@ -3,7 +3,7 @@ import { findAll } from "scripts/Utils"
// Filter anime on explore page // Filter anime on explore page
export function filterAnime(arn: AnimeNotifier, input: HTMLInputElement) { export function filterAnime(arn: AnimeNotifier, input: HTMLInputElement) {
let root = document.getElementById("filter-root") let root = document.getElementById("filter-root") as HTMLElement
let elementYear = document.getElementById("filter-year") as HTMLSelectElement let elementYear = document.getElementById("filter-year") as HTMLSelectElement
let elementSeason = document.getElementById("filter-season") as HTMLSelectElement let elementSeason = document.getElementById("filter-season") as HTMLSelectElement

View File

@ -4,24 +4,30 @@ import AnimeNotifier from "../AnimeNotifier"
export function editPost(arn: AnimeNotifier, element: HTMLElement) { export function editPost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id let postId = element.dataset.id
let render = document.getElementById("render-" + postId) if(!postId) {
let toolbar = document.getElementById("toolbar-" + postId) console.error("Post missing post ID:", postId)
let title = document.getElementById("title-" + postId) return
let source = document.getElementById("source-" + postId) }
let edit = document.getElementById("edit-toolbar-" + postId)
let render = document.getElementById("render-" + postId) as HTMLElement
let toolbar = document.getElementById("toolbar-" + postId) as HTMLElement
let source = document.getElementById("source-" + postId) as HTMLElement
let edit = document.getElementById("edit-toolbar-" + postId) as HTMLElement
render.classList.toggle("hidden") render.classList.toggle("hidden")
toolbar.classList.toggle("hidden") toolbar.classList.toggle("hidden")
source.classList.toggle("hidden") source.classList.toggle("hidden")
edit.classList.toggle("hidden") edit.classList.toggle("hidden")
let title = document.getElementById("title-" + postId)
if(title) { if(title) {
title.classList.toggle("hidden") title.classList.toggle("hidden")
} }
} }
// Save post // Save post
export function savePost(arn: AnimeNotifier, element: HTMLElement) { export async function savePost(arn: AnimeNotifier, element: HTMLElement) {
let postId = element.dataset.id let postId = element.dataset.id
let source = document.getElementById("source-" + postId) as HTMLTextAreaElement let source = document.getElementById("source-" + postId) as HTMLTextAreaElement
let title = document.getElementById("title-" + postId) as HTMLInputElement let title = document.getElementById("title-" + postId) as HTMLInputElement
@ -38,26 +44,32 @@ export function savePost(arn: AnimeNotifier, element: HTMLElement) {
let apiEndpoint = arn.findAPIEndpoint(element) let apiEndpoint = arn.findAPIEndpoint(element)
arn.post(apiEndpoint, updates) try {
.then(() => arn.reloadContent()) await arn.post(apiEndpoint, updates)
.catch(err => arn.statusMessage.showError(err)) arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Delete post // Delete post
export function deletePost(arn: AnimeNotifier, element: HTMLElement) { export async function deletePost(arn: AnimeNotifier, element: HTMLElement) {
if(!confirm(`Are you sure you want to delete this Post?`)) { if(!confirm(`Are you sure you want to delete this Post?`)) {
return return
} }
let endpoint = arn.findAPIEndpoint(element) let endpoint = arn.findAPIEndpoint(element)
arn.post(endpoint + "/delete") try {
.then(() => arn.reloadContent()) await arn.post(endpoint + "/delete")
.catch(err => arn.statusMessage.showError(err)) arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Create post // Create post
export function createPost(arn: AnimeNotifier, element: HTMLElement) { export async function createPost(arn: AnimeNotifier, element: HTMLElement) {
let textarea = document.getElementById("new-post-text") as HTMLTextAreaElement let textarea = document.getElementById("new-post-text") as HTMLTextAreaElement
let {parentId, parentType} = element.dataset let {parentId, parentType} = element.dataset
@ -68,14 +80,17 @@ export function createPost(arn: AnimeNotifier, element: HTMLElement) {
tags: [] tags: []
} }
arn.post("/api/new/post", post) try {
.then(() => arn.reloadContent()) await arn.post("/api/new/post", post)
.then(() => textarea.value = "") await arn.reloadContent()
.catch(err => arn.statusMessage.showError(err)) textarea.value = ""
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Create thread // Create thread
export function createThread(arn: AnimeNotifier) { export async function createThread(arn: AnimeNotifier) {
let title = document.getElementById("title") as HTMLInputElement let title = document.getElementById("title") as HTMLInputElement
let text = document.getElementById("text") as HTMLTextAreaElement let text = document.getElementById("text") as HTMLTextAreaElement
let category = document.getElementById("tag") as HTMLInputElement let category = document.getElementById("tag") as HTMLInputElement
@ -86,9 +101,12 @@ export function createThread(arn: AnimeNotifier) {
tags: [category.value] tags: [category.value]
} }
arn.post("/api/new/thread", thread) try {
.then(() => arn.app.load("/forum/" + thread.tags[0])) await arn.post("/api/new/thread", thread)
.catch(err => arn.statusMessage.showError(err)) await arn.app.load("/forum/" + thread.tags[0])
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Reply to a post // Reply to a post
@ -97,6 +115,11 @@ export async function reply(arn: AnimeNotifier, element: HTMLElement) {
let repliesId = `replies-${element.dataset.postId}` let repliesId = `replies-${element.dataset.postId}`
let replies = document.getElementById(repliesId) let replies = document.getElementById(repliesId)
if(!replies) {
console.error("Missing replies container:", element)
return
}
// Delete old reply area // Delete old reply area
let oldReplyArea = document.getElementById("new-post") let oldReplyArea = document.getElementById("new-post")
@ -130,16 +153,16 @@ export function cancelReply(arn: AnimeNotifier, element: HTMLElement) {
// Lock thread // Lock thread
export function lockThread(arn: AnimeNotifier, element: HTMLButtonElement) { export function lockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
setThreadLock(arn, element, true) return setThreadLock(arn, element, true)
} }
// Unlock thread // Unlock thread
export function unlockThread(arn: AnimeNotifier, element: HTMLButtonElement) { export function unlockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
setThreadLock(arn, element, false) return setThreadLock(arn, element, false)
} }
// Set thread locked state // Set thread locked state
function setThreadLock(arn: AnimeNotifier, element: HTMLButtonElement, state: boolean) { async function setThreadLock(arn: AnimeNotifier, element: HTMLButtonElement, state: boolean) {
let verb = state ? "lock" : "unlock" let verb = state ? "lock" : "unlock"
if(!confirm(`Are you sure you want to ${verb} this Thread?`)) { if(!confirm(`Are you sure you want to ${verb} this Thread?`)) {
@ -148,7 +171,10 @@ function setThreadLock(arn: AnimeNotifier, element: HTMLButtonElement, state: bo
let endpoint = arn.findAPIEndpoint(element) let endpoint = arn.findAPIEndpoint(element)
arn.post(`${endpoint}/${verb}`) try {
.then(() => arn.reloadContent()) await arn.post(`${endpoint}/${verb}`)
.catch(err => arn.statusMessage.showError(err)) await arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
} }

View File

@ -1,17 +1,30 @@
import AnimeNotifier from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
// New // New
export function newObject(arn: AnimeNotifier, button: HTMLButtonElement) { export async function newObject(arn: AnimeNotifier, button: HTMLButtonElement) {
let dataType = button.dataset.type let dataType = button.dataset.type
arn.post(`/api/new/${dataType}`) if(!dataType) {
.then(response => response.json()) console.error("Missing data type:", button)
.then(obj => arn.app.load(`/${dataType}/${obj.id}/edit`)) return
.catch(err => arn.statusMessage.showError(err)) }
try {
let response = await arn.post(`/api/new/${dataType}`)
if(!response) {
throw `Failed creating ${dataType}`
}
let json = await response.json()
await arn.app.load(`/${dataType}/${json.id}/edit`)
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Delete // Delete
export function deleteObject(arn: AnimeNotifier, button: HTMLButtonElement) { export async function deleteObject(arn: AnimeNotifier, button: HTMLButtonElement) {
let confirmType = button.dataset.confirmType let confirmType = button.dataset.confirmType
let returnPath = button.dataset.returnPath let returnPath = button.dataset.returnPath
@ -28,13 +41,15 @@ export function deleteObject(arn: AnimeNotifier, button: HTMLButtonElement) {
let endpoint = arn.findAPIEndpoint(button) let endpoint = arn.findAPIEndpoint(button)
arn.post(endpoint + "/delete") try {
.then(() => { await arn.post(endpoint + "/delete")
if(returnPath) { if(returnPath) {
arn.app.load(returnPath) await arn.app.load(returnPath)
} else { } else {
arn.reloadContent() await arn.reloadContent()
} }
}) } catch(err) {
.catch(err => arn.statusMessage.showError(err)) arn.statusMessage.showError(err)
}
} }

View File

@ -1,19 +1,25 @@
import AnimeNotifier from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
// Publish // Publish
export function publish(arn: AnimeNotifier, button: HTMLButtonElement) { export async function publish(arn: AnimeNotifier, button: HTMLButtonElement) {
let endpoint = arn.findAPIEndpoint(button) let endpoint = arn.findAPIEndpoint(button)
arn.post(endpoint + "/publish") try {
.then(() => arn.app.load(arn.app.currentPath.replace("/edit", ""))) await arn.post(endpoint + "/publish")
.catch(err => arn.statusMessage.showError(err)) await arn.app.load(arn.app.currentPath.replace("/edit", ""))
} catch(err) {
arn.statusMessage.showError(err)
}
} }
// Unpublish // Unpublish
export function unpublish(arn: AnimeNotifier, button: HTMLButtonElement) { export async function unpublish(arn: AnimeNotifier, button: HTMLButtonElement) {
let endpoint = arn.findAPIEndpoint(button) let endpoint = arn.findAPIEndpoint(button)
arn.post(endpoint + "/unpublish") try {
.then(() => arn.reloadContent()) await arn.post(endpoint + "/unpublish")
.catch(err => arn.statusMessage.showError(err)) await arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
} }

View File

@ -514,7 +514,7 @@ export default class AnimeNotifier {
e.dataTransfer.setData("text", element.dataset.index) e.dataTransfer.setData("text", element.dataset.index)
}, false) }, false)
element.addEventListener("dblclick", e => { element.addEventListener("dblclick", async e => {
if(!element.draggable || !element.dataset.index) { if(!element.draggable || !element.dataset.index) {
return return
} }
@ -527,10 +527,13 @@ export default class AnimeNotifier {
let apiEndpoint = this.findAPIEndpoint(element) let apiEndpoint = this.findAPIEndpoint(element)
this.post(apiEndpoint + "/use/" + element.dataset.index) try {
.then(() => this.reloadContent()) await this.post(apiEndpoint + "/use/" + element.dataset.index)
.then(() => this.statusMessage.showInfo(`You used ${itemName}.`)) await this.reloadContent()
.catch(err => this.statusMessage.showError(err)) this.statusMessage.showInfo(`You used ${itemName}.`)
} catch(err) {
this.statusMessage.showError(err)
}
}, false) }, false)
element.addEventListener("dragenter", e => { element.addEventListener("dragenter", e => {