From 8758baa0face9eab01c5efc1f9d555ae5244c867 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 27 Jun 2017 04:15:52 +0200 Subject: [PATCH] Create new thread --- main.go | 2 ++ pages/forum/forum.pixy | 8 +++++-- pages/forum/forum.scarlet | 7 ++++-- pages/newthread/newthread.go | 20 +++++++++++++++++ pages/newthread/newthread.pixy | 20 +++++++++++++++++ scripts/AnimeNotifier.ts | 16 +++++++++++++- scripts/actions.ts | 40 ++++++++++++++++++++++------------ 7 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 pages/newthread/newthread.go create mode 100644 pages/newthread/newthread.pixy diff --git a/main.go b/main.go index c2993c4e..e97f2b24 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/pages/forum/forum.pixy b/pages/forum/forum.pixy index dd4f9ee1..a248d5ed 100644 --- a/pages/forum/forum.pixy +++ b/pages/forum/forum.pixy @@ -4,8 +4,12 @@ component Forum(tag string, threads []*arn.Thread, threadsPerPage int) .forum ThreadList(threads) - if len(threads) == threadsPerPage - .buttons + + .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 diff --git a/pages/forum/forum.scarlet b/pages/forum/forum.scarlet index 4775f365..79f1ee73 100644 --- a/pages/forum/forum.scarlet +++ b/pages/forum/forum.scarlet @@ -29,5 +29,8 @@ .forum-tag-text display none -#load-more-threads - margin-top 1rem \ No newline at end of file +> 1250px + #new-thread + position fixed + right content-padding + bottom content-padding diff --git a/pages/newthread/newthread.go b/pages/newthread/newthread.go new file mode 100644 index 00000000..614af029 --- /dev/null +++ b/pages/newthread/newthread.go @@ -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)) +} diff --git a/pages/newthread/newthread.pixy b/pages/newthread/newthread.pixy new file mode 100644 index 00000000..bd83d888 --- /dev/null +++ b/pages/newthread/newthread.pixy @@ -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 \ No newline at end of file diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index fdc2f926..56052c01 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -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, { diff --git a/scripts/actions.ts b/scripts/actions.ts index 3d9bdd1b..c575d55a 100644 --- a/scripts/actions.ts +++ b/scripts/actions.ts @@ -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 - } - - textarea.value = "" - }) + 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] + } + + arn.post("/api/thread/new", thread) + .then(() => arn.app.load("/forum/" + thread.tags[0])) .catch(console.error) }