179 lines
4.2 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
// Edit post
2019-04-22 09:06:50 +00:00
export function editPost(_: AnimeNotifier, element: HTMLElement) {
2019-11-17 09:25:14 +00:00
const postId = element.dataset.id
2017-10-17 09:27:15 +00:00
2019-04-22 06:02:51 +00:00
if(!postId) {
console.error("Post missing post ID:", postId)
return
}
2019-11-17 09:25:14 +00:00
const render = document.getElementById("render-" + postId) as HTMLElement
const source = document.getElementById("source-" + postId) as HTMLElement
const edit = document.getElementById("edit-toolbar-" + postId) as HTMLElement
2017-10-17 09:27:15 +00:00
render.classList.toggle("hidden")
source.classList.toggle("hidden")
edit.classList.toggle("hidden")
2019-11-17 09:25:14 +00:00
const title = document.getElementById("title-" + postId)
2019-04-22 06:02:51 +00:00
2017-10-17 09:27:15 +00:00
if(title) {
title.classList.toggle("hidden")
}
}
// Save post
2019-04-22 06:02:51 +00:00
export async function savePost(arn: AnimeNotifier, element: HTMLElement) {
2019-11-17 09:25:14 +00:00
const postId = element.dataset.id
const source = document.getElementById("source-" + postId) as HTMLTextAreaElement
const title = document.getElementById("title-" + postId) as HTMLInputElement
const text = source.value
2017-10-17 09:27:15 +00:00
2019-11-17 09:25:14 +00:00
const updates: any = {
2017-10-17 09:27:15 +00:00
Text: text,
}
// Add title for threads only
if(title) {
updates.Title = title.value
}
2019-11-17 09:25:14 +00:00
const apiEndpoint = arn.findAPIEndpoint(element)
2017-10-17 09:27:15 +00:00
2019-04-22 06:02:51 +00:00
try {
await arn.post(apiEndpoint, updates)
arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
2017-10-17 09:27:15 +00:00
}
2017-11-25 14:26:56 +00:00
// Delete post
2019-04-22 06:02:51 +00:00
export async function deletePost(arn: AnimeNotifier, element: HTMLElement) {
2017-11-25 14:26:56 +00:00
if(!confirm(`Are you sure you want to delete this Post?`)) {
return
}
2019-11-17 09:25:14 +00:00
const endpoint = arn.findAPIEndpoint(element)
2019-04-22 06:02:51 +00:00
try {
await arn.post(endpoint + "/delete")
arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
}
2018-10-29 02:30:23 +00:00
// Create post
2019-04-22 06:02:51 +00:00
export async function createPost(arn: AnimeNotifier, element: HTMLElement) {
2019-11-17 09:25:14 +00:00
const textarea = document.getElementById("new-post-text") as HTMLTextAreaElement
const {parentId, parentType} = element.dataset
2017-10-17 09:27:15 +00:00
2019-11-17 09:25:14 +00:00
const post = {
2017-10-17 09:27:15 +00:00
text: textarea.value,
2018-10-29 02:30:23 +00:00
parentId,
parentType,
2017-10-17 09:27:15 +00:00
tags: []
}
2019-04-22 06:02:51 +00:00
try {
await arn.post("/api/new/post", post)
await arn.reloadContent()
textarea.value = ""
} catch(err) {
arn.statusMessage.showError(err)
}
2017-10-17 09:27:15 +00:00
}
// Create thread
2019-04-22 06:02:51 +00:00
export async function createThread(arn: AnimeNotifier) {
2019-11-17 09:25:14 +00:00
const title = document.getElementById("title") as HTMLInputElement
const text = document.getElementById("text") as HTMLTextAreaElement
const category = document.getElementById("tag") as HTMLInputElement
2017-10-17 09:27:15 +00:00
2019-11-17 09:25:14 +00:00
const thread = {
2017-10-17 09:27:15 +00:00
title: title.value,
text: text.value,
tags: [category.value]
}
2019-04-22 06:02:51 +00:00
try {
await arn.post("/api/new/thread", thread)
await arn.app.load("/forum/" + thread.tags[0])
} catch(err) {
arn.statusMessage.showError(err)
}
2018-04-25 16:59:23 +00:00
}
2018-11-05 10:05:36 +00:00
// Reply to a post
2018-11-05 11:57:37 +00:00
export async function reply(arn: AnimeNotifier, element: HTMLElement) {
2019-11-17 09:25:14 +00:00
const apiEndpoint = arn.findAPIEndpoint(element)
const repliesId = `replies-${element.dataset.postId}`
const replies = document.getElementById(repliesId)
2018-11-05 11:57:37 +00:00
2019-04-22 06:02:51 +00:00
if(!replies) {
console.error("Missing replies container:", element)
return
}
2018-11-05 11:57:37 +00:00
// Delete old reply area
2019-11-17 09:25:14 +00:00
const oldReplyArea = document.getElementById("new-post")
2018-11-05 11:57:37 +00:00
if(oldReplyArea) {
oldReplyArea.remove()
}
// Delete old reply button
2019-11-17 09:25:14 +00:00
const oldPostActions = document.getElementsByClassName("new-post-actions")[0]
2018-11-05 11:57:37 +00:00
if(oldPostActions) {
oldPostActions.remove()
}
// Fetch new reply UI
try {
2019-11-17 09:25:14 +00:00
const response = await fetch(`${apiEndpoint}/reply/ui`)
const html = await response.text()
2018-11-05 11:57:37 +00:00
replies.innerHTML = html + replies.innerHTML
arn.onNewContent(replies)
arn.assignActions()
} catch(err) {
arn.statusMessage.showError(err)
}
}
2018-11-05 10:05:36 +00:00
2018-11-05 11:57:37 +00:00
// Cancel replying to a post
2019-04-22 09:06:50 +00:00
export function cancelReply(arn: AnimeNotifier, _: HTMLElement) {
2018-11-05 11:57:37 +00:00
arn.reloadContent()
2018-11-05 10:05:36 +00:00
}
2018-04-25 16:59:23 +00:00
// Lock thread
export function lockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
2019-04-22 06:02:51 +00:00
return setThreadLock(arn, element, true)
2018-04-25 16:59:23 +00:00
}
// Unlock thread
export function unlockThread(arn: AnimeNotifier, element: HTMLButtonElement) {
2019-04-22 06:02:51 +00:00
return setThreadLock(arn, element, false)
2018-04-25 16:59:23 +00:00
}
// Set thread locked state
2019-04-22 06:02:51 +00:00
async function setThreadLock(arn: AnimeNotifier, element: HTMLButtonElement, state: boolean) {
2019-11-17 09:25:14 +00:00
const verb = state ? "lock" : "unlock"
2018-04-25 16:59:23 +00:00
if(!confirm(`Are you sure you want to ${verb} this Thread?`)) {
return
}
2019-11-17 09:25:14 +00:00
const endpoint = arn.findAPIEndpoint(element)
2018-04-25 16:59:23 +00:00
2019-04-22 06:02:51 +00:00
try {
await arn.post(`${endpoint}/${verb}`)
await arn.reloadContent()
} catch(err) {
arn.statusMessage.showError(err)
}
2019-11-17 09:44:30 +00:00
}