From 64aa07a572b87b822866e3fc32e4523b785e5280 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Tue, 12 Sep 2023 19:08:35 +0800 Subject: [PATCH] [front/feat] show more answers --- src/main/frontend/events.cljs | 10 ++++++---- src/main/frontend/router.cljs | 11 ++++++----- src/main/frontend/views.cljs | 24 ++++++++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/frontend/events.cljs b/src/main/frontend/events.cljs index bd0164c..54de236 100644 --- a/src/main/frontend/events.cljs +++ b/src/main/frontend/events.cljs @@ -4,7 +4,6 @@ [clojure.string :as str])) - (if js/goog.DEBUG (def api-url "http://localhost:3000/api") (def api-url "./api")) @@ -16,7 +15,7 @@ (reg-event-fx :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]) set-page (assoc db :current-route new-match)] (case page @@ -27,11 +26,13 @@ ;; -- URL @ "/item" -------------------------------------------------------- :item {:db set-page - :dispatch [:get-page {:id id}]} + :dispatch [:get-page {:id id + :query query}]} ;; -- URL @ "/item" -------------------------------------------------------- :question {:db set-page - :dispatch [:get-question {:id id}]} + :dispatch [:get-question {:id id + :query query}]} )))) @@ -70,6 +71,7 @@ (fn [{:keys [db]} [_ params]] {:fetch {:method :get :url (endpoint "hq" (:id params)) + :params (:query params) :mode :cors :referrer :no-referrer :credentials :omit diff --git a/src/main/frontend/router.cljs b/src/main/frontend/router.cljs index a3c38c1..872b5d3 100644 --- a/src/main/frontend/router.cljs +++ b/src/main/frontend/router.cljs @@ -25,13 +25,14 @@ {:name :question :view frontend.views/question-page :link-text "Question" - :parameters {:path {:id int?} - :query {(ds/opt :foo) keyword?}}}]]]) + :parameters {:path {:id int?}}}]]]) (def router (rf/router routes)) -(def on-navigate - #(dispatch [:set-active-page {:new-match % - :id (get-in % [:path-params :id])}])) +(defn on-navigate + [match _] + (dispatch [:set-active-page {:new-match match + :id (get-in match [:path-params :id]) + :query (get match :query-params)}])) diff --git a/src/main/frontend/views.cljs b/src/main/frontend/views.cljs index 34b2798..5a1b78e 100644 --- a/src/main/frontend/views.cljs +++ b/src/main/frontend/views.cljs @@ -1,6 +1,6 @@ (ns frontend.views (:require [reitit.frontend.easy :as rfe] - [re-frame.core :refer [subscribe]])) + [re-frame.core :refer [subscribe dispatch]])) (defn button @@ -13,7 +13,6 @@ "Post " id] [:br]]) - (defn home-page [] [:div [:h2 "Try this!"] @@ -21,14 +20,12 @@ (button 96817849) (button 24425284)]) - (defn about-page [] [:div [:h2 "About"] [:ul [:li [:a {:href "https://git.southfox.me/southfox/liberty-hu"} "Source code"]]]]) - (defn item-page [] (let [loading @(subscribe [:loading]) post @(subscribe [:post])] @@ -44,7 +41,8 @@ (defn question-page [] (let [loading @(subscribe [:loading]) - post @(subscribe [:post])] + post @(subscribe [:post]) + end? (-> post :paging :is_end)] (if (:question loading) [:p "Loading..."] [:div @@ -56,13 +54,23 @@ [:div {:class "p-3 bg-white shadow rounded-lg"} [:div {:class "flex items-center mb-3"} [:img {:class "h-10 rounded" - :src (-> ans :author :avatar_url)}] + :src (-> ans :author :avatar_url)}] [:a {:class "text-lg ml-3"} (-> ans :author :name)]] [:div {: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]}] (let [active #(when (= % (-> current-route :data :name)) "> ")]