include custom interceptor for logging changes to the confirm hashes of owner bounties

This commit is contained in:
Rob Culliton 2018-05-04 15:49:23 -04:00
parent cafb39279b
commit a6287e64b6
No known key found for this signature in database
GPG Key ID: 6FDEF60B3DC84D94
3 changed files with 63 additions and 3 deletions

View File

@ -7,6 +7,7 @@
[commiteth.routes]
[commiteth.handlers]
[commiteth.subscriptions]
[commiteth.interceptors]
[commiteth.activity :refer [activity-page]]
[commiteth.bounties :refer [bounties-page]]
[commiteth.repos :refer [repos-page]]

View File

@ -15,7 +15,8 @@
:refer [reg-co-fx!]]
[commiteth.ui-model :as ui-model]
[commiteth.common :as common]
[commiteth.routes :as routes]))
[commiteth.routes :as routes]
[commiteth.interceptors]))
(rf-storage/reg-co-fx! :commiteth-sob {:fx :store
@ -68,7 +69,6 @@
(reg-event-fx
:initialize-web3
interceptors
(fn [{:keys [db]} [_]]
(let [injected-web3 (common/web3)
w3 (when (boolean injected-web3)
@ -214,7 +214,6 @@
(reg-event-fx
:load-owner-bounties
interceptors
(fn [{:keys [db]} [_]]
{:db (assoc db :owner-bounties-loading? true)
:http {:method GET
@ -223,6 +222,7 @@
(reg-event-db
:set-owner-bounties
[commiteth.interceptors/confirm-hash-update]
(fn [db [_ issues]]
(assoc db
:owner-bounties issues

View File

@ -0,0 +1,59 @@
(ns commiteth.interceptors
(:require [commiteth.db :as db]
[re-frame.core :refer [->interceptor
dispatch]]
[clojure.data :as data]))
(defn bounty-confirm-hashes [owner-bounties]
(-> owner-bounties
vals
(->> (map #(:confirm_hash %)))))
(def confirm-hash-update
"An interceptor which exaimines the event diff for `:load-owner-bounties`
and dispatches a `confirm-payout` event when one of the owner bounties has
its confirm_hash updated.
*Warning* this inteceptor is only intended for use with the
`:load-owner-bounties` event
More information on re-frame interceptors can be found here:
https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md"
(->interceptor
:id :confirm-hash-update
:after (fn confirm-hash-update-after
[context]
(let [event-name (-> context
:coeffects
:event
first)
start-ob (get-in context [:coeffects :db :owner-bounties])
end-ob (get-in context [:effects :db :owner-bounties])]
(if (empty? start-ob)
;; don't treat initial load as an update to watch for
context
(let [[only-before only-after both] (data/diff
(bounty-confirm-hashes start-ob)
(bounty-confirm-hashes end-ob))]
;; we only care about the case where
;; any of them were nill before and set currently
;; but first lets log these to see where we're at
(println "whole before: " start-ob)
(println "whole after: " end-ob)
(println "diff before: " only-before)
(println "diff after: " only-after)
(println "equal at:" both)
(if only-after
;; TODO we'll need to backtrack to get the issue-id
;; in order to dispatch confirm payout
(println "new confirm hash detected!"))
context
#_(when changes-after
(do (println "examining changes in:" event-name)
(println "after:" changes-after)
;; now we need to see if the update included
;; confirm hash
))))))))