[front/feat] process router match...again

This commit is contained in:
SouthFox 2023-08-25 11:11:48 +08:00
parent ce0de0d4e5
commit d34b377bce
5 changed files with 44 additions and 21 deletions

View file

@ -17,7 +17,7 @@
[org.slf4j/slf4j-nop "2.0.7"]
[reagent/reagent "1.2.0"]
[metosin/reitit "0.7.0-alpha5"]
[superstructor/re-frame-fetch-fx "0.2.0"]
[binaryage/devtools "1.0.7"]
[re-frame/re-frame "1.3.0"]
[day8.re-frame/tracing "0.6.2"]

View file

@ -8,10 +8,6 @@
[frontend.router]))
(defn on-navigate [new-match]
(when new-match
(dispatch [:navigated new-match])))
(defonce root (rdom/create-root (js/document.getElementById "app")))
(defn ^:dev/after-load start []
@ -22,7 +18,7 @@
(dispatch-sync [:initialize-db])
(rfe/start!
frontend.router/router
on-navigate
frontend.router/on-navigate
{:use-fragment true})
(js/console.log "init")
(start))

View file

@ -1,5 +1,6 @@
(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]))
@ -12,23 +13,45 @@
(reg-event-fx
:set-active-page
(fn [{:keys [db]} [_ {:keys [page id]}]]
(let [set-page (assoc db :active-page page)]
(fn [{:keys [db]} [_ {:keys [new-match id]}]]
(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
;; -- 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
(fn [_ _]
{:set-active-page nil}))
(reg-event-fx
:get-page
(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
:navigated
(fn [db [_ new-match]]
(let [old-match (:current-route db)]
(assoc db :current-route new-match))))
:get-page-success
(fn [db [_ {repos_url :body}]]
(-> db
(assoc :repos_url (:id repos_url)) )))
(def history
#(dispatch [:set-active-page {:page (:path %)
:id (get-in % [:path-params :id])}]))
(reg-event-db
:initialize-db
(fn [_ _]
{:current-route nil}))

View file

@ -1,5 +1,6 @@
(ns frontend.router
(:require [reitit.frontend :as rf]
[re-frame.core :refer [dispatch]]
[spec-tools.data-spec :as ds]
[frontend.views]))
@ -24,3 +25,7 @@
(def router
(rf/router
routes))
(def on-navigate
#(dispatch [:set-active-page {:new-match %
:id (get-in % [:path-params :id])}]))

View file

@ -24,7 +24,6 @@
[:h2 "Selected item " id]]))
(defn nav [{:keys [current-route]}]
(js/console.log current-route)
(let [active #(when (= % (-> current-route :data :name)) "> ")]
[:ul {:class "nav flex flex-col overflow-hidden"}
[:li