From 7a7a7145eec1786e664005faaf887c0311c898e8 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 4 Mar 2020 07:37:11 -0700 Subject: [PATCH] Fix using magic number as default category id --- src/frontend/forum.nim | 2 +- src/frontend/mainbuttons.nim | 6 +++--- src/frontend/threadlist.nim | 22 ++++++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/frontend/forum.nim b/src/frontend/forum.nim index d8fc003..bccbcf6 100644 --- a/src/frontend/forum.nim +++ b/src/frontend/forum.nim @@ -93,7 +93,7 @@ proc render(): VNode = ), r("/c/@id", (params: Params) => - (renderThreadList(getLoggedInUser(), params["id"].parseInt)) + (renderThreadList(getLoggedInUser(), some(params["id"].parseInt))) ), r("/newthread", (params: Params) => diff --git a/src/frontend/mainbuttons.nim b/src/frontend/mainbuttons.nim index a47bb09..9f4d4f8 100644 --- a/src/frontend/mainbuttons.nim +++ b/src/frontend/mainbuttons.nim @@ -27,7 +27,7 @@ when defined(js): let state = newState() - proc renderMainButtons*(currentUser: Option[User], categoryId = -1): VNode = + proc renderMainButtons*(currentUser: Option[User], categoryIdOption = none(int)): VNode = result = buildHtml(): section(class="navbar container grid-xl", id="main-buttons"): section(class="navbar-section"): @@ -38,8 +38,8 @@ when defined(js): ul(class="menu"): li: text "community" li: text "dev" ]# - if categoryId != -1: - state.categoryPicker.selectedCategoryID = categoryId + if categoryIdOption.isSome: + state.categoryPicker.selectedCategoryID = categoryIdOption.get() render(state.categoryPicker, currentUser, compact=false) for btn in buttons: diff --git a/src/frontend/threadlist.nim b/src/frontend/threadlist.nim index c93def1..f300cf9 100644 --- a/src/frontend/threadlist.nim +++ b/src/frontend/threadlist.nim @@ -147,10 +147,13 @@ when defined(js): else: state.list = some(list) - proc onLoadMore(ev: Event, n: VNode, categoryId: int) = + proc onLoadMore(ev: Event, n: VNode, categoryIdOption: Option[int]) = state.loading = true 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( list: seq[Thread], i: int, currentUser: Option[User] @@ -175,14 +178,17 @@ when defined(js): 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: return renderError("Couldn't retrieve threads.", state.status) if state.list.isNone: if not state.loading: 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")) @@ -223,10 +229,10 @@ when defined(js): tdiv(class="loading loading-lg") else: 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") - proc renderThreadList*(currentUser: Option[User], categoryId = -1): VNode = + proc renderThreadList*(currentUser: Option[User], categoryIdOption = none(int)): VNode = result = buildHtml(tdiv): - renderMainButtons(currentUser, categoryId=categoryId) - genThreadList(currentUser, categoryId) + renderMainButtons(currentUser, categoryIdOption=categoryIdOption) + genThreadList(currentUser, categoryIdOption)