[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
: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 [_ _]

View file

@ -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]}]