Skip to content

Commit

Permalink
Add chat invite
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferossgp committed Jul 22, 2020
1 parent 3ec1959 commit bd8b001
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ SPEC CHECKSUMS:
Flipper-RSocket: 64e7431a55835eb953b0bf984ef3b90ae9fdddd7
FlipperKit: afd4259ef9eadeeb2d30250b37d95cb3b6b97a69
Folly: 30e7936e1c45c08d884aa59369ed951a8e68cf51
glog: 1f3da668190260b06b429bb211bfbee5cd790c28
glog: 682164e7ac67e41afd8f7b6a37a96d04caf61cc0
OpenSSL-Universal: 8b48cc0d10c1b2923617dfe5c178aa9ed2689355
RCTRequired: cec6a34b3ac8a9915c37e7e4ad3aa74726ce4035
RCTTypeSafety: 93006131180074cffa227a1075802c89a49dd4ce
Expand Down
16 changes: 13 additions & 3 deletions src/status_im/acquisition/chat.cljs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
(ns status-im.acquisition.chat
(:require [status-im.utils.fx :as fx]
(:require [re-frame.core :as re-frame]
[status-im.utils.fx :as fx]
[status-im.acquisition.claim :as claim]
[status-im.ethereum.core :as ethereum]
[status-im.acquisition.persistance :as persistence]
[status-im.acquisition.gateway :as gateway]
[status-im.chat.models :as chat]))

(fx/defn start-acquisition
[{:keys [db] :as cofx} {:keys [key] :as referrer}]
{:db (assoc-in db [:acquisition :chat-referrer key] referrer)
::persistence/chat-initialized? (fn [state]
(when-not (= "initialized" state)
(re-frame/dispatch [::start-chat referrer])))})

(fx/defn start-chat
{:events [::start-chat]}
[cofx {:keys [key] :as referrer}]
(fx/merge cofx
{:db (assoc-in db [:acquisition :chat-referrer key] referrer)}
{::persistence/chat-initalized! true}
(chat/start-chat key)))

(fx/defn accept-pack
{:events [::advertiser-decision]}
{:events [::accept-pack]}
[{:keys [db] :as cofx} decision]
(let [referral (get-in db [:acquisition :referrer])
payload {:chat_key (get-in db [:multiaccount :public-key])
Expand Down
20 changes: 11 additions & 9 deletions src/status_im/acquisition/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,21 @@
{:events [::has-referrer]}
[{:keys [db] :as cofx} decision referrer]
(when referrer
(cond
(nil? decision)
(fx/merge cofx
{:db (assoc-in db [:acquisition :referrer] referrer)}
(fx/merge cofx
{:db (-> db
(assoc-in [:acquisition :referrer] referrer)
(assoc-in [:acquisition :decision] decision))}
(cond
(nil? decision)
(gateway/get-referrer
referrer
(fn [resp]
[::referrer-registered referrer resp])))
[::referrer-registered referrer resp]))

(= "accept" decision)
{::persistence/check-tx-state (fn [tx]
(when-not (nil? tx)
(re-frame/dispatch [::add-tx-watcher tx])))})))
(= "accept" decision)
{::persistence/check-tx-state (fn [tx]
(when-not (nil? tx)
(re-frame/dispatch [::add-tx-watcher tx])))}))))

(fx/defn app-setup
{}
Expand Down
19 changes: 19 additions & 0 deletions src/status_im/acquisition/persistance.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[taoensso.timbre :as log]
["@react-native-community/async-storage" :default async-storage]))

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

Expand Down Expand Up @@ -39,3 +40,21 @@
(.setItem tx-store-key tx)
(.catch (fn [error]
(log/error "[async-storage]" error))))))

(re-frame/reg-fx
::chat-initalized!
(fn []
(-> ^js async-storage
(.setItem chat-initialized-key "initialized")
(.catch (fn [error]
(log/error "[async-storage]" error))))))

(re-frame/reg-fx
::chat-initialized?
(fn [on-success]
(-> ^js async-storage
(.getItem chat-initialized-key)
(.then (fn [^js data]
(on-success data)))
(.catch (fn [error]
(log/error "[async-storage]" error))))))
5 changes: 3 additions & 2 deletions src/status_im/ui/components/invite/accept.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
(let [starter-pack-amount @(re-frame/subscribe [::acquisition/starter-pack])
all-tokens @(re-frame/subscribe [:wallet/all-tokens])
chain @(re-frame/subscribe [:ethereum/chain-keyword])
eth (:eth-amount starter-pack-amount)
tokens (->> (get starter-pack-amount :tokens)
(map #(tokens/address->token all-tokens %))
(into [(get tokens/all-native-currencies chain)])
(into (when (pos? eth) [(get tokens/all-native-currencies chain)]))
(mapv (fn [v i k] [k v i])
(into [(:eth-amount starter-pack-amount)]
(into (when (pos? eth) [eth])
(get starter-pack-amount :tokens-amount))
(range)))]
[rn/view
Expand Down
125 changes: 125 additions & 0 deletions src/status_im/ui/components/invite/chat.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
(ns status-im.ui.components.invite.chat
(:require [quo.design-system.colors :as colors]
[quo.design-system.spacing :as spacing]
[quo.react-native :as rn]
[re-frame.core :as re-frame]
[status-im.ethereum.tokens :as tokens]
[status-im.acquisition.core :as acquisition]
[clojure.string :as cstr]
[quo.core :as quo]
[status-im.acquisition.chat :as acquisition.chat]
[status-im.utils.config :as config]
[status-im.ui.components.invite.events :as invite]
[status-im.i18n :as i18n]
[status-im.ui.components.invite.style :as styles]))

(defn messages-wrapper []
(:small spacing/padding-vertical))

(defn message-wrapper []
{:padding-right 96
:padding-left 8
:flex-direction :row})

(defn message-view []
{:border-top-left-radius 16
:border-top-right-radius 16
:border-bottom-right-radius 16
:border-radius 8
:border-bottom-left-radius 4
:border-width 1
:margin-bottom 4
:border-color (:border-01 @colors/theme)})

(defn message-text []
(merge (:small spacing/padding-horizontal)
{:padding-vertical 6}))

(defn author-text []
(merge (:small spacing/padding-horizontal)
{:padding-top (:tiny spacing/spacing)
:paddingbottom (:x-tiny spacing/spacing)}))

(defn button-message []
{:flex-direction :row
:justify-content :center
:align-items :center})

(defn starter-pack-style []
(merge (:small spacing/padding-horizontal)
(:small spacing/padding-vertical)
{:background-color (:interactive-02 @colors/theme)
:flex-direction :row
:border-bottom-width 1
:border-top-width 1
:border-color (:border-02 @colors/theme)}))

(defn starter-pack []
(let [{:keys [tokens eth-amount tokens-amount]}
@(re-frame/subscribe [::acquisition/starter-pack])

all-tokens @(re-frame/subscribe [:wallet/all-tokens])
chain @(re-frame/subscribe [:ethereum/chain-keyword])
tokens (->> tokens
(map #(tokens/address->token all-tokens %))
(into [(get tokens/all-native-currencies chain)])
(mapv (fn [v i k] [k v i])
(into [eth-amount] tokens-amount)
(range)))
reward-text (cstr/join ", " (map (comp name :symbol first) tokens))]
[rn/view {:style (starter-pack-style)}
[rn/view {:style {:padding-right 16}}
(doall
(for [[{name :name
{source :source} :icon} _ idx] tokens]
^{:key name}
[rn/view {:style (styles/reward-token-icon idx)}
[rn/image {:source (if (fn? source) (source) source)
:style {:width 40
:height 40}}]]))]
[rn/view {:style styles/reward-description}
[quo/text {:weight :medium}
"Starter Pack"]
[quo/text {} reward-text]]]))

(defn render-message [{:keys [content]}]
[rn/view {:style (message-wrapper)}
[rn/view {:style (message-view)}
(for [{:keys [type value]} content]
(case type
:text [rn/view {:style (message-text)}
[quo/text value]]
:pack [starter-pack]
:author [rn/view {:style (author-text)}
[quo/text {:color :secondary}
value]]
:button [rn/view {:style (button-message)}
value]
nil))]])

(defn reward-messages []
(when config/referrals-invite-enabled?
(let [pending-invite @(re-frame/subscribe [::invite/pending-chat-invite])
messages [{:content [{:type :text
:value "👋"}]}
{:content [{:type :author
:value (i18n/label :t/invite-chat-name)}
{:type :text
:value (i18n/label :t/invite-chat-intro)}
{:type :pack}
{:type :button
:value [quo/button {:type :secondary
:on-press #(re-frame/dispatch [::acquisition.chat/accept-pack])}
(i18n/label :t/invite-chat-accept)]}]}
{:content [{:type :text
:value (i18n/label :t/invite-chat-rule)}]}
{:content [{:type :text
:value [:<>
(i18n/label :t/invite-privacy-policy1) " "
[quo/text {:color :link
:on-press #(re-frame/dispatch [::invite/terms-and-conditions])}
(i18n/label :t/invite-privacy-policy2)]]}]}]]
(when pending-invite
[rn/view {:style (messages-wrapper)}
(for [message messages]
[render-message message])]))))
10 changes: 10 additions & 0 deletions src/status_im/ui/components/invite/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@
(fn []
(get-in @db [:acquisition :referral-reward])))))

(re-frame/reg-sub
::pending-chat-invite
(fn [db]
(let [chat-id (get db :current-chat-id)
{:keys [referrer decision]} (get db :acquisition)
{:keys [attributed]} (get-in db [:acquisition :chat-referrer chat-id])]
(and referrer
(not attributed)
(nil? decision)))))

(re-frame/reg-sub
:invite/accounts-reward
(fn [db]
Expand Down
6 changes: 5 additions & 1 deletion src/status_im/ui/screens/chat/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
[status-im.ui.components.topbar :as topbar]
[status-im.ui.screens.chat.group :as chat.group]
[status-im.ui.screens.chat.message.gap :as gap]
[status-im.ui.components.invite.chat :as invite.chat]
[status-im.ui.screens.chat.components.accessory :as accessory]
[status-im.ui.screens.chat.components.input :as components]
[status-im.ui.screens.chat.message.datemark :as message-datemark])
Expand Down Expand Up @@ -146,7 +147,10 @@
:ref #(reset! messages-list-ref %)
:header (when (and group-chat (not public?))
[chat.group/group-chat-footer chat-id])
:footer [chat-intro-header-container chat no-messages?]
:footer [:<>
[chat-intro-header-container chat no-messages?]
(when (and (not group-chat) (not public?))
[invite.chat/reward-messages])]
:data messages
:inverted true
:render-fn (fn [{:keys [outgoing type] :as message} idx]
Expand Down
4 changes: 4 additions & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@
"invite-reward-friend-description": "Your friend will receive a Starter Pack consisting of some {{reward}} to get started",
"invite-privacy-policy1": "By accepting you agree to the Starter Pack",
"invite-privacy-policy2": "Terms and Conditions.",
"invite-chat-name": "Friend referral",
"invite-chat-intro": "You were referred by a friend to join Status. Here’s some crypto to get you started! Use it to register an ENS name or buy a sticker pack",
"invite-chat-accept": "Accept",
"invite-chat-rule": "Accepting will also reward your 
friend with a crypto referral bonus",
"attribution-received": "{{attrib}} out of {{max}} bonuses received",
"advertiser-starter-pack-title": "Starter Pack",
"advertiser-starter-pack-description": "Welcome to Status! Here is some crypto to get you started",
Expand Down

0 comments on commit bd8b001

Please sign in to comment.