[front/feat] store question history

This commit is contained in:
SouthFox 2023-09-20 20:59:56 +08:00
parent 0006e788d3
commit 5d2191dad3
4 changed files with 55 additions and 21 deletions

View file

@ -26,20 +26,18 @@
(case page (case page
;; -- URL @ "/" -------------------------------------------------------- ;; -- URL @ "/" --------------------------------------------------------
:frontpage {:db set-page} :frontpage {:db set-page}
;; -- URL @ "/history" --------------------------------------------------------
:history {:db set-page}
;; -- URL @ "/about" -------------------------------------------------------- ;; -- URL @ "/about" --------------------------------------------------------
:about {:db set-page} :about {:db set-page}
;; -- URL @ "/hp" --------------------------------------------------------
;; -- URL @ "/item" --------------------------------------------------------
:item {:db set-page :item {:db set-page
:dispatch [:get-page {:id id :dispatch [:get-page {:id id
:query query}]} :query query}]}
;; -- URL @ "/hq" --------------------------------------------------------
;; -- URL @ "/item" --------------------------------------------------------
:question {:db set-page :question {:db set-page
:dispatch [:get-question {:id id :dispatch [:get-question {:id id
:query query}]} :query query}]}))))
))))
(reg-event-fx (reg-event-fx
:get-page :get-page
@ -53,7 +51,6 @@
:response-content-types {#"application/.*json" :json} :response-content-types {#"application/.*json" :json}
:on-success [:get-page-success] :on-success [:get-page-success]
:on-failure [:get-page-failure]} :on-failure [:get-page-failure]}
:db (-> db :db (-> db
(assoc-in [:loading :post] true))})) (assoc-in [:loading :post] true))}))
@ -92,28 +89,31 @@
set-history-interceptor set-history-interceptor
(fn [{:keys [db]} history] (fn [{:keys [db]} history]
(let [history-item (first history) (let [history-item (first history)
update? (fn [x] select-item (fn [x]
(and (= (:id x) (:id history-item)) (and (= (:id x) (:id history-item))
(= (:type x) (:type history-item))))] (= (:type x) (:type history-item))))]
{:db (as-> db $ {:db (take 50
(remove update? $) (conj
(conj $ history-item))}))) (remove select-item db)
(merge (first (filter select-item db)) history-item)))})))
(reg-event-fx (reg-event-fx
:get-question-success :get-question-success
(fn [{:keys [db]} [_ {body :body}]] (fn [{:keys [db]} [_ {body :body}]]
(let [id (get-in db [:current-route :path-params :id]) (let [id (get-in db [:current-route :path-params :id])
type (get-in db [:current-route :data :name]) type (get-in db [:current-route :data :name])
title (get-in body [:question :title]) question (get body :question)
detail (get-in body [:question :detail])] params (if (nil? question)
{:id id
:type type}
{:id id
:type type
:title (get-in body [:question :title])
:detail (get-in body [:question :detail])})]
{:db (-> db {:db (-> db
(assoc-in [:loading :question] false) (assoc-in [:loading :question] false)
(assoc-in [:post] body)) (assoc-in [:post] body))
:dispatch [:set-history {:id id :dispatch [:set-history params]})))
:type type
:title title
:detail detail
:page 1}]})))
(reg-event-db (reg-event-db
:get-question-failure :get-question-failure

View file

@ -11,6 +11,10 @@
{:name :frontpage {:name :frontpage
:view frontend.views/home-page :view frontend.views/home-page
:link-text "Home"}] :link-text "Home"}]
["history"
{:name :history
:view frontend.views/history-page
:link-text "History"}]
["about" ["about"
{:name :about {:name :about
:view frontend.views/about-page :view frontend.views/about-page

View file

@ -12,6 +12,11 @@
(fn [db _] (fn [db _]
(:loading db))) (:loading db)))
(reg-sub
:history
(fn [db _]
(:history db)))
(reg-sub (reg-sub
:post :post
(fn [db _] (fn [db _]

View file

@ -32,6 +32,25 @@
[:a [:a
{:href "https://git.southfox.me/southfox/liberty-hu"} "Source code"]]]]) {:href "https://git.southfox.me/southfox/liberty-hu"} "Source code"]]]])
(defn history-page []
(let [histories @(subscribe [:history])]
[:div {:class "space-y-5"}
(for [history-item histories]
^{:key history-item}
[:div {:class "p-3 bg-white shadow cursor-pointer drop-shadow-xl hover:bg-slate-200"
:on-click #(rfe/push-state
(:type history-item)
{:id (:id history-item)}
(:query history-item))}
[:p {:class "text-2xl"}
(:title history-item)]
[:div {:class "mt-3"}
[:div
{:dangerouslySetInnerHTML
{:__html (:detail history-item)}}]]
[:div {:class "text-sm text-slate-400 text-end"}
(get-in history-item [:query :cursor])]])]))
(defn item-page [] (defn item-page []
(let [loading @(subscribe [:loading]) (let [loading @(subscribe [:loading])
post @(subscribe [:post])] post @(subscribe [:post])]
@ -51,7 +70,11 @@
end? (-> post :paging :is_end) end? (-> post :paging :is_end)
get-more (fn [event params] get-more (fn [event params]
(.preventDefault event) (.preventDefault event)
(dispatch [:get-more params]))] (dispatch [:get-more params])
(dispatch [:set-history {:id (get-in params [:request :id])
:type :question
:query (get-in params [:request :query])
}]))]
(if (:question loading) (if (:question loading)
[:p "Loading..."] [:p "Loading..."]
[:div [:div
@ -94,6 +117,8 @@
[:ul {:class "nav flex flex-col overflow-hidden"} [:ul {:class "nav flex flex-col overflow-hidden"}
[:li [:li
[:a {:href (rfe/href :frontpage)} (active :frontpage) "Home"]] [:a {:href (rfe/href :frontpage)} (active :frontpage) "Home"]]
[:li
[:a {:href (rfe/href :history)} (active :history) "History"]]
[:li [:li
[:a {:href (rfe/href :about)} (active :about) "About"]]])) [:a {:href (rfe/href :about)} (active :about) "About"]]]))