Implements post button for requesting a password reset.
This commit is contained in:
parent
81e5bf5af0
commit
581eba73e3
3 changed files with 90 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ type
|
||||||
message*: string
|
message*: string
|
||||||
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
|
import json
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
import karax / [vstyles, kajax, kdom]
|
import karax / [vstyles, kajax, kdom]
|
||||||
|
|
||||||
|
|
|
||||||
77
redesign/postbutton.nim
Normal file
77
redesign/postbutton.nim
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
## Simple generic button that can be clicked to make a post request.
|
||||||
|
## The button will show a loading indicator and a tick on success.
|
||||||
|
##
|
||||||
|
## Used for password reset emails.
|
||||||
|
|
||||||
|
import options, httpcore, json, sugar
|
||||||
|
when defined(js):
|
||||||
|
include karax/prelude
|
||||||
|
import karax/[kajax, kdom]
|
||||||
|
|
||||||
|
import error, karaxutils
|
||||||
|
|
||||||
|
type
|
||||||
|
PostButton* = ref object
|
||||||
|
uri, title, icon: string
|
||||||
|
formData: FormData
|
||||||
|
error: Option[PostError]
|
||||||
|
loading: bool
|
||||||
|
posted: bool
|
||||||
|
|
||||||
|
proc newPostButton*(uri: string, formData: FormData,
|
||||||
|
title: string, icon: string): PostButton =
|
||||||
|
PostButton(
|
||||||
|
uri: uri,
|
||||||
|
formData: formData,
|
||||||
|
title: title,
|
||||||
|
icon: icon
|
||||||
|
)
|
||||||
|
|
||||||
|
proc newResetPasswordButton*(email: string): PostButton =
|
||||||
|
var formData = newFormData()
|
||||||
|
formData.append("email", email)
|
||||||
|
result = newPostButton(
|
||||||
|
makeUri("/resetPassword"),
|
||||||
|
formData,
|
||||||
|
"Send password reset email",
|
||||||
|
"fas fa-envelope",
|
||||||
|
)
|
||||||
|
|
||||||
|
proc onPost(httpStatus: int, response: kstring, state: PostButton) =
|
||||||
|
postFinished:
|
||||||
|
discard
|
||||||
|
|
||||||
|
proc onClick(ev: Event, n: VNode, state: PostButton) =
|
||||||
|
if state.loading or state.posted: return
|
||||||
|
|
||||||
|
state.loading = true
|
||||||
|
state.posted = true
|
||||||
|
state.error = none[PostError]()
|
||||||
|
|
||||||
|
# TODO: This is a hack, karax should support this.
|
||||||
|
ajaxPost(state.uri, @[], cast[cstring](state.formData),
|
||||||
|
(s: int, r: kstring) => onPost(s, r, state))
|
||||||
|
|
||||||
|
ev.preventDefault()
|
||||||
|
|
||||||
|
proc render*(state: PostButton, disabled: bool): VNode =
|
||||||
|
result = buildHtml(tdiv()):
|
||||||
|
button(class=class({
|
||||||
|
"loading": state.loading,
|
||||||
|
"disabled": disabled
|
||||||
|
},
|
||||||
|
"btn btn-secondary"
|
||||||
|
),
|
||||||
|
onClick=(e: Event, n: VNode) => (onClick(e, n, state))):
|
||||||
|
if state.posted:
|
||||||
|
if state.error.isNone():
|
||||||
|
italic(class="fas fa-check")
|
||||||
|
else:
|
||||||
|
italic(class="fas fa-times")
|
||||||
|
else:
|
||||||
|
italic(class=state.icon)
|
||||||
|
text " " & state.title
|
||||||
|
|
||||||
|
if state.error.isSome():
|
||||||
|
p(class="text-error"):
|
||||||
|
text state.error.get().message
|
||||||
|
|
@ -16,7 +16,7 @@ type
|
||||||
when defined(js):
|
when defined(js):
|
||||||
include karax/prelude
|
include karax/prelude
|
||||||
import karax/[kajax]
|
import karax/[kajax]
|
||||||
import karaxutils
|
import karaxutils, postbutton
|
||||||
|
|
||||||
type
|
type
|
||||||
ProfileTab* = enum
|
ProfileTab* = enum
|
||||||
|
|
@ -32,6 +32,7 @@ when defined(js):
|
||||||
currentTab: ProfileTab
|
currentTab: ProfileTab
|
||||||
loading: bool
|
loading: bool
|
||||||
status: HttpCode
|
status: HttpCode
|
||||||
|
resetPassword: Option[PostButton]
|
||||||
|
|
||||||
proc newProfileState*(): ProfileState =
|
proc newProfileState*(): ProfileState =
|
||||||
ProfileState(
|
ProfileState(
|
||||||
|
|
@ -63,6 +64,8 @@ when defined(js):
|
||||||
|
|
||||||
state.profile = some(profile)
|
state.profile = some(profile)
|
||||||
resetSettings(state)
|
resetSettings(state)
|
||||||
|
if profile.email.isSome():
|
||||||
|
state.resetPassword = some(newResetPasswordButton(profile.email.get()))
|
||||||
|
|
||||||
proc genPostLink(link: PostLink): VNode =
|
proc genPostLink(link: PostLink): VNode =
|
||||||
let url = renderPostUrl(link)
|
let url = renderPostUrl(link)
|
||||||
|
|
@ -244,6 +247,14 @@ when defined(js):
|
||||||
text "Rank"
|
text "Rank"
|
||||||
tdiv(class="col-9 col-sm-12"):
|
tdiv(class="col-9 col-sm-12"):
|
||||||
rankSelect
|
rankSelect
|
||||||
|
if state.resetPassword.isSome():
|
||||||
|
tdiv(class="form-group"):
|
||||||
|
tdiv(class="col-3 col-sm-12"):
|
||||||
|
label(class="form-label"):
|
||||||
|
text "Password"
|
||||||
|
tdiv(class="col-9 col-sm-12"):
|
||||||
|
render(state.resetPassword.get(),
|
||||||
|
disabled=state.settings.rank==EmailUnconfirmed)
|
||||||
|
|
||||||
tdiv(class="float-right"):
|
tdiv(class="float-right"):
|
||||||
button(class="btn btn-link",
|
button(class="btn btn-link",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue