[front/refactor] show more logic

This commit is contained in:
SouthFox 2023-09-19 22:49:55 +08:00
parent 5685ae67a2
commit 44fbfc7ead
2 changed files with 52 additions and 38 deletions

View file

@ -69,42 +69,25 @@
(reg-event-fx (reg-event-fx
:get-question :get-question
(fn [{:keys [db]} [_ params]] (fn [{:keys [db]} [_ params]]
(let [loading (if (empty? (:query params)) {:fetch {:method :get
:question :url (endpoint "hq" (:id params))
:comment)] :params (:query params)
{:fetch {:method :get :mode :cors
:url (endpoint "hq" (:id params)) :referrer :no-referrer
:params (:query params) :credentials :omit
:mode :cors :timeout 10000
:referrer :no-referrer :response-content-types {#"application/.*json" :json}
:credentials :omit :on-success [:get-question-success]
:timeout 10000 :on-failure [:get-question-failure]}
:response-content-types {#"application/.*json" :json} :db (-> db
:on-success [:get-question-success] (assoc-in [:loading :question] true))}))
: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)))))
(reg-event-db (reg-event-db
:get-question-success :get-question-success
(fn [db [_ {body :body}]] (fn [db [_ {body :body}]]
(-> db (-> db
(assoc-in [:loading :question] false) (assoc-in [:loading :question] false)
(assoc-in [:loading :comment] false) (assoc-in [:post] body))))
(merge-question body))))
(reg-event-db (reg-event-db
:get-question-failure :get-question-failure
@ -113,6 +96,32 @@
(assoc-in [:loading :question] false) (assoc-in [:loading :question] false)
(assoc :post body)))) (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 (reg-event-db
:initialize-db :initialize-db
(fn [_ _] (fn [_ _]

View file

@ -48,7 +48,10 @@
(defn question-page [] (defn question-page []
(let [loading @(subscribe [:loading]) (let [loading @(subscribe [:loading])
post @(subscribe [:post]) 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) (if (:question loading)
[:p "Loading..."] [:p "Loading..."]
[:div [:div
@ -68,17 +71,19 @@
{:__html (:content ans)}}]])] {:__html (:content ans)}}]])]
(if end? (if end?
[:p {:class "mt-5"} "answers end here."] [:p {:class "mt-5"} "answers end here."]
(if (:comment loading) (if (:more loading)
[:p {:class "mt-5"} "loading comment..."] [:p {:class "mt-5"} "loading more..."]
[:div {:class "mt-5"} [:div {:class "mt-5"}
[:button {:type "button" [:button {:type "button"
:href (-> post :paging :next) :href (-> post :paging :next)
:class "btn btn-blue" :class "btn btn-blue"
:on-click #(rfe/push-state :on-click #(get-more %
:question {:request {:path "hq"
{:id (-> post :paging :question_id)} :id (-> post :paging :question_id)
{:cursor (-> post :paging :cursor) :query {:cursor (-> post :paging :cursor)
:session_id (-> post :paging :session_id)})} :session_id (-> post :paging :session_id)}}
:db-path [:post :answers]
:loading-type :comment})}
"Load More"]]))]))) "Load More"]]))])))
(defn nav [{:keys [current-route]}] (defn nav [{:keys [current-route]}]