Create new thread

This commit is contained in:
Eduard Urbach 2017-06-27 04:15:52 +02:00
parent 4d24b817ff
commit 8758baa0fa
7 changed files with 94 additions and 19 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/animenotifier/notify.moe/pages/forum"
"github.com/animenotifier/notify.moe/pages/forums"
"github.com/animenotifier/notify.moe/pages/login"
"github.com/animenotifier/notify.moe/pages/newthread"
"github.com/animenotifier/notify.moe/pages/popularanime"
"github.com/animenotifier/notify.moe/pages/posts"
"github.com/animenotifier/notify.moe/pages/profile"
@ -64,6 +65,7 @@ func configure(app *aero.Application) *aero.Application {
app.Ajax("/user/:nick/posts", profile.GetPostsByUser)
app.Ajax("/user/:nick/animelist", animelist.Get)
app.Ajax("/user/:nick/animelist/:id", animelistitem.Get)
app.Ajax("/new/thread", newthread.Get)
app.Ajax("/settings", settings.Get)
app.Ajax("/admin", admin.Get)
app.Ajax("/search/:term", search.Get)

View File

@ -4,8 +4,12 @@ component Forum(tag string, threads []*arn.Thread, threadsPerPage int)
.forum
ThreadList(threads)
if len(threads) == threadsPerPage
.buttons
button#new-thread.action(data-action="load", data-trigger="click", data-url="/new/thread")
Icon("plus")
span New thread
if len(threads) == threadsPerPage
button
Icon("refresh")
span Load more

View File

@ -29,5 +29,8 @@
.forum-tag-text
display none
#load-more-threads
margin-top 1rem
> 1250px
#new-thread
position fixed
right content-padding
bottom content-padding

View File

@ -0,0 +1,20 @@
package newthread
import (
"net/http"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// Get forums page.
func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
return ctx.HTML(components.NewThread(user))
}

View File

@ -0,0 +1,20 @@
component NewThread(user *arn.User)
.widgets
.widget
input#title.widget-element(type="text", placeholder="Title")
textarea#text.widget-element(placeholder="Content")
select#tag.widget-element
option(value="general") General
option(value="news") News
option(value="anime") Anime
option(value="bug") Bug
option(value="suggestion") Suggestion
if user.Role == "admin"
option(value="update") Update
button.action(data-action="createThread", data-trigger="click")
Icon("check")
span Create thread

View File

@ -153,7 +153,7 @@ export class AnimeNotifier {
}
}
load(url: string) {
diff(url: string) {
let request = fetch("/_" + url, {
credentials: "same-origin"
})
@ -175,6 +175,20 @@ export class AnimeNotifier {
})
}
post(url, obj) {
return fetch(url, {
method: "POST",
body: JSON.stringify(obj),
credentials: "same-origin"
})
.then(response => response.text())
.then(body => {
if(body !== "ok") {
throw body
}
})
}
onPopState(e: PopStateEvent) {
if(e.state) {
this.app.load(e.state, {

View File

@ -57,10 +57,16 @@ export function save(arn: AnimeNotifier, input: HTMLInputElement | HTMLTextAreaE
})
}
// Load
export function load(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href")
arn.app.load(url)
}
// Diff
export function diff(arn: AnimeNotifier, element: HTMLElement) {
let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href")
arn.load(url)
arn.diff(url)
}
// Forum reply
@ -74,20 +80,26 @@ export function forumReply(arn: AnimeNotifier) {
tags: []
}
fetch("/api/post/new", {
method: "POST",
body: JSON.stringify(post),
credentials: "same-origin"
})
.then(response => response.text())
.then(body => {
if(body !== "ok") {
throw body
arn.post("/api/post/new", post)
.then(() => arn.reloadContent())
.then(() => textarea.value = "")
.catch(console.error)
}
// Create thread
export function createThread(arn: AnimeNotifier) {
let title = arn.app.find("title") as HTMLInputElement
let text = arn.app.find("text") as HTMLTextAreaElement
let category = arn.app.find("tag") as HTMLInputElement
let thread = {
title: title.value,
text: text.value,
tags: [category.value]
}
textarea.value = ""
})
.then(() => arn.reloadContent())
arn.post("/api/thread/new", thread)
.then(() => arn.app.load("/forum/" + thread.tags[0]))
.catch(console.error)
}