diff --git a/src/cljs/commiteth/core.cljs b/src/cljs/commiteth/core.cljs index 5dc1854..693403b 100644 --- a/src/cljs/commiteth/core.cljs +++ b/src/cljs/commiteth/core.cljs @@ -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]] diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index efe7ce9..4e43dea 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -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 diff --git a/src/cljs/commiteth/interceptors.cljs b/src/cljs/commiteth/interceptors.cljs new file mode 100644 index 0000000..131c425 --- /dev/null +++ b/src/cljs/commiteth/interceptors.cljs @@ -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 + ))))))))