better handling of context

this interceptor only produces side effects, so no matter what just return context
This commit is contained in:
Rob Culliton 2018-05-04 17:25:06 -04:00
parent 222d334c25
commit d801af1287
No known key found for this signature in database
GPG Key ID: 6FDEF60B3DC84D94
1 changed files with 20 additions and 23 deletions

View File

@ -1,10 +1,10 @@
(ns commiteth.interceptors (ns commiteth.interceptors
(:require [commiteth.db :as db] (:require [commiteth.db :as db]
[re-frame.core :refer [->interceptor [re-frame.core :as rf]
dispatch]]
[clojure.data :as data])) [clojure.data :as data]))
(defn bounty-confirm-hashes [owner-bounties] (defn bounty-confirm-hashes [owner-bounties]
"for app-db representation of owner bounties, return a list of confirm hashes"
(-> owner-bounties (-> owner-bounties
vals vals
(->> (map #(:confirm_hash %))))) (->> (map #(:confirm_hash %)))))
@ -19,7 +19,8 @@
More information on re-frame interceptors can be found here: More information on re-frame interceptors can be found here:
https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md" https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md"
(->interceptor
(rf/->interceptor
:id :confirm-hash-update :id :confirm-hash-update
:after (fn confirm-hash-update-after :after (fn confirm-hash-update-after
[context] [context]
@ -29,27 +30,23 @@
first) first)
start-ob (get-in context [:coeffects :db :owner-bounties]) start-ob (get-in context [:coeffects :db :owner-bounties])
end-ob (get-in context [:effects :db :owner-bounties])] end-ob (get-in context [:effects :db :owner-bounties])]
(if (empty? start-ob) (when (not-empty start-ob)
;; don't treat initial load as an update to watch for
context
(let [[only-before only-after both] (data/diff (let [[only-before only-after both] (data/diff
(bounty-confirm-hashes start-ob) (bounty-confirm-hashes start-ob)
(bounty-confirm-hashes end-ob))] (bounty-confirm-hashes end-ob))]
;; making it here means ther was a change in the confirm hashes (when only-after ;; confirm hashes changed, now find out which ones
;; that was not the result of just setting them on page load (let [updated-bounties (->> end-ob
(if only-after
;; TODO we'll need to backtrack to get the issue-id
;; in order to dispatch confirm payout
;; now let's see if we can get the issue-id of the one that chagned
(let [updated-issues (->> end-ob
(filter (fn [[issue-id owner-bounty]] (filter (fn [[issue-id owner-bounty]]
(let [current-confirm-hash (:confirm_hash owner-bounty) (let [current-confirm-hash (:confirm_hash owner-bounty)
old-confirm-hash (get-in start-ob [issue-id :confirm_hash])] old-confirm-hash (get-in start-ob [issue-id :confirm_hash])]
(println "old-confirm-hash is" old-confirm-hash) (and (nil? old-confirm-hash) (some? current-confirm-hash)))))
(println "new confirm-hash is" current-confirm-hash)
(and (nil? old-confirm-hash) (some? new-confirm-hash)))))
vals)] vals)]
(println "updated issues are " updated-issues)) ;; for bounties which just had confirm hash set: perform side effect
context))))))) ;; but interceptor must return context
(map (fn [bounty]
(rf/dispatch [:confirm-payout {:issue_id (:issue-id bounty)
:owner_address (:owner-address bounty)
:contract_address (:contract_address bounty)
:confirm_hash (:confirm-hash bounty)}])) updated-bounties)))))
context))))