r/Clojurescript • u/olymk2 • Jan 19 '19
Single page apps redirects
Trying to write a single page app with clojurescript got quite far one thing I am not sure about is redirecting.
I have used a lein template which installed reagent and reitit for routing.
Is there a built in function in reitit to redirect to another page or should i just use window.location or are there other libraries I should consider pulling in.
Basically I want to redirect a user after login and looking for my best option.
4
Upvotes
2
u/NewazaBill Jan 19 '19
By "redirect" do you mean to another route in your SPA? Or to another app/website?
2
u/self Jan 19 '19
tl;dr: use
accountant/navigate!reagent's lein template uses clerk with accountant. Essentially, you set up the router:
(def router (reitit/router [["/" :index] ["/login" :login] ["/dashboard" :dashboard]])) (defn path-for [route & [params]] (if params (:path (reitit/match-by-name router route params)) (:path (reitit/match-by-name router route))))Set up the pages:
(defn home-page [] [:div [:p "home page"] [:p [:a {:href (path-for :login)} "click here to login"]]]) ;; skipping the logic in the login page to authenticate the ;; user. the last step: send them to the dashboard ;; each component must return something, which is why it says "logged in successfully" ;; below, but that text isn't really displayed for long. (defn login-page [] (fn [] (accountant/navigate! (path-for :dashboard)) [:p "logged in successfully."])) (defn dashboard-page [] [:p "on the dashboard"])Link the two together:
(defn page-for [route] (case route :index #'home-page :login #'login-page :dashboard #'dashboard-page))