From b3a7566cc6538e1ae53c7d8b9d49b81a15c966cc Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Sun, 8 Dec 2024 21:55:55 +0100 Subject: fix: fix loading of tab related streams --- src/frontend/tubo/channel/events.cljs | 80 ++++++++++++++++++++++++++++------- src/frontend/tubo/channel/views.cljs | 29 ++++++++----- 2 files changed, 83 insertions(+), 26 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/tubo/channel/events.cljs b/src/frontend/tubo/channel/events.cljs index 49ed26a..bf00a69 100644 --- a/src/frontend/tubo/channel/events.cljs +++ b/src/frontend/tubo/channel/events.cljs @@ -41,40 +41,88 @@ (rf/reg-event-fx :channel/load-tab - (fn [{:keys [db]} [_ res]] - (let [tab-res (js->clj res :keywordize-keys true)] + (fn [{:keys [db]} [_ tab-id res]] + (let [tab-res (js->clj res :keywordize-keys true) + selected-tab (first (filter #(= tab-id + (-> % + :contentFilters + first)) + (-> db + :channel + :tabs))) + tab-idx (.indexOf (-> db + :channel + :tabs) + selected-tab)] {:db (-> db - (assoc-in [:channel :related-streams] (:related-streams tab-res)) - (assoc-in [:channel :next-page-url] (:next-page-url tab-res)))}))) + (assoc-in [:channel :tabs tab-idx :related-streams] + (:related-streams tab-res)) + (assoc-in [:channel :tabs tab-idx :next-page] + (:next-page tab-res)))}))) (rf/reg-event-fx :channel/fetch-tab (fn [_ [_ uri tab-id]] (api/get-request (str "/channels/" (js/encodeURIComponent uri) "/tabs/" tab-id) - [:channel/load-tab] + [:channel/load-tab tab-id] [:channel/bad-page-response]))) (rf/reg-event-db :channel/load-paginated - (fn [db [_ res]] - (let [channel-res (js->clj res :keywordize-keys true)] - (if (empty? (:related-streams channel-res)) + (fn [db [_ tab-id res]] + (let [channel-res (js->clj res :keywordize-keys true) + selected-tab (first (filter #(= tab-id + (-> % + :contentFilters + first)) + (-> db + :channel + :tabs))) + tab-idx (.indexOf (-> db + :channel + :tabs) + selected-tab)] + (if (empty? (:related-streams (if tab-id selected-tab channel-res))) (-> db - (assoc-in [:channel :next-page] nil) + (assoc-in (if tab-id + [:channel :tabs tab-idx :next-page] + [:channel :next-page]) + nil) (assoc :show-pagination-loading false)) (-> db - (update-in [:channel :related-streams] + (update-in (if tab-id + [:channel :tabs tab-idx :related-streams] + [:channel :related-streams]) #(apply conj %1 %2) (:related-streams channel-res)) - (assoc-in [:channel :next-page] (:next-page channel-res)) + (assoc-in (if tab-id + [:channel :tabs tab-idx :next-page] + [:channel :next-page]) + (:next-page channel-res)) (assoc :show-pagination-loading false)))))) (rf/reg-event-fx :channel/bad-paginated-response - (fn [{:keys [db]} [_ res]] - {:fx [[:dispatch [:bad-response res]]] - :db (assoc db :show-pagination-loading false)})) + (fn [{:keys [db]} [_ tab-id res]] + (let [selected-tab (first (filter #(= tab-id + (-> % + :contentFilters + first)) + (-> db + :channel + :tabs))) + tab-idx (.indexOf (-> db + :channel + :tabs) + selected-tab)] + {:fx [[:dispatch [:bad-response res]]] + :db (-> db + (assoc-in (if tab-id + [:channel :tabs tab-idx :next-page] + [:channel :next-page]) + nil) + (assoc :show-pagination-loading false))}))) (rf/reg-event-fx :channel/fetch-paginated @@ -85,8 +133,8 @@ (api/get-request (str "/channels/" (js/encodeURIComponent uri) "/tabs/" (or tab-id "default")) - [:channel/load-paginated] - [:channel/bad-paginated-response] + [:channel/load-paginated tab-id] + [:channel/bad-paginated-response tab-id] {:nextPage (js/encodeURIComponent next-page-url)}) :db (assoc db :show-pagination-loading true))))) diff --git a/src/frontend/tubo/channel/views.cljs b/src/frontend/tubo/channel/views.cljs index a4b7b1e..e665543 100644 --- a/src/frontend/tubo/channel/views.cljs +++ b/src/frontend/tubo/channel/views.cljs @@ -43,16 +43,21 @@ [_] (let [!show-description? (r/atom false) !layout (r/atom (:items-layout @(rf/subscribe [:settings]))) - !active-tab (r/atom nil)] + !active-tab-id (r/atom nil)] (fn [{{:keys [url]} :query-params}] (let [{:keys [banners description next-page related-streams] :as channel} @(rf/subscribe [:channel]) - next-page-url (:url next-page) - scrolled-to-bottom? @(rf/subscribe [:scrolled-to-bottom]) - page-loading? @(rf/subscribe [:show-page-loading])] - (when (and next-page-url scrolled-to-bottom?) - (rf/dispatch [:channel/fetch-paginated url @!active-tab - next-page-url])) + page-loading? @(rf/subscribe [:show-page-loading]) + active-tab (first (filter #(= @!active-tab-id + (-> % + :contentFilters + first)) + (:tabs channel))) + next-page-url (if active-tab + (-> active-tab + :next-page + :url) + (:url next-page))] [:<> (when-not page-loading? (when banners @@ -76,9 +81,13 @@ :contentFilters first)}) (:tabs channel)) - :selected-id @!active-tab + :selected-id @!active-tab-id :on-change - #(do (reset! !active-tab %) + #(do (reset! !active-tab-id %) (rf/dispatch [:channel/fetch-tab url %]))] [items/layout-switcher !layout]] - [items/related-streams related-streams next-page-url !layout]]])))) + [items/related-streams + (or (:related-streams active-tab) related-streams) next-page-url + !layout + #(rf/dispatch [:channel/fetch-paginated url @!active-tab-id + next-page-url])]]])))) -- cgit v1.2.3