[front/refactor] show more logic
This commit is contained in:
parent
5685ae67a2
commit
44fbfc7ead
2 changed files with 52 additions and 38 deletions
|
@ -69,9 +69,6 @@
|
|||
(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)
|
||||
|
@ -82,29 +79,15 @@
|
|||
: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)))))
|
||||
(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 [_ _]
|
||||
|
|
|
@ -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]}]
|
||||
|
|
Loading…
Reference in a new issue