[ISSUE #355] persist bounty filter to query string

This commit is contained in:
Marco Süß 2018-03-22 22:12:26 -06:00 committed by Tetiana Churikova
parent c83d2e143f
commit 5d852481ee
4 changed files with 89 additions and 15 deletions

View File

@ -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)}})))

View File

@ -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)))

View File

@ -179,7 +179,7 @@
(mapcat keys)
(filter identity)
set)]
(into #{"ETH"} token-ids))))
(into #{:ETH} token-ids))))
(reg-sub
::filtered-and-sorted-open-bounties

View File

@ -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)