Skip to content

Commit

Permalink
Cleanup business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferossgp committed Jul 24, 2020
1 parent 6c28c89 commit e3bddc1
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 306 deletions.
2 changes: 1 addition & 1 deletion src/status_im/acquisition/advertiser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
:method "PATCH"
:url (gateway/get-url :clicks referral)
:on-success ::claim/success-starter-pack-claim})
{::persistence/set-referrer-decision "decline"})
{::persistence/set-referrer-state :declined})
(popover/hide-popover))))
12 changes: 6 additions & 6 deletions src/status_im/acquisition/claim.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
(fx/defn success-tx-received
{:events [::success-tx-received]}
[_]
{::persistence/set-referrer-decision "claimed"
::notifications/local-notification {:title (i18n/label :t/starter-pack-received)
:message (i18n/label :t/starter-pack-received-description)}})
{::persistence/set-referrer-state :claimed
::notifications/local-notification {:title (i18n/label :t/starter-pack-received)
:message (i18n/label :t/starter-pack-received-description)}})

(fx/defn add-tx-watcher
[cofx tx]
Expand All @@ -25,7 +25,7 @@
{:events [::success-starter-pack-claim]}
[cofx {:keys [tx]}]
(fx/merge cofx
{::persistence/set-watch-tx tx
::persistence/set-referrer-state :accepted}
(add-tx-watcher tx)
(notifications/request-permission)
{::persistence/set-wtach-tx tx
::persistence/set-referrer-decision "accept"}))
(notifications/request-permission)))
103 changes: 21 additions & 82 deletions src/status_im/acquisition/core.cljs
Original file line number Diff line number Diff line change
@@ -1,46 +1,16 @@
(ns status-im.acquisition.core
(:require [clojure.string :as cstr]
[re-frame.core :as re-frame]
[reagent.ratom :refer [make-reaction]]
(:require [re-frame.core :as re-frame]
[status-im.utils.fx :as fx]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.ethereum.contracts :as contracts]
[status-im.utils.money :as money]
[status-im.acquisition.chat :as chat]
[status-im.acquisition.advertiser :as advertiser]
[status-im.acquisition.persistance :as persistence]
[status-im.acquisition.gateway :as gateway]
["react-native-device-info" :refer [getInstallReferrer]]))
[status-im.acquisition.install-referrer :as install-referrer]))

(def not-found-code "notfound.click_id")
(def advertiser-type "advertiser")
(def chat-type "chat")

(defn- split-param [param]
(->
(cstr/split param #"=")
(concat (repeat ""))
(->>
(take 2))))

(defn- url-decode
[string]
(some-> string str (cstr/replace #"\+" "%20") (js/decodeURIComponent)))

(defn- query->map
[qstr]
(when-not (cstr/blank? qstr)
(some->> (cstr/split qstr #"&")
seq
(mapcat split-param)
(map url-decode)
(apply hash-map))))

(defn parse-referrer
"Google return query params for referral with all utm tags"
[referrer]
(-> referrer query->map (get "referrer")))

(fx/defn handle-registration
[cofx {:keys [message on-success]}]
(gateway/handle-acquisition cofx
Expand All @@ -52,27 +22,27 @@
(re-frame/reg-fx
::get-referrer
(fn []
(persistence/get-referrer-decision
(persistence/get-referrer-flow-state
(fn [^js data]
(-> (getInstallReferrer)
(.then (fn [install-referrer]
(persistence/set-referrer install-referrer)
(when (and (seq (parse-referrer install-referrer))
(not= install-referrer "unknown"))
(re-frame/dispatch [::has-referrer data (parse-referrer install-referrer)])))))))))
(install-referrer/get-referrer
(fn [install-referrer]
(persistence/set-referrer install-referrer)
(when (not= install-referrer "unknown")
(when-let [referrer (install-referrer/parse-referrer install-referrer)]
(re-frame/dispatch [::has-referrer data referrer])))))))))

(re-frame/reg-fx
::check-referrer
(fn [external-referrer]
(persistence/get-referrer-decision
(persistence/get-referrer-flow-state
(fn [^js data]
(if external-referrer
(re-frame/dispatch [::has-referrer data external-referrer])
(persistence/get-referrer
(fn [install-referrer]
(when (and (seq (parse-referrer install-referrer))
(not= install-referrer "unknown"))
(re-frame/dispatch [::has-referrer data (parse-referrer install-referrer)])))))))))
(when (not= install-referrer "unknown")
(when-let [referrer (install-referrer/parse-referrer install-referrer)]
(re-frame/dispatch [::has-referrer data referrer]))))))))))

(fx/defn referrer-registered
{:events [::referrer-registered]}
Expand All @@ -90,28 +60,25 @@
(fx/defn outdated-referrer
{:events [::outdated-referrer]}
[_ _]
{::persistence/set-referrer-decision "outdated"})
{::persistence/set-referrer-state :outdated})

(fx/defn has-referrer
{:events [::has-referrer]}
[{:keys [db] :as cofx} decision referrer]
[{:keys [db] :as cofx} flow-state referrer]
(when referrer
(fx/merge cofx
{:db (-> db
(assoc-in [:acquisition :referrer] referrer)
(assoc-in [:acquisition :decision] decision))}
(assoc-in [:acquisition :flow-state] flow-state))}
(cond
(nil? decision)
(nil? flow-state)
(gateway/get-referrer
referrer
(fn [resp]
[::referrer-registered referrer resp])
(fn [{:keys [code]}]
(= code not-found-code))
(fn [resp]
[::outdated-referrer resp]))
(fn [resp] [::referrer-registered referrer resp])
(fn [{:keys [code]}] (= code not-found-code))
(fn [resp] [::outdated-referrer resp]))

(= "accept" decision)
(= flow-state (:accepted persistence/referrer-state))
{::persistence/check-tx-state (fn [tx]
(when-not (nil? tx)
(re-frame/dispatch [::add-tx-watcher tx])))}))))
Expand All @@ -125,31 +92,3 @@
{}
[_]
{::check-referrer nil})

;; Starter pack

(fx/defn get-starter-pack-amount
{:events [::starter-pack-amount]}
[{:keys [db]} [_ eth-amount tokens tokens-amount sticker-packs]]
{:db (assoc-in db [:acquisition :starter-pack :pack]
{:eth-amount (money/wei->ether eth-amount)
:tokens tokens
:tokens-amount (mapv money/wei->ether tokens-amount)
:sticker-packs sticker-packs})})

(fx/defn starter-pack
{:events [::starter-pack]}
[{:keys [db]}]
(let [contract (contracts/get-address db :status/acquisition)]
{::json-rpc/eth-call [{:contract contract
:method "getPack()"
:outputs ["address" "uint256" "address[]" "uint256[]" "uint256[]"]
:on-success #(re-frame/dispatch [::starter-pack-amount (vec %)])}]}))

(re-frame/reg-sub-raw
::starter-pack
(fn [db]
(re-frame/dispatch [::starter-pack])
(make-reaction
(fn []
(get-in @db [:acquisition :starter-pack :pack])))))
34 changes: 34 additions & 0 deletions src/status_im/acquisition/install_referrer.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(ns status-im.acquisition.install-referrer
(:require [taoensso.timbre :as log]
[clojure.string :as cstr]
["react-native-device-info" :refer [getInstallReferrer]]))

(defn- split-param [param]
(->
(cstr/split param #"=")
(concat (repeat ""))
(->>
(take 2))))

(defn- url-decode
[string]
(some-> string str (cstr/replace #"\+" "%20") (js/decodeURIComponent)))

(defn- query->map
[qstr]
(when-not (cstr/blank? qstr)
(some->> (cstr/split qstr #"&")
seq
(mapcat split-param)
(map url-decode)
(apply hash-map))))

(defn parse-referrer
"Google return query params for referral with all utm tags"
[referrer]
(-> referrer query->map (get "referrer")))

(defn get-referrer [cb]
(-> (getInstallReferrer)
(.then cb)
(.catch #(log/error "[install-referrer]" %))))
19 changes: 13 additions & 6 deletions src/status_im/acquisition/persistance.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@
["@react-native-community/async-storage" :default async-storage]))

(def chat-initialized-key "acquisition-chat-initialized")
(def referrer-decision-key "acquisition-referrer-decision")
(def referrer-flow-state-key "acquisition-referrer-flow-state")
(def tx-store-key "acquisition-watch-tx")
(def referrer-key "acquisition-referrer")

(def referrer-state {:accepted "accepted"
:declined "declined"
:claimed "claimed"
:outdated "outdated"})

(re-frame/reg-fx
::set-referrer-decision
::set-referrer-state
(fn [decision]
(-> ^js async-storage
(.setItem referrer-decision-key decision)
(.setItem referrer-flow-state-key (get referrer-state decision))
(.then (fn []
(re-frame/dispatch [:set-in [:acquisition :flow-state] decision])))
(.catch (fn [error]
(log/error "[async-storage]" error))))))

(defn get-referrer-decision [on-success]
(defn get-referrer-flow-state [on-success]
(-> ^js async-storage
(.getItem referrer-decision-key)
(.getItem referrer-flow-state-key)
(.then (fn [^js data]
(on-success data)))
(.catch (fn [error]
Expand All @@ -35,7 +42,7 @@
(log/error "[async-storage]" error))))))

(re-frame/reg-fx
::set-wtach-tx
::set-watch-tx
(fn [tx]
(-> ^js async-storage
(.setItem tx-store-key tx)
Expand Down
2 changes: 0 additions & 2 deletions src/status_im/acquisition/service.cljs

This file was deleted.

99 changes: 0 additions & 99 deletions src/status_im/ui/components/invite/accept.cljs

This file was deleted.

Loading

0 comments on commit e3bddc1

Please sign in to comment.