[front/feat] show question page

This commit is contained in:
SouthFox 2023-09-04 08:03:46 +08:00
parent 8dec6f8900
commit 82145993dc
3 changed files with 63 additions and 1 deletions

View file

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

View file

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

View file

@ -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)) "> ")]