Move everything related to ThreadList to the threadlist module.
This commit is contained in:
parent
2e95d078e1
commit
032d70a233
2 changed files with 46 additions and 32 deletions
|
|
@ -1,21 +1,15 @@
|
||||||
import strformat, times, options, json
|
import strformat, times, options, json
|
||||||
|
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
import karax / [vstyles, kajax]
|
|
||||||
|
|
||||||
import threadlist, karaxutils
|
import threadlist, karaxutils
|
||||||
|
|
||||||
type
|
type
|
||||||
State = ref object
|
State = ref object
|
||||||
list: Option[ThreadList]
|
|
||||||
|
|
||||||
proc newState(): State =
|
proc newState(): State =
|
||||||
State(
|
State()
|
||||||
list: none[ThreadList]()
|
|
||||||
)
|
|
||||||
|
|
||||||
const
|
|
||||||
baseUrl = "http://localhost:5000/"
|
|
||||||
|
|
||||||
proc genHeader(): VNode =
|
proc genHeader(): VNode =
|
||||||
result = buildHtml(header(id="main-navbar")):
|
result = buildHtml(header(id="main-navbar")):
|
||||||
|
|
@ -35,27 +29,9 @@ proc genHeader(): VNode =
|
||||||
|
|
||||||
var state = newState()
|
var state = newState()
|
||||||
|
|
||||||
proc onThreadList(httpStatus: int, response: kstring) =
|
|
||||||
let parsed = parseJson($response)
|
|
||||||
let list = to(parsed, ThreadList)
|
|
||||||
|
|
||||||
if state.list.isSome:
|
|
||||||
state.list.get().threads.add(list.threads)
|
|
||||||
state.list.get().moreCount = list.moreCount
|
|
||||||
state.list.get().lastVisit = list.lastVisit
|
|
||||||
else:
|
|
||||||
state.list = some(list)
|
|
||||||
|
|
||||||
proc render(): VNode =
|
proc render(): VNode =
|
||||||
if state.list.isNone:
|
|
||||||
ajaxGet(baseUrl & "threads.json", @[], onThreadList)
|
|
||||||
|
|
||||||
result = buildHtml(tdiv()):
|
result = buildHtml(tdiv()):
|
||||||
genHeader()
|
genHeader()
|
||||||
genTopButtons()
|
renderThreadList()
|
||||||
if state.list.isNone:
|
|
||||||
tdiv(class="loading loading-lg")
|
|
||||||
else:
|
|
||||||
genThreadList(state.list.get())
|
|
||||||
|
|
||||||
setRenderer render
|
setRenderer render
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import strformat, times
|
import strformat, times, options, json
|
||||||
|
|
||||||
type
|
type
|
||||||
User* = object
|
User* = object
|
||||||
|
|
@ -27,13 +27,28 @@ type
|
||||||
lastVisit*: int64 ## Unix timestamp
|
lastVisit*: int64 ## Unix timestamp
|
||||||
moreCount*: int ## How many more threads are left
|
moreCount*: int ## How many more threads are left
|
||||||
|
|
||||||
|
const
|
||||||
|
baseUrl = "http://localhost:5000/"
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
import karax / [vstyles]
|
import karax / [vstyles, kajax, kdom]
|
||||||
|
|
||||||
import karaxutils
|
import karaxutils
|
||||||
|
|
||||||
proc genTopButtons*(): VNode =
|
type
|
||||||
|
State = ref object
|
||||||
|
list: Option[ThreadList]
|
||||||
|
|
||||||
|
proc newState(): State =
|
||||||
|
State(
|
||||||
|
list: none[ThreadList]()
|
||||||
|
)
|
||||||
|
|
||||||
|
var
|
||||||
|
state = newState()
|
||||||
|
|
||||||
|
proc genTopButtons(): VNode =
|
||||||
result = buildHtml():
|
result = buildHtml():
|
||||||
section(class="navbar container grid-xl", id="main-buttons"):
|
section(class="navbar container grid-xl", id="main-buttons"):
|
||||||
section(class="navbar-section"):
|
section(class="navbar-section"):
|
||||||
|
|
@ -103,7 +118,24 @@ when defined(js):
|
||||||
td(class=class({"text-success": isNew, "text-gray": not isNew})): # TODO: Colors.
|
td(class=class({"text-success": isNew, "text-gray": not isNew})): # TODO: Colors.
|
||||||
text renderActivity(thread.activity)
|
text renderActivity(thread.activity)
|
||||||
|
|
||||||
proc genThreadList*(list: ThreadList): VNode =
|
proc onThreadList(httpStatus: int, response: kstring) =
|
||||||
|
let parsed = parseJson($response)
|
||||||
|
let list = to(parsed, ThreadList)
|
||||||
|
|
||||||
|
if state.list.isSome:
|
||||||
|
state.list.get().threads.add(list.threads)
|
||||||
|
state.list.get().moreCount = list.moreCount
|
||||||
|
state.list.get().lastVisit = list.lastVisit
|
||||||
|
else:
|
||||||
|
state.list = some(list)
|
||||||
|
|
||||||
|
proc genThreadList(): VNode =
|
||||||
|
if state.list.isNone:
|
||||||
|
ajaxGet(baseUrl & "threads.json", @[], onThreadList)
|
||||||
|
|
||||||
|
return buildHtml(tdiv(class="loading loading-lg"))
|
||||||
|
|
||||||
|
let list = state.list.get()
|
||||||
result = buildHtml():
|
result = buildHtml():
|
||||||
section(class="container grid-xl"): # TODO: Rename to `.thread-list`.
|
section(class="container grid-xl"): # TODO: Rename to `.thread-list`.
|
||||||
table(class="table"):
|
table(class="table"):
|
||||||
|
|
@ -119,7 +151,8 @@ when defined(js):
|
||||||
for i in 0 ..< list.threads.len:
|
for i in 0 ..< list.threads.len:
|
||||||
let thread = list.threads[i]
|
let thread = list.threads[i]
|
||||||
let isLastVisit =
|
let isLastVisit =
|
||||||
i+1 < list.threads.len and list.threads[i].activity < list.lastVisit
|
i+1 < list.threads.len and
|
||||||
|
list.threads[i].activity < list.lastVisit
|
||||||
let isNew = thread.creation < list.lastVisit
|
let isNew = thread.creation < list.lastVisit
|
||||||
genThread(thread, isNew,
|
genThread(thread, isNew,
|
||||||
noBorder=isLastVisit or i+1 == list.threads.len)
|
noBorder=isLastVisit or i+1 == list.threads.len)
|
||||||
|
|
@ -132,3 +165,8 @@ when defined(js):
|
||||||
tr(class="load-more-separator"):
|
tr(class="load-more-separator"):
|
||||||
td(colspan="6"):
|
td(colspan="6"):
|
||||||
span(text "load more threads")
|
span(text "load more threads")
|
||||||
|
|
||||||
|
proc renderThreadList*(): VNode =
|
||||||
|
result = buildHtml(tdiv):
|
||||||
|
genTopButtons()
|
||||||
|
genThreadList()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue