aboutsummaryrefslogtreecommitdiff
path: root/src/frontend
diff options
context:
space:
mode:
authorMiguel Ángel Moreno <mail@migalmoreno.com>2024-12-19 02:21:55 +0100
committerMiguel Ángel Moreno <mail@migalmoreno.com>2024-12-19 02:21:55 +0100
commit62d5d995345fa4ae8814f76cfa4f3eb930a7d9a1 (patch)
tree354779ed86601126f6342e271cf2142643905d4a /src/frontend
parent13e285955508e601d6a648c25e29632340b4d9b6 (diff)
refactor: make routing shared between frontend and backend
Diffstat (limited to 'src/frontend')
-rw-r--r--src/frontend/tubo/core.cljs4
-rw-r--r--src/frontend/tubo/router.cljs93
-rw-r--r--src/frontend/tubo/routes.cljs76
3 files changed, 95 insertions, 78 deletions
diff --git a/src/frontend/tubo/core.cljs b/src/frontend/tubo/core.cljs
index c9f7e7c..4f8762f 100644
--- a/src/frontend/tubo/core.cljs
+++ b/src/frontend/tubo/core.cljs
@@ -4,7 +4,7 @@
[reagent.core :as r]
[re-frame.core :as rf]
[tubo.events]
- [tubo.routes :as routes]
+ [tubo.router :as router]
[tubo.subs]
[tubo.views :as views]))
@@ -13,7 +13,7 @@
(defn ^:dev/after-load mount-root
[]
(rf/clear-subscription-cache!)
- (routes/start-routes!)
+ (router/start-router!)
(.render root (r/as-element [(fn [] views/app)])))
(defn ^:export init [] (rf/dispatch-sync [:initialize-db]) (mount-root))
diff --git a/src/frontend/tubo/router.cljs b/src/frontend/tubo/router.cljs
new file mode 100644
index 0000000..3e51171
--- /dev/null
+++ b/src/frontend/tubo/router.cljs
@@ -0,0 +1,93 @@
+(ns tubo.router
+ (:require
+ [reitit.core :as r]
+ [reitit.frontend :as ref]
+ [reitit.frontend.easy :as rfe]
+ [re-frame.core :as rf]
+ [tubo.bookmarks.views :as bookmarks]
+ [tubo.channel.views :as channel]
+ [tubo.kiosks.views :as kiosk]
+ [tubo.playlist.views :as playlist]
+ [tubo.routes :as routes]
+ [tubo.search.views :as search]
+ [tubo.settings.views :as settings]
+ [tubo.stream.views :as stream]))
+
+(defn expand-routes
+ [data opts]
+ (if (keyword? data)
+ (case data
+ :web/homepage {:view kiosk/kiosk
+ :name :homepage
+ :controllers [{:start #(rf/dispatch [:fetch-homepage])}]}
+ :web/search {:view search/search
+ :name :search-page
+ :controllers [{:parameters {:query [:q :serviceId]}
+ :start (fn [{{:keys [serviceId q]}
+ :query}]
+ (rf/dispatch
+ [:search/fetch-page
+ serviceId
+ q]))
+ :stop #(rf/dispatch
+ [:search/show-form false])}]}
+ :web/stream {:view stream/stream
+ :name :stream-page
+ :controllers [{:parameters {:query [:url]}
+ :start (fn [{{:keys [url]} :query}]
+ (rf/dispatch
+ [:stream/fetch-page
+ url]))}]}
+ :web/channel {:view channel/channel
+ :name :channel-page
+ :controllers [{:parameters {:query [:url]}
+ :start (fn [{{:keys [url]} :query}]
+ (rf/dispatch
+ [:channel/fetch-page
+ url]))}]}
+ :web/playlist {:view playlist/playlist
+ :name :playlist-page
+ :controllers [{:parameters {:query [:url]}
+ :start (fn [{{:keys [url]} :query}]
+ (rf/dispatch
+ [:playlist/fetch-page
+ url]))}]}
+ :web/kiosk {:view kiosk/kiosk
+ :name :kiosk-page
+ :controllers [{:parameters {:query [:kioskId :serviceId]}
+ :start (fn [{{:keys [serviceId
+ kioskId]}
+ :query}]
+ (rf/dispatch
+ [:kiosks/fetch-page
+ serviceId
+ kioskId]))}]}
+ :web/settings {:view settings/settings
+ :name :settings-page
+ :controllers [{:start #(rf/dispatch
+ [:settings/fetch-page])}]}
+ :web/bookmark {:view bookmarks/bookmark
+ :name :bookmark-page
+ :controllers [{:parameters {:query [:id]}
+ :start (fn [{{:keys [id]} :query}]
+ (rf/dispatch
+ [:bookmark/fetch-page
+ id]))}]}
+ :web/bookmarks {:view bookmarks/bookmarks
+ :name :bookmarks-page
+ :controllers [{:start #(rf/dispatch
+ [:bookmarks/fetch-page])}]}
+ nil)
+ (r/expand data opts)))
+
+(def router
+ (ref/router routes/routes {:expand expand-routes}))
+
+(defn on-navigate
+ [new-match]
+ (when new-match
+ (rf/dispatch [:navigation/navigated new-match])))
+
+(defn start-router!
+ []
+ (rfe/start! router on-navigate {:use-fragment false}))
diff --git a/src/frontend/tubo/routes.cljs b/src/frontend/tubo/routes.cljs
deleted file mode 100644
index 8e72d09..0000000
--- a/src/frontend/tubo/routes.cljs
+++ /dev/null
@@ -1,76 +0,0 @@
-(ns tubo.routes
- (:require
- [reitit.frontend :as ref]
- [reitit.frontend.easy :as rfe]
- [re-frame.core :as rf]
- [tubo.channel.views :as channel]
- [tubo.kiosks.views :as kiosk]
- [tubo.playlist.views :as playlist]
- [tubo.bookmarks.views :as bookmarks]
- [tubo.search.views :as search]
- [tubo.settings.views :as settings]
- [tubo.stream.views :as stream]))
-
-(def router
- (ref/router
- [["/"
- {:view kiosk/kiosk
- :name :homepage
- :controllers [{:start #(rf/dispatch [:fetch-homepage])}]}]
- ["/search"
- {:view search/search
- :name :search-page
- :controllers [{:parameters {:query [:q :serviceId]}
- :start (fn [{{:keys [serviceId q]} :query}]
- (rf/dispatch [:search/fetch-page serviceId
- q]))
- :stop #(rf/dispatch [:search/show-form false])}]}]
- ["/stream"
- {:view stream/stream
- :name :stream-page
- :controllers [{:parameters {:query [:url]}
- :start (fn [{{:keys [url]} :query}]
- (rf/dispatch [:stream/fetch-page url]))}]}]
- ["/channel"
- {:view channel/channel
- :name :channel-page
- :controllers [{:parameters {:query [:url]}
- :start (fn [{{:keys [url]} :query}]
- (rf/dispatch [:channel/fetch-page url]))
- :stop #(rf/dispatch [:channel/reset])}]}]
- ["/playlist"
- {:view playlist/playlist
- :name :playlist-page
- :controllers [{:parameters {:query [:url]}
- :start (fn [{{:keys [url]} :query}]
- (rf/dispatch [:playlist/fetch-page url]))}]}]
- ["/kiosk"
- {:view kiosk/kiosk
- :name :kiosk-page
- :controllers [{:parameters {:query [:kioskId :serviceId]}
- :start (fn [{{:keys [serviceId kioskId]} :query}]
- (rf/dispatch [:kiosks/fetch-page serviceId
- kioskId]))}]}]
- ["/settings"
- {:view settings/settings
- :name :settings-page
- :controllers [{:start #(rf/dispatch [:settings/fetch-page])}]}]
- ["/bookmark"
- {:view bookmarks/bookmark
- :name :bookmark-page
- :controllers [{:parameters {:query [:id]}
- :start (fn [{{:keys [id]} :query}]
- (rf/dispatch [:bookmark/fetch-page id]))}]}]
- ["/bookmarks"
- {:view bookmarks/bookmarks
- :name :bookmarks-page
- :controllers [{:start #(rf/dispatch [:bookmarks/fetch-page])}]}]]))
-
-(defn on-navigate
- [new-match]
- (when new-match
- (rf/dispatch [:navigation/navigated new-match])))
-
-(defn start-routes!
- []
- (rfe/start! router on-navigate {:use-fragment false}))