Fixes based on review

This commit is contained in:
Valtteri Harmainen 2019-04-26 15:52:44 +03:00
parent 22f38d9992
commit 0919b46236
2 changed files with 14 additions and 12 deletions

View file

@ -31,8 +31,12 @@
(defn Redirect (defn Redirect
"Component that only causes a redirect side-effect." "Component that only causes a redirect side-effect."
[props] [props]
(redirect! props) (r/create-class
nil) {:component-did-mount (fn [this] (redirect! (r/props this)))
:component-did-update (fn [this [_ prev-props]]
(if (not= (r/props this) prev-props)
(redirect! (r/props this))))
:render (fn [this] nil)}))
(defn item-page [match] (defn item-page [match]
(let [{:keys [path query]} (:parameters match) (let [{:keys [path query]} (:parameters match)

View file

@ -25,7 +25,9 @@
(re-frame/reg-event-db (re-frame/reg-event-db
::navigated ::navigated
(fn [db [_ new-match]] (fn [db [_ new-match]]
(assoc db :current-route new-match))) (let [old-match (:current-route db)
controllers (rfc/apply-controllers (:controllers old-match) new-match)]
(assoc db :current-route (assoc new-match :controllers controllers)))))
;;; Subscriptions ;;; ;;; Subscriptions ;;;
@ -99,11 +101,8 @@
:stop (fn [& params] (js/console.log "Leaving sub-page 2"))}]}]]) :stop (fn [& params] (js/console.log "Leaving sub-page 2"))}]}]])
(defn on-navigate [new-match] (defn on-navigate [new-match]
(let [old-match (re-frame/subscribe [::current-route])] (when new-match
(when new-match (re-frame/dispatch [::navigated new-match])))
(let [cs (rfc/apply-controllers (:controllers @old-match) new-match)
m (assoc new-match :controllers cs)]
(re-frame/dispatch [::navigated m])))))
(def router (def router
(rf/router (rf/router
@ -118,16 +117,15 @@
{:use-fragment true})) {:use-fragment true}))
(defn nav [{:keys [router current-route]}] (defn nav [{:keys [router current-route]}]
(into [:ul
[:ul]
(for [route-name (r/route-names router) (for [route-name (r/route-names router)
:let [route (r/match-by-name router route-name) :let [route (r/match-by-name router route-name)
text (-> route :data :link-text)]] text (-> route :data :link-text)]]
[:li [:li {:key route-name}
(when (= route-name (-> current-route :data :name)) (when (= route-name (-> current-route :data :name))
"> ") "> ")
;; Create a normal links that user can click ;; Create a normal links that user can click
[:a {:href (href route-name)} text]]))) [:a {:href (href route-name)} text]])])
(defn router-component [{:keys [router]}] (defn router-component [{:keys [router]}]
(let [current-route @(re-frame/subscribe [::current-route])] (let [current-route @(re-frame/subscribe [::current-route])]