From 5d852481eea9aefc94e344c6c9b70e504fa9a08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20S=C3=BC=C3=9F?= Date: Thu, 22 Mar 2018 22:12:26 -0600 Subject: [PATCH] [ISSUE #355] persist bounty filter to query string --- src/cljs/commiteth/handlers.cljs | 43 +++++++++++++++++++----- src/cljs/commiteth/routes.cljs | 11 ++++-- src/cljs/commiteth/subscriptions.cljs | 2 +- src/cljs/commiteth/ui_model.cljs | 48 +++++++++++++++++++++++++-- 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index 0f436dc..61447c1 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -13,7 +13,8 @@ :as rf-storage :refer [reg-co-fx!]] [commiteth.ui-model :as ui-model] - [commiteth.common :as common])) + [commiteth.common :as common] + [commiteth.routes :as routes])) (rf-storage/reg-co-fx! :commiteth-sob {:fx :store @@ -40,6 +41,20 @@ (println "redirecting to" path) (set! (.-pathname js/location) path))) +(reg-fx + :persist-bounty-filters-in-query + (fn [{:keys [bounty-filters]}] + (let [query + (->> bounty-filters + (remove (comp nil? val)) + (map (fn [[k v]] + [(ui-model/bounty-filter-type->query-param k) + (ui-model/bounty-filter-value->query-param k v)])) + (into {}))] + (routes/nav! :bounties {} (if (= {} query) + nil + query))))) + (reg-event-fx :initialize-db [(inject-cofx :store)] @@ -65,10 +80,17 @@ (reg-event-db :set-active-page - (fn [db [_ page]] + (fn [db [_ page params query]] (assoc db :page page :page-number 1 - ::db/open-bounties-filters {} + ::db/open-bounties-filters + (reduce-kv + #(let [type (ui-model/query-param->bounty-filter-type %2)] + (assoc %1 + type + (ui-model/query-param->bounty-filter-value type %3))) + {} + query) ::db/open-bounties-sorting-type ::ui-model/bounty-sorting-type|most-recent))) (reg-event-db @@ -468,9 +490,12 @@ (merge db {::db/open-bounties-sorting-type sorting-type :page-number 1}))) -(reg-event-db - ::set-open-bounty-filter-type - (fn [db [_ filter-type filter-value]] - (-> db - (assoc-in [::db/open-bounties-filters filter-type] filter-value) - (assoc :page-number 1)))) +(reg-event-fx + ::set-open-bounty-filter-type + (fn [{:keys [event db]} [_ filter-type filter-value]] + (println "db" db) + (let [filters (::db/open-bounties-filters db)] + (println "filters" filters) + {:persist-bounty-filters-in-query + {:bounty-filters + (assoc filters filter-type filter-value)}}))) diff --git a/src/cljs/commiteth/routes.cljs b/src/cljs/commiteth/routes.cljs index 1c3cc5e..c5cb2f5 100644 --- a/src/cljs/commiteth/routes.cljs +++ b/src/cljs/commiteth/routes.cljs @@ -14,12 +14,17 @@ "A function which will be called on each route change." [name params query] (println "Route change to: " name params query) - (rf/dispatch [:set-active-page name])) + (rf/dispatch [:set-active-page name params query])) (defn setup-nav! [] (bide/start! router {:default :bounties :on-navigate on-navigate})) -(defn nav! [route-id] - (bide/navigate! router route-id {})) +(defn nav! + ([route-id] + (nav! route-id nil)) + ([route-id params] + (nav! route-id params nil)) + ([route-id params query] + (bide/navigate! router route-id params query))) diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index a48ee67..3ecd6e2 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -179,7 +179,7 @@ (mapcat keys) (filter identity) set)] - (into #{"ETH"} token-ids)))) + (into #{:ETH} token-ids)))) (reg-sub ::filtered-and-sorted-open-bounties diff --git a/src/cljs/commiteth/ui_model.cljs b/src/cljs/commiteth/ui_model.cljs index d814a45..d953264 100644 --- a/src/cljs/commiteth/ui_model.cljs +++ b/src/cljs/commiteth/ui_model.cljs @@ -87,11 +87,11 @@ ::bounty-filter-type.category ::bounty-filter-type-category|multiple-dynamic-options ::bounty-filter-type.re-frame-subs-key-for-options :commiteth.subscriptions/open-bounties-currencies ::bounty-filter-type.predicate (fn [filter-value bounty] - (or (and (contains? filter-value "ETH") + (or (and (contains? filter-value :ETH) (< 0 (js/parseFloat (:balance-eth bounty)))) (not-empty (set/intersection - (->> filter-value (remove #{"ETH"}) set) + (->> filter-value (remove #{:ETH}) set) (-> bounty :tokens keys set)))))} ::bounty-filter-type|date @@ -124,6 +124,50 @@ (defn bounty-filter-type->name [filter-type] (-> bounty-filter-types-def (get filter-type) ::bounty-filter-type.name)) +(defn bounty-filter-type->query-param [filter-type] + (-> filter-type + name + (clojure.string/replace #".*\|" ""))) + +(defn bounty-filter-value->query-param [type value] + (let [category (-> bounty-filter-types-def type ::bounty-filter-type.category)] + (cond + (= category ::bounty-filter-type-category|multiple-dynamic-options) + (vec value) + + (= category ::bounty-filter-type-category|single-static-option) + (bounty-filter-type->query-param value) + + (= category ::bounty-filter-type-category|range) + (str (first value) "-" (second value))))) + +(defn query-param->bounty-filter-type [query-param] + (keyword (namespace ::_) (str "bounty-filter-type|" (name query-param)))) + +(defn query-param->bounty-filter-value [type value] + (let [category (-> bounty-filter-types-def type ::bounty-filter-type.category)] + (cond + (= type ::bounty-filter-type|currency) + (if (string? value) + #{(keyword value)} + (set (map keyword value))) + + (= type ::bounty-filter-type|owner) + (if (string? value) + #{value} + (set value)) + + (= type ::bounty-filter-type|claims) + (keyword (namespace ::_) + (str "bounty-filter-type-claims-option|" (name value))) + + (= type ::bounty-filter-type|date) + (keyword (namespace ::_) + (str "bounty-filter-type-date-option|" (name value))) + + (= category ::bounty-filter-type-category|range) + (clojure.string/split value #"-")))) + (defn bounty-filter-value->short-text [filter-type filter-value] (cond (= filter-type ::bounty-filter-type|date)