Add 'add category button' to categories page
This commit is contained in:
parent
95b21874db
commit
9127bc4c88
2 changed files with 39 additions and 10 deletions
|
|
@ -3,25 +3,33 @@ import options, json, httpcore
|
||||||
import category
|
import category
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
|
import sugar
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
import karax / [vstyles, kajax]
|
import karax / [vstyles, kajax]
|
||||||
|
|
||||||
import karaxutils, error, user, mainbuttons
|
import karaxutils, error, user, mainbuttons, addcategorymodal
|
||||||
|
|
||||||
type
|
type
|
||||||
State = ref object
|
State = ref object
|
||||||
list: Option[CategoryList]
|
list: Option[CategoryList]
|
||||||
loading: bool
|
loading: bool
|
||||||
status: HttpCode
|
status: HttpCode
|
||||||
|
addCategoryModal: AddCategoryModal
|
||||||
|
|
||||||
|
var state: State
|
||||||
|
|
||||||
proc newState(): State =
|
proc newState(): State =
|
||||||
State(
|
State(
|
||||||
list: none[CategoryList](),
|
list: none[CategoryList](),
|
||||||
loading: false,
|
loading: false,
|
||||||
status: Http200
|
status: Http200,
|
||||||
|
addCategoryModal: newAddCategoryModal(
|
||||||
|
onAddCategory=
|
||||||
|
(category: Category) => state.list.get().categories.add(category)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
var
|
|
||||||
state = newState()
|
state = newState()
|
||||||
|
|
||||||
proc genCategory(category: Category, noBorder = false): VNode =
|
proc genCategory(category: Category, noBorder = false): VNode =
|
||||||
result = buildHtml():
|
result = buildHtml():
|
||||||
|
|
@ -50,7 +58,18 @@ when defined(js):
|
||||||
else:
|
else:
|
||||||
state.list = some(list)
|
state.list = some(list)
|
||||||
|
|
||||||
proc renderCategories(): VNode =
|
proc renderCategoryHeader*(currentUser: Option[User]): VNode =
|
||||||
|
result = buildHtml(tdiv(id="add-category")):
|
||||||
|
text "Category"
|
||||||
|
if currentUser.isAdmin():
|
||||||
|
button(class="plus-btn btn btn-link",
|
||||||
|
onClick=(ev: Event, n: VNode) => (
|
||||||
|
state.addCategoryModal.setModalShown(true)
|
||||||
|
)):
|
||||||
|
italic(class="fas fa-plus")
|
||||||
|
render(state.addCategoryModal)
|
||||||
|
|
||||||
|
proc renderCategories(currentUser: Option[User]): 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)
|
||||||
|
|
||||||
|
|
@ -68,7 +87,8 @@ when defined(js):
|
||||||
table(id="categories-list", class="table"):
|
table(id="categories-list", class="table"):
|
||||||
thead():
|
thead():
|
||||||
tr:
|
tr:
|
||||||
th(text "Category")
|
th:
|
||||||
|
renderCategoryHeader(currentUser)
|
||||||
th(text "Topics")
|
th(text "Topics")
|
||||||
tbody():
|
tbody():
|
||||||
for i in 0 ..< list.categories.len:
|
for i in 0 ..< list.categories.len:
|
||||||
|
|
@ -80,4 +100,4 @@ when defined(js):
|
||||||
proc renderCategoryList*(currentUser: Option[User]): VNode =
|
proc renderCategoryList*(currentUser: Option[User]): VNode =
|
||||||
result = buildHtml(tdiv):
|
result = buildHtml(tdiv):
|
||||||
renderMainButtons(currentUser)
|
renderMainButtons(currentUser)
|
||||||
renderCategories()
|
renderCategories(currentUser)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,16 @@ when defined(js):
|
||||||
let uri = makeUri("/c/" & $newCategory.id)
|
let uri = makeUri("/c/" & $newCategory.id)
|
||||||
navigateTo(uri)
|
navigateTo(uri)
|
||||||
|
|
||||||
let catPicker = newCategoryPicker(onCategoryChange=onSelectedCategoryChanged)
|
type
|
||||||
|
State = ref object
|
||||||
|
categoryPicker: CategoryPicker
|
||||||
|
|
||||||
|
proc newState(): State =
|
||||||
|
State(
|
||||||
|
categoryPicker: newCategoryPicker(onCategoryChange=onSelectedCategoryChanged),
|
||||||
|
)
|
||||||
|
|
||||||
|
let state = newState()
|
||||||
|
|
||||||
proc renderMainButtons*(currentUser: Option[User], categoryId = -1): VNode =
|
proc renderMainButtons*(currentUser: Option[User], categoryId = -1): VNode =
|
||||||
result = buildHtml():
|
result = buildHtml():
|
||||||
|
|
@ -30,8 +39,8 @@ when defined(js):
|
||||||
li: text "community"
|
li: text "community"
|
||||||
li: text "dev" ]#
|
li: text "dev" ]#
|
||||||
if categoryId != -1:
|
if categoryId != -1:
|
||||||
catPicker.selectedCategoryID = categoryId
|
state.categoryPicker.selectedCategoryID = categoryId
|
||||||
render(catPicker, currentUser, compact=false)
|
render(state.categoryPicker, currentUser, compact=false)
|
||||||
|
|
||||||
for btn in buttons:
|
for btn in buttons:
|
||||||
let active = btn.url == window.location.href
|
let active = btn.url == window.location.href
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue