From 44fbfc7eadd2fe52f889af63b36001b22d94017d Mon Sep 17 00:00:00 2001 From: SouthFox Date: Tue, 19 Sep 2023 22:49:55 +0800 Subject: [PATCH] [front/refactor] show more logic --- src/main/frontend/events.cljs | 69 ++++++++++++++++++++--------------- src/main/frontend/views.cljs | 21 +++++++---- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index e4d356b..8f12a89 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -69,42 +69,25 @@ (reg-event-fx :get-question (fn [{:keys [db]} [_ params]] - (let [loading (if (empty? (:query params)) - :question - :comment)] - {:fetch {:method :get - :url (endpoint "hq" (:id params)) - :params (:query params) - :mode :cors - :referrer :no-referrer - :credentials :omit - :timeout 10000 - :response-content-types {#"application/.*json" :json} - :on-success [:get-question-success] - :on-failure [:get-question-failure]} - - :db (-> db - (assoc-in [:loading loading] true))}))) - -(defn merge-question - [db body] - (if (empty? (:post db)) - (-> db - (assoc-in [:post :question] (:question body)) - (assoc-in [:post :answers] (:answers body)) - (assoc-in [:post :paging] (:paging body))) - (-> db - (assoc-in [:post :question] (:question body)) - (update-in [:post :answers] into (:answers body)) - (assoc-in [:post :paging] (:paging body))))) + {:fetch {:method :get + :url (endpoint "hq" (:id params)) + :params (:query params) + :mode :cors + :referrer :no-referrer + :credentials :omit + :timeout 10000 + :response-content-types {#"application/.*json" :json} + :on-success [:get-question-success] + :on-failure [:get-question-failure]} + :db (-> db + (assoc-in [:loading :question] true))})) (reg-event-db :get-question-success (fn [db [_ {body :body}]] (-> db (assoc-in [:loading :question] false) - (assoc-in [:loading :comment] false) - (merge-question body)))) + (assoc-in [:post] body)))) (reg-event-db :get-question-failure @@ -113,6 +96,32 @@ (assoc-in [:loading :question] false) (assoc :post body)))) +(reg-event-fx + :get-more + (fn [{:keys [db]} [_ {:keys [request db-path]}]] + {:fetch {:method :get + :url (endpoint (:path request) (:id request)) + :params (:query request) + :mode :cors + :referrer :no-referrer + :credentials :omit + :timeout 10000 + :response-content-types {#"application/.*json" :json} + :on-success [:get-more-success] + :on-failure [:get-question-failure]} + :db (-> db + (assoc-in [:loading :more] true) + (assoc :more-db-path db-path))})) + +(reg-event-db + :get-more-success + (fn [db [_ {body :body}]] + (let [db-path (get-in db [:more-db-path])] + (-> db + (assoc-in [:loading :more] false) + (update-in db-path into (:answers body)) + (assoc-in [:post :paging] (:paging body)))))) + (reg-event-db :initialize-db (fn [_ _] diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index bac8925..c399467 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -48,7 +48,10 @@ (defn question-page [] (let [loading @(subscribe [:loading]) post @(subscribe [:post]) - end? (-> post :paging :is_end)] + end? (-> post :paging :is_end) + get-more (fn [event params] + (.preventDefault event) + (dispatch [:get-more params]))] (if (:question loading) [:p "Loading..."] [:div @@ -68,17 +71,19 @@ {:__html (:content ans)}}]])] (if end? [:p {:class "mt-5"} "answers end here."] - (if (:comment loading) - [:p {:class "mt-5"} "loading comment..."] + (if (:more loading) + [:p {:class "mt-5"} "loading more..."] [:div {:class "mt-5"} [:button {:type "button" :href (-> post :paging :next) :class "btn btn-blue" - :on-click #(rfe/push-state - :question - {:id (-> post :paging :question_id)} - {:cursor (-> post :paging :cursor) - :session_id (-> post :paging :session_id)})} + :on-click #(get-more % + {:request {:path "hq" + :id (-> post :paging :question_id) + :query {:cursor (-> post :paging :cursor) + :session_id (-> post :paging :session_id)}} + :db-path [:post :answers] + :loading-type :comment})} "Load More"]]))]))) (defn nav [{:keys [current-route]}]