Fix using magic number as default category id

This commit is contained in:
Joey Yakimowich-Payne 2020-03-04 07:37:11 -07:00 • committed by Dominik Picheta
commit 7a7a7145ee
3 changed files with 18 additions and 12 deletions

View file

@ -93,7 +93,7 @@ proc render(): VNode =
), ),
r("/c/@id", r("/c/@id",
(params: Params) => (params: Params) =>
(renderThreadList(getLoggedInUser(), params["id"].parseInt)) (renderThreadList(getLoggedInUser(), some(params["id"].parseInt)))
), ),
r("/newthread", r("/newthread",
(params: Params) => (params: Params) =>

View file

@ -27,7 +27,7 @@ when defined(js):
let state = newState() let state = newState()
proc renderMainButtons*(currentUser: Option[User], categoryId = -1): VNode = proc renderMainButtons*(currentUser: Option[User], categoryIdOption = none(int)): 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"):
@ -38,8 +38,8 @@ when defined(js):
ul(class="menu"): ul(class="menu"):
li: text "community" li: text "community"
li: text "dev" ]# li: text "dev" ]#
if categoryId != -1: if categoryIdOption.isSome:
state.categoryPicker.selectedCategoryID = categoryId state.categoryPicker.selectedCategoryID = categoryIdOption.get()
render(state.categoryPicker, currentUser, compact=false) render(state.categoryPicker, currentUser, compact=false)
for btn in buttons: for btn in buttons:

View file

@ -147,10 +147,13 @@ when defined(js):
else: else:
state.list = some(list) state.list = some(list)
proc onLoadMore(ev: Event, n: VNode, categoryId: int) = proc onLoadMore(ev: Event, n: VNode, categoryIdOption: Option[int]) =
state.loading = true state.loading = true
let start = state.list.get().threads.len let start = state.list.get().threads.len
ajaxGet(makeUri("threads.json?start=" & $start & "&categoryId=" & $categoryId), @[], onThreadList) if categoryIdOption.isSome:
ajaxGet(makeUri("threads.json?start=" & $start & "&categoryId=" & $categoryIdOption.get()), @[], onThreadList)
else:
ajaxGet(makeUri("threads.json?start=" & $start), @[], onThreadList)
proc getInfo( proc getInfo(
list: seq[Thread], i: int, currentUser: Option[User] list: seq[Thread], i: int, currentUser: Option[User]
@ -175,14 +178,17 @@ when defined(js):
isNew: thread.creation > previousVisitAt isNew: thread.creation > previousVisitAt
) )
proc genThreadList(currentUser: Option[User], categoryId: int): VNode = proc genThreadList(currentUser: Option[User], categoryIdOption: Option[int]): VNode =
if state.status != Http200: if state.status != Http200:
return renderError("Couldn't retrieve threads.", state.status) return renderError("Couldn't retrieve threads.", state.status)
if state.list.isNone: if state.list.isNone:
if not state.loading: if not state.loading:
state.loading = true state.loading = true
ajaxGet(makeUri("threads.json?categoryId=" & $categoryId), @[], onThreadList) if categoryIdOption.isSome:
ajaxGet(makeUri("threads.json?categoryId=" & $categoryIdOption.get()), @[], onThreadList)
else:
ajaxGet(makeUri("threads.json"), @[], onThreadList)
return buildHtml(tdiv(class="loading loading-lg")) return buildHtml(tdiv(class="loading loading-lg"))
@ -223,10 +229,10 @@ when defined(js):
tdiv(class="loading loading-lg") tdiv(class="loading loading-lg")
else: else:
td(colspan="6", td(colspan="6",
onClick = (ev: Event, n: VNode) => (onLoadMore(ev, n, categoryId))): onClick = (ev: Event, n: VNode) => (onLoadMore(ev, n, categoryIdOption))):
span(text "load more threads") span(text "load more threads")
proc renderThreadList*(currentUser: Option[User], categoryId = -1): VNode = proc renderThreadList*(currentUser: Option[User], categoryIdOption = none(int)): VNode =
result = buildHtml(tdiv): result = buildHtml(tdiv):
renderMainButtons(currentUser, categoryId=categoryId) renderMainButtons(currentUser, categoryIdOption=categoryIdOption)
genThreadList(currentUser, categoryId) genThreadList(currentUser, categoryIdOption)