Added null checks and async functions

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

View File

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