From 82145993dc18ea66ce172c74e9987ee9796f44f8 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Mon, 4 Sep 2023 08:03:46 +0800 Subject: [PATCH] [front/feat] show question page --- src/main/frontend/events.cljs | 38 ++++++++++++++++++++++++++++++++++- src/main/frontend/router.cljs | 6 ++++++ src/main/frontend/views.cljs | 20 ++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index 54f59d4..bd0164c 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -27,7 +27,13 @@ ;; -- URL @ "/item" -------------------------------------------------------- :item {:db set-page - :dispatch [:get-page {:id id}]})))) + :dispatch [:get-page {:id id}]} + + ;; -- URL @ "/item" -------------------------------------------------------- + :question {:db set-page + :dispatch [:get-question {:id id}]} + )))) + (reg-event-fx :get-page @@ -59,6 +65,36 @@ (assoc-in [:loading :post] false) (assoc :post body)))) +(reg-event-fx + :get-question + (fn [{:keys [db]} [_ params]] + {:fetch {:method :get + :url (endpoint "hq" (:id 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 :post body)))) + +(reg-event-db + :get-question-failure + (fn [db [_ {body :body}]] + (-> db + (assoc-in [:loading :question] false) + (assoc :post body)))) + (reg-event-db :initialize-db (fn [_ _] diff --git a/src/main/frontend/router.cljs b/src/main/frontend/router.cljs index d97e4e8..9deae04 100644 --- a/src/main/frontend/router.cljs +++ b/src/main/frontend/router.cljs @@ -19,6 +19,12 @@ {:name :item :view frontend.views/item-page :link-text "Item" + :parameters {:path {:id int?} + :query {(ds/opt :foo) keyword?}}}] + ["hq/:id" + {:name :question + :view frontend.views/question-page + :link-text "Question" :parameters {:path {:id int?} :query {(ds/opt :foo) keyword?}}}]]]) diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index c7d8e61..63bdbb8 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -42,6 +42,26 @@ {:dangerouslySetInnerHTML {:__html (:content post)}}]]))) +(defn question-page [] + (let [loading @(subscribe [:loading]) + post @(subscribe [:post])] + (if (:question loading) + [:p "Loading..."] + [:div + [:h1 {:class "text-2xl"} + (-> post :question :title)] + [:div {:class "space-y-5"} + (for [ans (:answers post)] + ^{:key ans} + [:div {:class "p-3 bg-white shadow rounded-lg"} + [:img + {:src (-> ans :author :avatar_url)}] + [:a {:class "text-lg"} + (-> ans :author :name)] + [:div + {:dangerouslySetInnerHTML + {:__html (:content ans)}}] + ])]]))) (defn nav [{:keys [current-route]}] (let [active #(when (= % (-> current-route :data :name)) "> ")]