From 5d2191dad38177df95d3fb1185ac9a363147cf2b Mon Sep 17 00:00:00 2001 From: SouthFox Date: Wed, 20 Sep 2023 20:59:56 +0800 Subject: [PATCH] [front/feat] store question history --- src/main/frontend/events.cljs | 40 +++++++++++++++++------------------ src/main/frontend/router.cljs | 4 ++++ src/main/frontend/subs.cljs | 5 +++++ src/main/frontend/views.cljs | 27 ++++++++++++++++++++++- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index 4b4b2e7..df427e0 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -26,20 +26,18 @@ (case page ;; -- URL @ "/" -------------------------------------------------------- :frontpage {:db set-page} + ;; -- URL @ "/history" -------------------------------------------------------- + :history {:db set-page} ;; -- URL @ "/about" -------------------------------------------------------- :about {:db set-page} - - ;; -- URL @ "/item" -------------------------------------------------------- + ;; -- URL @ "/hp" -------------------------------------------------------- :item {:db set-page :dispatch [:get-page {:id id :query query}]} - - ;; -- URL @ "/item" -------------------------------------------------------- + ;; -- URL @ "/hq" -------------------------------------------------------- :question {:db set-page :dispatch [:get-question {:id id - :query query}]} - )))) - + :query query}]})))) (reg-event-fx :get-page @@ -53,7 +51,6 @@ :response-content-types {#"application/.*json" :json} :on-success [:get-page-success] :on-failure [:get-page-failure]} - :db (-> db (assoc-in [:loading :post] true))})) @@ -92,28 +89,31 @@ set-history-interceptor (fn [{:keys [db]} history] (let [history-item (first history) - update? (fn [x] - (and (= (:id x) (:id history-item)) + select-item (fn [x] + (and (= (:id x) (:id history-item)) (= (:type x) (:type history-item))))] - {:db (as-> db $ - (remove update? $) - (conj $ history-item))}))) + {:db (take 50 + (conj + (remove select-item db) + (merge (first (filter select-item db)) history-item)))}))) (reg-event-fx :get-question-success (fn [{:keys [db]} [_ {body :body}]] (let [id (get-in db [:current-route :path-params :id]) type (get-in db [:current-route :data :name]) - title (get-in body [:question :title]) - detail (get-in body [:question :detail])] + question (get body :question) + 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 (assoc-in [:loading :question] false) (assoc-in [:post] body)) - :dispatch [:set-history {:id id - :type type - :title title - :detail detail - :page 1}]}))) + :dispatch [:set-history params]}))) (reg-event-db :get-question-failure diff --git a/src/main/frontend/router.cljs b/src/main/frontend/router.cljs index 872b5d3..6220414 100644 --- a/src/main/frontend/router.cljs +++ b/src/main/frontend/router.cljs @@ -11,6 +11,10 @@ {:name :frontpage :view frontend.views/home-page :link-text "Home"}] + ["history" + {:name :history + :view frontend.views/history-page + :link-text "History"}] ["about" {:name :about :view frontend.views/about-page diff --git a/src/main/frontend/subs.cljs b/src/main/frontend/subs.cljs index 83af771..d4c4975 100644 --- a/src/main/frontend/subs.cljs +++ b/src/main/frontend/subs.cljs @@ -12,6 +12,11 @@ (fn [db _] (:loading db))) +(reg-sub + :history + (fn [db _] + (:history db))) + (reg-sub :post (fn [db _] diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index b77b3e6..c50bc03 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -32,6 +32,25 @@ [:a {: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 [] (let [loading @(subscribe [:loading]) post @(subscribe [:post])] @@ -51,7 +70,11 @@ end? (-> post :paging :is_end) get-more (fn [event params] (.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) [:p "Loading..."] [:div @@ -94,6 +117,8 @@ [:ul {:class "nav flex flex-col overflow-hidden"} [:li [:a {:href (rfe/href :frontpage)} (active :frontpage) "Home"]] + [:li + [:a {:href (rfe/href :history)} (active :history) "History"]] [:li [:a {:href (rfe/href :about)} (active :about) "About"]]]))