From d34b377bcefede27b3782690fd071c99360fec90 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 25 Aug 2023 11:11:48 +0800 Subject: [PATCH] [front/feat] process router match...again --- project.clj | 2 +- src/main/frontend/app.cljs | 6 +---- src/main/frontend/events.cljs | 51 +++++++++++++++++++++++++---------- src/main/frontend/router.cljs | 5 ++++ src/main/frontend/views.cljs | 1 - 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/project.clj b/project.clj index 5a01872..b171277 100644 --- a/project.clj +++ b/project.clj @@ -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"] diff --git a/src/main/frontend/app.cljs b/src/main/frontend/app.cljs index b7ae5fa..1edf958 100644 --- a/src/main/frontend/app.cljs +++ b/src/main/frontend/app.cljs @@ -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)) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index 4a348de..8e0444a 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -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})) diff --git a/src/main/frontend/router.cljs b/src/main/frontend/router.cljs index 0845464..d97e4e8 100644 --- a/src/main/frontend/router.cljs +++ b/src/main/frontend/router.cljs @@ -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])}])) diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index f4a88fb..a0eea60 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -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