[front/feat] show more answers

This commit is contained in:
SouthFox 2023-09-12 19:08:35 +08:00
parent 36112c3195
commit 64aa07a572
3 changed files with 28 additions and 17 deletions

View file

@ -4,7 +4,6 @@
[clojure.string :as str])) [clojure.string :as str]))
(if js/goog.DEBUG (if js/goog.DEBUG
(def api-url "http://localhost:3000/api") (def api-url "http://localhost:3000/api")
(def api-url "./api")) (def api-url "./api"))
@ -16,7 +15,7 @@
(reg-event-fx (reg-event-fx
:set-active-page :set-active-page
(fn [{:keys [db]} [_ {:keys [new-match id]}]] (fn [{:keys [db]} [_ {:keys [new-match id query]}]]
(let [page (get-in new-match [:data :name]) (let [page (get-in new-match [:data :name])
set-page (assoc db :current-route new-match)] set-page (assoc db :current-route new-match)]
(case page (case page
@ -27,11 +26,13 @@
;; -- URL @ "/item" -------------------------------------------------------- ;; -- URL @ "/item" --------------------------------------------------------
:item {:db set-page :item {:db set-page
:dispatch [:get-page {:id id}]} :dispatch [:get-page {:id id
:query query}]}
;; -- URL @ "/item" -------------------------------------------------------- ;; -- URL @ "/item" --------------------------------------------------------
:question {:db set-page :question {:db set-page
:dispatch [:get-question {:id id}]} :dispatch [:get-question {:id id
:query query}]}
)))) ))))
@ -70,6 +71,7 @@
(fn [{:keys [db]} [_ params]] (fn [{:keys [db]} [_ params]]
{:fetch {:method :get {:fetch {:method :get
:url (endpoint "hq" (:id params)) :url (endpoint "hq" (:id params))
:params (:query params)
:mode :cors :mode :cors
:referrer :no-referrer :referrer :no-referrer
:credentials :omit :credentials :omit

View file

@ -25,13 +25,14 @@
{:name :question {:name :question
:view frontend.views/question-page :view frontend.views/question-page
:link-text "Question" :link-text "Question"
:parameters {:path {:id int?} :parameters {:path {:id int?}}}]]])
:query {(ds/opt :foo) keyword?}}}]]])
(def router (def router
(rf/router (rf/router
routes)) routes))
(def on-navigate (defn on-navigate
#(dispatch [:set-active-page {:new-match % [match _]
:id (get-in % [:path-params :id])}])) (dispatch [:set-active-page {:new-match match
:id (get-in match [:path-params :id])
:query (get match :query-params)}]))

View file

@ -1,6 +1,6 @@
(ns frontend.views (ns frontend.views
(:require [reitit.frontend.easy :as rfe] (:require [reitit.frontend.easy :as rfe]
[re-frame.core :refer [subscribe]])) [re-frame.core :refer [subscribe dispatch]]))
(defn button (defn button
@ -13,7 +13,6 @@
"Post " id] "Post " id]
[:br]]) [:br]])
(defn home-page [] (defn home-page []
[:div [:div
[:h2 "Try this!"] [:h2 "Try this!"]
@ -21,14 +20,12 @@
(button 96817849) (button 96817849)
(button 24425284)]) (button 24425284)])
(defn about-page [] (defn about-page []
[:div [:div
[:h2 "About"] [:h2 "About"]
[:ul [:ul
[:li [:a {:href "https://git.southfox.me/southfox/liberty-hu"} "Source code"]]]]) [:li [:a {:href "https://git.southfox.me/southfox/liberty-hu"} "Source code"]]]])
(defn item-page [] (defn item-page []
(let [loading @(subscribe [:loading]) (let [loading @(subscribe [:loading])
post @(subscribe [:post])] post @(subscribe [:post])]
@ -44,7 +41,8 @@
(defn question-page [] (defn question-page []
(let [loading @(subscribe [:loading]) (let [loading @(subscribe [:loading])
post @(subscribe [:post])] post @(subscribe [:post])
end? (-> post :paging :is_end)]
(if (:question loading) (if (:question loading)
[:p "Loading..."] [:p "Loading..."]
[:div [:div
@ -56,13 +54,23 @@
[:div {:class "p-3 bg-white shadow rounded-lg"} [:div {:class "p-3 bg-white shadow rounded-lg"}
[:div {:class "flex items-center mb-3"} [:div {:class "flex items-center mb-3"}
[:img {:class "h-10 rounded" [:img {:class "h-10 rounded"
:src (-> ans :author :avatar_url)}] :src (-> ans :author :avatar_url)}]
[:a {:class "text-lg ml-3"} [:a {:class "text-lg ml-3"}
(-> ans :author :name)]] (-> ans :author :name)]]
[:div [:div
{:dangerouslySetInnerHTML {:dangerouslySetInnerHTML
{:__html (:content ans)}}] {:__html (:content ans)}}]])]
])]]))) (if end?
[:p "answers end here."]
[:div
[:button {:type "button"
:href (-> post :paging :next)
:class "btn btn-blue"
:on-click #(rfe/push-state
:question
{:id (-> post :paging :question_id)}
{:cursor (-> post :paging :cursor)
:session_id (-> post :paging :session_id)})}]])])))
(defn nav [{:keys [current-route]}] (defn nav [{:keys [current-route]}]
(let [active #(when (= % (-> current-route :data :name)) "> ")] (let [active #(when (= % (-> current-route :data :name)) "> ")]