From 09cd84a89b527809a78f1938bbd6796c06be5109 Mon Sep 17 00:00:00 2001 From: pablodip Date: Sun, 21 Jan 2018 10:27:32 +0100 Subject: [PATCH] sorting behaviour --- src/clj/commiteth/routes/services.clj | 3 ++- src/cljs/commiteth/bounties.cljs | 8 +++---- src/cljs/commiteth/subscriptions.cljs | 31 +++++++++++++++++-------- src/cljs/commiteth/ui_model.cljs | 33 +++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/clj/commiteth/routes/services.clj b/src/clj/commiteth/routes/services.clj index 82c5633..6a60fec 100644 --- a/src/clj/commiteth/routes/services.clj +++ b/src/clj/commiteth/routes/services.clj @@ -155,7 +155,8 @@ :value_usd :value-usd :claim_count :claim-count :balance_eth :balance-eth - :user_has_address :user-has-address}] + :user_has_address :user-has-address + :created_at :created-at}] (map #(-> % (rename-keys renames) (update :value-usd usd-decimal->str) diff --git a/src/cljs/commiteth/bounties.cljs b/src/cljs/commiteth/bounties.cljs index ffd3162..5169598 100644 --- a/src/cljs/commiteth/bounties.cljs +++ b/src/cljs/commiteth/bounties.cljs @@ -196,19 +196,19 @@ :on-blur #(reset! open? false)} [:div.open-bounties-sort-element {:on-click #(swap! open? not)} - (ui-model/bounty-sorting-types-def @current-sorting) + (ui-model/bounty-sorting-type->name @current-sorting) [:div.icon-forward-white-box [:img {:src "icon-forward-white.svg"}]]] (when @open? [:div.open-bounties-sort-element-tooltip - (for [[sorting-type sorting-name] ui-model/bounty-sorting-types-def] + (for [sorting-type (keys ui-model/bounty-sorting-types-def)] ^{:key (str sorting-type)} [:div.open-bounties-sort-type {:on-click #(do (reset! open? false) (rf/dispatch [::handlers/set-open-bounties-sorting-type sorting-type]))} - sorting-name])])])))) + (ui-model/bounty-sorting-type->name sorting-type)])])])))) (defn bounties-list [open-bounties] [:div.ui.container.open-bounties-container @@ -225,7 +225,7 @@ (defn bounties-page [] - (let [open-bounties (rf/subscribe [:open-bounties]) + (let [open-bounties (rf/subscribe [::subs/filtered-and-sorted-open-bounties]) open-bounties-loading? (rf/subscribe [:get-in [:open-bounties-loading?]])] (fn [] (if @open-bounties-loading? diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index 7ad64f9..d91f770 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -1,10 +1,11 @@ (ns commiteth.subscriptions (:require [re-frame.core :refer [reg-sub]] - [commiteth.db :as db])) + [commiteth.db :as db] + [commiteth.ui-model :as ui-model])) (reg-sub - :db - (fn [db _] db)) + :db + (fn [db _] db)) (reg-sub :page @@ -68,17 +69,17 @@ (get-in db path))) (reg-sub - :usage-metrics - (fn [db _] - (:usage-metrics db))) + :usage-metrics + (fn [db _] + (:usage-metrics db))) (reg-sub - :metrics-loading? - (fn [db _] - (:metrics-loading? db))) + :metrics-loading? + (fn [db _] + (:metrics-loading? db))) (reg-sub - :user-dropdown-open? + :user-dropdown-open? (fn [db _] (:user-dropdown-open? db))) @@ -110,3 +111,13 @@ (filter identity) set)] (into #{"ETH"} token-ids)))) + +(reg-sub + ::filtered-and-sorted-open-bounties + :<- [:open-bounties] + :<- [::open-bounties-sorting-type] + (fn [[open-bounties sorting-type] _] + (println "RAW" open-bounties) + (cond->> open-bounties + sorting-type (ui-model/sort-bounties-by-sorting-type sorting-type) + ))) diff --git a/src/cljs/commiteth/ui_model.cljs b/src/cljs/commiteth/ui_model.cljs index e274297..39dc764 100644 --- a/src/cljs/commiteth/ui_model.cljs +++ b/src/cljs/commiteth/ui_model.cljs @@ -2,10 +2,35 @@ ;;;; bounty sorting types -(def bounty-sorting-types-def {::bounty-sorting-type|most-recent "Most recent" - ::bounty-sorting-type|lowest-value "Lowest value" - ::bounty-sorting-type|highest-value "Highest value" - ::bounty-sorting-type|owner "Owner"}) +(def bounty-sorting-types-def + {::bounty-sorting-type|most-recent {::bounty-sorting-type.name "Most recent" + ::bounty-sorting-type.sort-key-fn (fn [bounty] + (:created-at bounty)) + ::bounty-sorting-type.sort-comparator-fn compare} + ::bounty-sorting-type|lowest-value {::bounty-sorting-type.name "Lowest value" + ::bounty-sorting-type.sort-key-fn (fn [bounty] + (:value-usd bounty)) + ::bounty-sorting-type.sort-comparator-fn compare} + ::bounty-sorting-type|highest-value {::bounty-sorting-type.name "Highest value" + ::bounty-sorting-type.sort-key-fn (fn [bounty] + (:value-usd bounty)) + ::bounty-sorting-type.sort-comparator-fn (comp - compare)} + ::bounty-sorting-type|owner {::bounty-sorting-type.name "Owner" + ::bounty-sorting-type.sort-key-fn (fn [bounty] + (:repo-owner bounty)) + ::bounty-sorting-type.sort-comparator-fn compare}}) + +(defn bounty-sorting-type->name [sorting-type] + (-> bounty-sorting-types-def (get sorting-type) ::bounty-sorting-type.name)) + +(defn sort-bounties-by-sorting-type [sorting-type bounties] + (let [keyfn (-> bounty-sorting-types-def + sorting-type + ::bounty-sorting-type.sort-key-fn) + comparator (-> bounty-sorting-types-def + sorting-type + ::bounty-sorting-type.sort-comparator-fn)] + (sort-by keyfn comparator bounties))) ;;;; bounty filter types