Refactor on*Post procedures.
This commit is contained in:
parent
7635478b34
commit
7f895123f9
3 changed files with 23 additions and 49 deletions
|
|
@ -41,3 +41,22 @@ when defined(js):
|
||||||
if (e.errorFields.len == 1 and e.errorFields[0] == name) or isLast:
|
if (e.errorFields.len == 1 and e.errorFields[0] == name) or isLast:
|
||||||
p(class="form-input-hint"):
|
p(class="form-input-hint"):
|
||||||
text e.message
|
text e.message
|
||||||
|
|
||||||
|
template postFinished*(onSuccess: untyped): untyped =
|
||||||
|
state.loading = false
|
||||||
|
let status = httpStatus.HttpCode
|
||||||
|
if status == Http200:
|
||||||
|
onSuccess
|
||||||
|
else:
|
||||||
|
# TODO: Karax should pass the content-type...
|
||||||
|
try:
|
||||||
|
let parsed = parseJson($response)
|
||||||
|
let error = to(parsed, PostError)
|
||||||
|
|
||||||
|
state.error = some(error)
|
||||||
|
except:
|
||||||
|
kout(getCurrentExceptionMsg().cstring)
|
||||||
|
state.error = some(PostError(
|
||||||
|
errorFields: @[],
|
||||||
|
message: "Unknown error occurred."
|
||||||
|
))
|
||||||
|
|
@ -17,24 +17,9 @@ when defined(js):
|
||||||
error: Option[PostError]
|
error: Option[PostError]
|
||||||
|
|
||||||
proc onLogInPost(httpStatus: int, response: kstring, state: LoginModal) =
|
proc onLogInPost(httpStatus: int, response: kstring, state: LoginModal) =
|
||||||
state.loading = false
|
postFinished:
|
||||||
let status = httpStatus.HttpCode
|
|
||||||
if status == Http200:
|
|
||||||
state.shown = false
|
state.shown = false
|
||||||
state.onLogIn()
|
state.onLogIn()
|
||||||
else:
|
|
||||||
# TODO: Karax should pass the content-type...
|
|
||||||
try:
|
|
||||||
let parsed = parseJson($response)
|
|
||||||
let error = to(parsed, PostError)
|
|
||||||
|
|
||||||
state.error = some(error)
|
|
||||||
except:
|
|
||||||
kout(getCurrentExceptionMsg().cstring)
|
|
||||||
state.error = some(PostError(
|
|
||||||
errorFields: @[],
|
|
||||||
message: "Unknown error occurred."
|
|
||||||
))
|
|
||||||
|
|
||||||
proc onLogInClick(ev: Event, n: VNode, state: LoginModal) =
|
proc onLogInClick(ev: Event, n: VNode, state: LoginModal) =
|
||||||
state.loading = true
|
state.loading = true
|
||||||
|
|
|
||||||
|
|
@ -40,24 +40,9 @@ when defined(js):
|
||||||
state.shown = true
|
state.shown = true
|
||||||
|
|
||||||
proc onPreviewPost(httpStatus: int, response: kstring, state: ReplyBox) =
|
proc onPreviewPost(httpStatus: int, response: kstring, state: ReplyBox) =
|
||||||
state.loading = false
|
postFinished:
|
||||||
let status = httpStatus.HttpCode
|
|
||||||
if status == Http200:
|
|
||||||
kout(response)
|
kout(response)
|
||||||
state.rendering = some[kstring](response)
|
state.rendering = some[kstring](response)
|
||||||
else:
|
|
||||||
# TODO: login has similar code, abstract this.
|
|
||||||
try:
|
|
||||||
let parsed = parseJson($response)
|
|
||||||
let error = to(parsed, PostError)
|
|
||||||
|
|
||||||
state.error = some(error)
|
|
||||||
except:
|
|
||||||
kout(getCurrentExceptionMsg().cstring)
|
|
||||||
state.error = some(PostError(
|
|
||||||
errorFields: @[],
|
|
||||||
message: "Unknown error occurred."
|
|
||||||
))
|
|
||||||
|
|
||||||
proc onPreviewClick(e: Event, n: VNode, state: ReplyBox) =
|
proc onPreviewClick(e: Event, n: VNode, state: ReplyBox) =
|
||||||
state.preview = true
|
state.preview = true
|
||||||
|
|
@ -71,25 +56,10 @@ when defined(js):
|
||||||
(s: int, r: kstring) => onPreviewPost(s, r, state))
|
(s: int, r: kstring) => onPreviewPost(s, r, state))
|
||||||
|
|
||||||
proc onReplyPost(httpStatus: int, response: kstring, state: ReplyBox) =
|
proc onReplyPost(httpStatus: int, response: kstring, state: ReplyBox) =
|
||||||
state.loading = false
|
postFinished:
|
||||||
let status = httpStatus.HttpCode
|
|
||||||
if status == Http200:
|
|
||||||
state.text = ""
|
state.text = ""
|
||||||
state.shown = false
|
state.shown = false
|
||||||
state.onPost(parseJson($response).getInt())
|
state.onPost(parseJson($response).getInt())
|
||||||
else:
|
|
||||||
# TODO: login has similar code, abstract this.
|
|
||||||
try:
|
|
||||||
let parsed = parseJson($response)
|
|
||||||
let error = to(parsed, PostError)
|
|
||||||
|
|
||||||
state.error = some(error)
|
|
||||||
except:
|
|
||||||
kout(getCurrentExceptionMsg().cstring)
|
|
||||||
state.error = some(PostError(
|
|
||||||
errorFields: @[],
|
|
||||||
message: "Unknown error occurred."
|
|
||||||
))
|
|
||||||
|
|
||||||
proc onReplyClick(e: Event, n: VNode, state: ReplyBox,
|
proc onReplyClick(e: Event, n: VNode, state: ReplyBox,
|
||||||
thread: Thread, replyingTo: Option[Post]) =
|
thread: Thread, replyingTo: Option[Post]) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue