From 8c20ed32f339e7cc940b2ac73dcc88cfb9a3850b Mon Sep 17 00:00:00 2001 From: SouthFox Date: Sun, 17 Sep 2023 18:00:16 +0800 Subject: [PATCH] [front/perf] load more answers --- src/main/frontend/events.cljs | 42 ++++++++++++++++++++++++----------- src/main/frontend/views.cljs | 20 +++++++++-------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index 54de236..e4d356b 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -69,26 +69,42 @@ (reg-event-fx :get-question (fn [{:keys [db]} [_ params]] - {: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]} + (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 :question] true))})) + :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))))) (reg-event-db :get-question-success (fn [db [_ {body :body}]] (-> db (assoc-in [:loading :question] false) - (assoc :post body)))) + (assoc-in [:loading :comment] false) + (merge-question body)))) (reg-event-db :get-question-failure diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index d940a53..b6f16b8 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -68,15 +68,17 @@ {:__html (:content ans)}}]])] (if end? [:p "answers end here."] - [:div - [: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)})}]])]))) + (if (:comment loading) + [:p "loading comment..."] + [:div + [: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)})}]]))]))) (defn nav [{:keys [current-route]}] (let [active #(when (= % (-> current-route :data :name)) "> ")]