From ab75772b5ac11f03344aa97001d9e03521e0d9d0 Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Mon, 16 Oct 2023 00:28:02 +0200 Subject: feat: simplify events for setting the service styles --- src/backend/tubo/api/services.clj | 3 ++ src/frontend/tubo/events.cljs | 78 ++++++++++++++++++++++++++------------- src/frontend/tubo/routes.cljs | 18 +++------ 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/backend/tubo/api/services.clj b/src/backend/tubo/api/services.clj index 0bc44af..ab1a655 100644 --- a/src/backend/tubo/api/services.clj +++ b/src/backend/tubo/api/services.clj @@ -21,6 +21,7 @@ info (SearchInfo/getInfo service query-handler)] {:items (items/get-items (.getRelatedItems info)) :next-page (j/from-java (.getNextPage info)) + :service-id service-id :search-suggestion (.getSearchSuggestion info) :corrected-search? (.isCorrectedSearch info)})) ([service-id query content-filters sort-filter page-url] @@ -41,6 +42,7 @@ info (KioskInfo/getInfo extractor)] {:id (.getId info) :url (.getUrl info) + :service-id service-id :next-page (j/from-java (.getNextPage info)) :related-streams (items/get-items (.getRelatedItems info))})) ([kiosk-id service-id] @@ -50,6 +52,7 @@ info (KioskInfo/getInfo extractor)] {:id (.getId info) :url (.getUrl info) + :service-id service-id :next-page (j/from-java (.getNextPage info)) :related-streams (items/get-items (.getRelatedItems info))})) ([kiosk-id service-id page-url] diff --git a/src/frontend/tubo/events.cljs b/src/frontend/tubo/events.cljs index 11e2498..87778b8 100644 --- a/src/frontend/tubo/events.cljs +++ b/src/frontend/tubo/events.cljs @@ -187,6 +187,13 @@ (fn [db [_ res]] (assoc db :services (js->clj res :keywordize-keys true)))) +(rf/reg-event-fx + ::set-service-styles + (fn [{:keys [db]} [_ res]] + {:db db + :fx [[:dispatch [::change-service-id (:service-id res)]] + [:dispatch [::get-kiosks (:service-id res)]]]})) + (rf/reg-event-fx ::get-services (fn [{:keys [db]} _] @@ -256,14 +263,16 @@ (api/get-request (str "/api/services/" id "/kiosks") [::load-kiosks] [::bad-response]))) -(rf/reg-event-db +(rf/reg-event-fx ::load-kiosk - (fn [db [_ res]] - (assoc db :kiosk (js->clj res :keywordize-keys true) - :show-page-loading false))) + (fn [{:keys [db]} [_ res]] + (let [kiosk-res (js->clj res :keywordize-keys true)] + {:db (assoc db :kiosk kiosk-res + :show-page-loading false) + :fx [[:dispatch [::set-service-styles kiosk-res]]]}))) (rf/reg-event-fx - ::get-default-kiosk + ::get-default-kiosk-page (fn [{:keys [db]} [_ service-id]] (assoc (api/get-request (str "/api/services/" service-id "/default-kiosk") @@ -271,7 +280,7 @@ :db (assoc db :show-page-loading true)))) (rf/reg-event-fx - ::get-kiosk + ::get-kiosk-page (fn [{:keys [db]} [_ service-id kiosk-id]] (if kiosk-id (assoc @@ -279,7 +288,7 @@ (js/encodeURIComponent kiosk-id)) [::load-kiosk] [::bad-response]) :db (assoc db :show-page-loading true)) - {:fx [[:dispatch [::get-default-kiosk service-id]]]}))) + {:fx [[:dispatch [::get-default-kiosk-page service-id]]]}))) (rf/reg-event-fx ::change-service @@ -313,23 +322,42 @@ :db (assoc db :show-pagination-loading true))))) (rf/reg-event-fx - ::load-stream + ::load-global-player-stream + (fn [{:keys [db]} [_ res]] + (let [stream-res (js->clj res :keywordize-keys true)] + {:db (assoc db :show-global-player-loading false) + :fx [[:dispatch [::change-media-queue-stream + (-> stream-res :audio-streams first :content)]]]}))) + +(rf/reg-event-fx + ::load-stream-page (fn [{:keys [db]} [_ res]] (let [stream-res (js->clj res :keywordize-keys true)] {:db (assoc db :stream stream-res :show-page-loading false) :fx [[:dispatch [::change-stream-format nil]] [:dispatch [::get-comments (:url stream-res)]] - [:dispatch [::change-service-id (:service-id stream-res)]] - [:dispatch [::get-kiosks (:service-id stream-res)]]]}))) + [:dispatch [::set-service-styles stream-res]]]}))) (rf/reg-event-fx - ::get-stream + ::fetch-stream-page + (fn [{:keys [db]} [_ uri]] + (api/get-request (str "/api/streams/" (js/encodeURIComponent uri)) + [::load-stream-page] [::bad-response]))) + +(rf/reg-event-fx + ::fetch-global-player-stream (fn [{:keys [db]} [_ uri]] (assoc (api/get-request (str "/api/streams/" (js/encodeURIComponent uri)) - [::load-stream] [::bad-response]) - :db (assoc db :show-page-loading true)))) + [::load-global-player-stream] [::bad-response]) + :db (assoc db :show-global-player-loading true)))) + +(rf/reg-event-fx + ::get-stream-page + (fn [{:keys [db]} [_ uri]] + {:db (assoc db :show-page-loading true) + :fx [[:dispatch [::fetch-stream-page uri]]]})) (rf/reg-event-db ::change-stream-format @@ -349,11 +377,10 @@ (let [channel-res (js->clj res :keywordize-keys true)] {:db (assoc db :channel channel-res :show-page-loading false) - :fx [[:dispatch [::change-service-id (:service-id channel-res)]] - [:dispatch [::get-kiosks (:service-id channel-res)]]]}))) + :fx [[:dispatch [::set-service-styles channel-res]]]}))) (rf/reg-event-fx - ::get-channel + ::get-channel-page (fn [{:keys [db]} [_ uri]] (assoc (api/get-request @@ -367,26 +394,27 @@ (let [playlist-res (js->clj res :keywordize-keys true)] {:db (assoc db :playlist playlist-res :show-page-loading false) - :fx [[:dispatch [::change-service-id (:service-id playlist-res)]] - [:dispatch [::get-kiosks (:service-id playlist-res)]]]}))) + :fx [[:dispatch [::set-service-styles playlist-res]]]}))) (rf/reg-event-fx - ::get-playlist + ::get-playlist-page (fn [{:keys [db]} [_ uri]] (assoc (api/get-request (str "/api/playlists/" (js/encodeURIComponent uri)) [::load-playlist] [::bad-response]) :db (assoc db :show-page-loading true)))) -(rf/reg-event-db +(rf/reg-event-fx ::load-search-results - (fn [db [_ res]] - (assoc db :search-results (js->clj res :keywordize-keys true) - :show-page-loading false - :global-search ""))) + (fn [{:keys [db]} [_ res]] + (let [search-res (js->clj res :keywordize-keys true)] + {:db (assoc db :search-results search-res + :show-page-loading false + :global-search "") + :fx [[:dispatch [::set-service-styles search-res]]]}))) (rf/reg-event-fx - ::get-search-results + ::get-search-page (fn [{:keys [db]} [_ service-id query]] (assoc (api/get-request (str "/api/services/" service-id "/search") diff --git a/src/frontend/tubo/routes.cljs b/src/frontend/tubo/routes.cljs index 67ccc8c..725c999 100644 --- a/src/frontend/tubo/routes.cljs +++ b/src/frontend/tubo/routes.cljs @@ -15,38 +15,32 @@ [["/" {:view kiosk/kiosk :name ::home :controllers [{:start (fn [_] - (rf/dispatch [::events/change-service-id 0]) - (rf/dispatch [::events/get-default-kiosk 0]) - (rf/dispatch [::events/get-kiosks 0]))}]}] + (rf/dispatch [::events/get-default-kiosk-page 0]))}]}] ["/search" {:view search/search :name ::search :controllers [{:parameters {:query [:q :serviceId]} :start (fn [{{:keys [serviceId q]} :query}] - (rf/dispatch [::events/get-search-results serviceId q]) - (rf/dispatch [::events/change-service-id (js/parseInt serviceId)]) - (rf/dispatch [::events/get-kiosks (js/parseInt serviceId)]))}]}] + (rf/dispatch [::events/get-search-page serviceId q]))}]}] ["/stream" {:view stream/stream :name ::stream :controllers [{:parameters {:query [:url]} :start (fn [{{:keys [url]} :query}] - (rf/dispatch [::events/get-stream url]))}]}] + (rf/dispatch [::events/get-stream-page url]))}]}] ["/channel" {:view channel/channel :name ::channel :controllers [{:parameters {:query [:url]} :start (fn [{{:keys [url]} :query}] - (rf/dispatch [::events/get-channel url]))}]}] + (rf/dispatch [::events/get-channel-page url]))}]}] ["/playlist" {:view playlist/playlist :name ::playlist :controllers [{:parameters {:query [:url]} :start (fn [{{:keys [url]} :query}] - (rf/dispatch [::events/get-playlist url]))}]}] + (rf/dispatch [::events/get-playlist-page url]))}]}] ["/kiosk" {:view kiosk/kiosk :name ::kiosk :controllers [{:parameters {:query [:kioskId :serviceId]} :start (fn [{{:keys [serviceId kioskId]} :query}] - (rf/dispatch [::events/change-service-id (js/parseInt serviceId)]) - (rf/dispatch [::events/get-kiosk serviceId kioskId]) - (rf/dispatch [::events/get-kiosks serviceId]))}]}]])) + (rf/dispatch [::events/get-kiosk-page serviceId kioskId]))}]}]])) (defn on-navigate [new-match] -- cgit v1.2.3