Use Jester's pattern matcher for simple routing.
This commit is contained in:
parent
cf37fa34c4
commit
4176bdee3c
3 changed files with 29 additions and 10 deletions
|
|
@ -1046,8 +1046,8 @@ proc selectThread(threadRow: seq[string]): Thread =
|
||||||
views: threadRow[2].parseInt,
|
views: threadRow[2].parseInt,
|
||||||
activity: threadRow[3].parseInt,
|
activity: threadRow[3].parseInt,
|
||||||
creation: posts[1].parseInt,
|
creation: posts[1].parseInt,
|
||||||
isLocked: false,
|
isLocked: false, # TODO:
|
||||||
isSolved: false # TODO: ^ and this. Add a field to `post` to identify.
|
isSolved: false # TODO: Add a field to `post` to identify the solution.
|
||||||
)
|
)
|
||||||
|
|
||||||
# Gather the users list.
|
# Gather the users list.
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import strformat, times, options, json
|
import strformat, times, options, json, tables, future
|
||||||
from dom import window, Location
|
from dom import window, Location
|
||||||
|
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
|
import jester/patterns
|
||||||
|
|
||||||
import threadlist, postlist, karaxutils
|
import threadlist, postlist, karaxutils
|
||||||
|
|
||||||
|
|
@ -40,13 +40,32 @@ proc genHeader(): VNode =
|
||||||
italic(class="fas fa-sign-in-alt")
|
italic(class="fas fa-sign-in-alt")
|
||||||
text " Log in"
|
text " Log in"
|
||||||
|
|
||||||
|
const appName = "/karax"
|
||||||
|
type Params = Table[string, string]
|
||||||
|
type
|
||||||
|
Route = object
|
||||||
|
n: string
|
||||||
|
p: proc (params: Params): VNode
|
||||||
|
|
||||||
|
proc r(n: string, p: proc (params: Params): VNode): Route = Route(n: n, p: p)
|
||||||
|
proc route(routes: openarray[Route]): VNode =
|
||||||
|
for route in routes:
|
||||||
|
let pattern = (appName & route.n).parsePattern()
|
||||||
|
let (matched, params) = pattern.match($state.url.pathname)
|
||||||
|
if matched:
|
||||||
|
return route.p(params)
|
||||||
|
|
||||||
proc render(): VNode =
|
proc render(): VNode =
|
||||||
result = buildHtml(tdiv()):
|
result = buildHtml(tdiv()):
|
||||||
genHeader()
|
genHeader()
|
||||||
if "/t/" in state.url.pathname:
|
route([
|
||||||
renderPostList(3806, false)
|
r("/t/@id?",
|
||||||
else:
|
(params: Params) =>
|
||||||
renderThreadList()
|
(kout(params["id"].cstring);
|
||||||
|
renderPostList(params["id"].parseInt(), false))
|
||||||
|
),
|
||||||
|
r("/", (params: Params) => renderThreadList())
|
||||||
|
])
|
||||||
|
|
||||||
window.onPopState = onPopState
|
window.onPopState = onPopState
|
||||||
setRenderer render
|
setRenderer render
|
||||||
|
|
@ -56,7 +56,7 @@ when defined(js):
|
||||||
let parsed = parseJson($response)
|
let parsed = parseJson($response)
|
||||||
let list = to(parsed, PostList)
|
let list = to(parsed, PostList)
|
||||||
|
|
||||||
if state.list.isSome:
|
if state.list.isSome and state.list.get().thread.id == list.thread.id:
|
||||||
state.list.get().posts.add(list.posts)
|
state.list.get().posts.add(list.posts)
|
||||||
# TODO: Incorporate other possible changes?
|
# TODO: Incorporate other possible changes?
|
||||||
else:
|
else:
|
||||||
|
|
@ -101,7 +101,7 @@ when defined(js):
|
||||||
if state.status != Http200:
|
if state.status != Http200:
|
||||||
return renderError("Couldn't retrieve posts.")
|
return renderError("Couldn't retrieve posts.")
|
||||||
|
|
||||||
if state.list.isNone:
|
if state.list.isNone or state.list.get().thread.id != threadId:
|
||||||
ajaxGet(makeUri("posts.json?id=" & $threadId), @[], onPostList)
|
ajaxGet(makeUri("posts.json?id=" & $threadId), @[], onPostList)
|
||||||
|
|
||||||
return buildHtml(tdiv(class="loading loading-lg"))
|
return buildHtml(tdiv(class="loading loading-lg"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue