[front/feat] process router match...again
This commit is contained in:
parent
ce0de0d4e5
commit
d34b377bce
5 changed files with 44 additions and 21 deletions
|
@ -17,7 +17,7 @@
|
||||||
[org.slf4j/slf4j-nop "2.0.7"]
|
[org.slf4j/slf4j-nop "2.0.7"]
|
||||||
[reagent/reagent "1.2.0"]
|
[reagent/reagent "1.2.0"]
|
||||||
[metosin/reitit "0.7.0-alpha5"]
|
[metosin/reitit "0.7.0-alpha5"]
|
||||||
|
[superstructor/re-frame-fetch-fx "0.2.0"]
|
||||||
[binaryage/devtools "1.0.7"]
|
[binaryage/devtools "1.0.7"]
|
||||||
[re-frame/re-frame "1.3.0"]
|
[re-frame/re-frame "1.3.0"]
|
||||||
[day8.re-frame/tracing "0.6.2"]
|
[day8.re-frame/tracing "0.6.2"]
|
||||||
|
|
|
@ -8,10 +8,6 @@
|
||||||
[frontend.router]))
|
[frontend.router]))
|
||||||
|
|
||||||
|
|
||||||
(defn on-navigate [new-match]
|
|
||||||
(when new-match
|
|
||||||
(dispatch [:navigated new-match])))
|
|
||||||
|
|
||||||
(defonce root (rdom/create-root (js/document.getElementById "app")))
|
(defonce root (rdom/create-root (js/document.getElementById "app")))
|
||||||
|
|
||||||
(defn ^:dev/after-load start []
|
(defn ^:dev/after-load start []
|
||||||
|
@ -22,7 +18,7 @@
|
||||||
(dispatch-sync [:initialize-db])
|
(dispatch-sync [:initialize-db])
|
||||||
(rfe/start!
|
(rfe/start!
|
||||||
frontend.router/router
|
frontend.router/router
|
||||||
on-navigate
|
frontend.router/on-navigate
|
||||||
{:use-fragment true})
|
{:use-fragment true})
|
||||||
(js/console.log "init")
|
(js/console.log "init")
|
||||||
(start))
|
(start))
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
(ns frontend.events
|
(ns frontend.events
|
||||||
(:require [re-frame.core :refer [reg-event-db reg-event-fx dispatch]]
|
(:require [re-frame.core :refer [reg-event-db reg-event-fx]]
|
||||||
|
[superstructor.re-frame.fetch-fx]
|
||||||
[clojure.string :as str]))
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,23 +13,45 @@
|
||||||
|
|
||||||
(reg-event-fx
|
(reg-event-fx
|
||||||
:set-active-page
|
:set-active-page
|
||||||
(fn [{:keys [db]} [_ {:keys [page id]}]]
|
(fn [{:keys [db]} [_ {:keys [new-match id]}]]
|
||||||
(let [set-page (assoc db :active-page page)]
|
(js/console.log new-match)
|
||||||
|
(let [page (get-in new-match [:data :name])
|
||||||
|
set-page (assoc db :current-route new-match)]
|
||||||
|
(js/console.log page)
|
||||||
(case page
|
(case page
|
||||||
;; -- URL @ "/" --------------------------------------------------------
|
;; -- URL @ "/" --------------------------------------------------------
|
||||||
"/" {:db set-page}))))
|
:frontpage {:db set-page
|
||||||
|
:dispatch [:get-page]}
|
||||||
|
;; -- URL @ "/about" --------------------------------------------------------
|
||||||
|
:about {:db set-page}
|
||||||
|
|
||||||
|
;; -- URL @ "/item" --------------------------------------------------------
|
||||||
|
:item {:db set-page}
|
||||||
|
))))
|
||||||
|
|
||||||
|
|
||||||
(reg-event-db :initialize-db
|
(reg-event-fx
|
||||||
(fn [_ _]
|
:get-page
|
||||||
{:set-active-page nil}))
|
(fn [{:keys [db]} _]
|
||||||
|
{:db (assoc-in db [:loading :tags] true)
|
||||||
|
:fetch {:method :get
|
||||||
|
:url "https://api.github.com/orgs/day8"
|
||||||
|
:mode :cors
|
||||||
|
:referrer :no-referrer
|
||||||
|
:credentials :omit
|
||||||
|
:timeout 5000
|
||||||
|
:response-content-types {#"application/.*json" :json}
|
||||||
|
:on-success [:get-page-success]
|
||||||
|
:on-failure [:initialize-db]
|
||||||
|
}}))
|
||||||
|
|
||||||
(reg-event-db
|
(reg-event-db
|
||||||
:navigated
|
:get-page-success
|
||||||
(fn [db [_ new-match]]
|
(fn [db [_ {repos_url :body}]]
|
||||||
(let [old-match (:current-route db)]
|
(-> db
|
||||||
(assoc db :current-route new-match))))
|
(assoc :repos_url (:id repos_url)) )))
|
||||||
|
|
||||||
(def history
|
(reg-event-db
|
||||||
#(dispatch [:set-active-page {:page (:path %)
|
:initialize-db
|
||||||
:id (get-in % [:path-params :id])}]))
|
(fn [_ _]
|
||||||
|
{:current-route nil}))
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
(ns frontend.router
|
(ns frontend.router
|
||||||
(:require [reitit.frontend :as rf]
|
(:require [reitit.frontend :as rf]
|
||||||
|
[re-frame.core :refer [dispatch]]
|
||||||
[spec-tools.data-spec :as ds]
|
[spec-tools.data-spec :as ds]
|
||||||
[frontend.views]))
|
[frontend.views]))
|
||||||
|
|
||||||
|
@ -24,3 +25,7 @@
|
||||||
(def router
|
(def router
|
||||||
(rf/router
|
(rf/router
|
||||||
routes))
|
routes))
|
||||||
|
|
||||||
|
(def on-navigate
|
||||||
|
#(dispatch [:set-active-page {:new-match %
|
||||||
|
:id (get-in % [:path-params :id])}]))
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
[:h2 "Selected item " id]]))
|
[:h2 "Selected item " id]]))
|
||||||
|
|
||||||
(defn nav [{:keys [current-route]}]
|
(defn nav [{:keys [current-route]}]
|
||||||
(js/console.log current-route)
|
|
||||||
(let [active #(when (= % (-> current-route :data :name)) "> ")]
|
(let [active #(when (= % (-> current-route :data :name)) "> ")]
|
||||||
[:ul {:class "nav flex flex-col overflow-hidden"}
|
[:ul {:class "nav flex flex-col overflow-hidden"}
|
||||||
[:li
|
[:li
|
||||||
|
|
Loading…
Reference in a new issue