[front/feat] show post

This commit is contained in:
SouthFox 2023-08-25 19:02:07 +08:00
parent 97561beed8
commit 16b4a83c45
3 changed files with 39 additions and 25 deletions

View file

@ -4,7 +4,10 @@
[clojure.string :as str]))
(def api-url "http://localhost:3000/hp")
(if js/goog.DEBUG
(def api-url "http://localhost:3000/api")
(def api-url "./api"))
(defn endpoint
"Concat params to api-uri by /"
@ -20,36 +23,36 @@
(js/console.log page)
(case page
;; -- URL @ "/" --------------------------------------------------------
:frontpage {:db set-page
:dispatch [:get-page]}
:frontpage {:db set-page}
;; -- URL @ "/about" --------------------------------------------------------
:about {:db set-page}
:about {:db set-page}
;; -- URL @ "/item" --------------------------------------------------------
:item {:db set-page}
))))
:item {:db set-page
:dispatch [:get-page {:id id}]}))))
(reg-event-fx
:get-page
(fn [{:keys [db]} _]
{:db (assoc-in db [:loading :tags] true)
:fetch {:method :get
:url "https://api.github.com/orgs/day8"
:mode :cors
:referrer :no-referrer
:credentials :omit
:timeout 5000
:response-content-types {#"application/.*json" :json}
:on-success [:get-page-success]
:on-failure [:initialize-db]
}}))
(fn [{:keys [db]} [_ params]]
{:fetch {:method :get
:url (endpoint "hp" (:id params))
:mode :cors
:referrer :no-referrer
:credentials :omit
:timeout 10000
:response-content-types {#"application/.*json" :json}
:on-success [:get-page-success]
:on-failure [:initialize-db]}
:db (-> db
(assoc-in [:loading :post] true))}))
(reg-event-db
:get-page-success
(fn [db [_ {repos_url :body}]]
(fn [db [_ {body :body}]]
(-> db
(assoc :repos_url (:id repos_url)) )))
(assoc-in [:loading :post] false)
(assoc :post (:content body)))))
(reg-event-db
:initialize-db

View file

@ -6,3 +6,13 @@
:current-route
(fn [db]
(:current-route db)))
(reg-sub
:loading
(fn [db _]
(:loading db)))
(reg-sub
:post
(fn [db _]
(:post db)))

View file

@ -18,10 +18,11 @@
[:li [:a {:href "stub"} "Source code"]]]])
(defn item-page []
(let [current-route @(subscribe [:current-route])
id (get-in current-route [:path-params :id])]
(let [loading @(subscribe [:loading])
post @(subscribe [:post])]
[:div
[:h2 "Selected item " id]]))
{:dangerouslySetInnerHTML
{:__html post}}]))
(defn nav [{:keys [current-route]}]
(let [active #(when (= % (-> current-route :data :name)) "> ")]
@ -31,7 +32,7 @@
[:li
[:a {:href (rfe/href :about)} (active :about) "About"]]
[:li
[:a {:href (rfe/href :item {:id 233})} (active :item) "Item 233"]]]))
[:a {:href (rfe/href :item {:id 96817849})} (active :item) "Item 96817849"]]]))
(defn current-page []