Reset states properly when navigating to new url.

This commit is contained in:
Dominik Picheta 2018-05-19 20:27:29 +01:00
commit 0a53392258
4 changed files with 34 additions and 6 deletions

View file

@ -58,10 +58,14 @@ when defined(js):
(s: int, r: kstring) => onEditPost(s, r, state))
proc render*(state: EditBox, post: Post): VNode =
if state.post.id != post.id:
state.rawContent = none[kstring]()
state.status = Http200
if state.status != Http200:
return renderError("Couldn't retrieve raw post", state.status)
if state.rawContent.isNone() or state.post.id != post.id:
if state.rawContent.isNone():
state.post = post
state.rawContent = none[kstring]()
var params = @[("id", $post.id)]

View file

@ -13,9 +13,22 @@ type
profile: ProfileState
newThread: NewThread
proc copyLocation(loc: Location): Location =
# TODO: It sucks that I had to do this. We need a nice way to deep copy in JS.
Location(
hash: loc.hash,
host: loc.host,
hostname: loc.hostname,
href: loc.href,
pathname: loc.pathname,
port: loc.port,
protocol: loc.protocol,
search: loc.search
)
proc newState(): State =
State(
url: window.location,
url: copyLocation(window.location),
profile: newProfileState(),
newThread: newNewThread()
)
@ -25,8 +38,11 @@ proc onPopState(event: dom.Event) =
# This event is usually only called when the user moves back in their
# history. I fire it in karaxutils.anchorCB as well to ensure the URL is
# always updated. This should be moved into Karax in the future.
kout(kstring"New URL: ", window.location.href)
state.url = window.location
kout(kstring"New URL: ", window.location.href, " ", state.url.href)
if state.url.href != window.location.href:
state = newState() # Reload the state to remove stale data.
state.url = copyLocation(window.location)
redraw()
type Params = Table[string, string]

View file

@ -307,10 +307,14 @@ when defined(js):
proc renderPostList*(threadId: int, postId: Option[int],
currentUser: Option[User]): VNode =
if state.list.isSome() and state.list.get().thread.id != threadId:
state.list = none[PostList]()
state.status = Http200
if state.status != Http200:
return renderError("Couldn't retrieve posts.", state.status)
if state.list.isNone or state.list.get().thread.id != threadId:
if state.list.isNone:
var params = @[("id", $threadId)]
if postId.isSome():
params.add(("anchor", $postId.get()))

View file

@ -57,10 +57,14 @@ when defined(js):
username: string,
currentUser: Option[User]
): VNode =
if state.profile.isSome() and state.profile.get().user.name != username:
state.profile = none[Profile]()
state.status = Http200
if state.status != Http200:
return renderError("Couldn't retrieve profile.", state.status)
if state.profile.isNone or state.profile.get().user.name != username:
if state.profile.isNone:
let uri = makeUri("profile.json", ("username", username))
ajaxGet(uri, @[], (s: int, r: kstring) => onProfile(s, r, state))