|
1 | 1 | (ns status-im.contexts.chat.home.add-new-contact.events
|
2 | 2 | (:require
|
3 | 3 | [clojure.string :as string]
|
| 4 | + [re-frame.core :as re-frame] |
4 | 5 | [status-im.common.validators :as validators]
|
5 | 6 | [status-im.contexts.chat.contacts.events :as data-store.contacts]
|
6 | 7 | status-im.contexts.chat.home.add-new-contact.effects
|
7 |
| - [status-im.navigation.events :as navigation] |
8 | 8 | [utils.ens.stateofus :as stateofus]
|
9 | 9 | [utils.ethereum.chain :as chain]
|
10 |
| - [utils.re-frame :as rf] |
11 | 10 | [utils.string :as utils.string]))
|
12 | 11 |
|
13 | 12 | (defn init-contact
|
|
87 | 86 |
|
88 | 87 | (def validate-contact (comp ->state ->type ->id))
|
89 | 88 |
|
90 |
| -(defn dispatcher [event input] (fn [arg] (rf/dispatch [event input arg]))) |
| 89 | +(declare build-contact) |
91 | 90 |
|
92 |
| -(rf/defn set-new-identity |
93 |
| - {:events [:contacts/set-new-identity]} |
94 |
| - [{:keys [db]} input scanned] |
| 91 | +(defn set-new-identity |
| 92 | + [{:keys [db]} [{:keys [input build-success-fn failure-fn]}]] |
95 | 93 | (let [user-public-key (get-in db [:profile/profile :public-key])
|
96 | 94 | {:keys [input id ens state]
|
97 | 95 | :as contact} (-> {:user-public-key user-public-key
|
98 | 96 | :input input
|
99 |
| - :scanned scanned} |
| 97 | + :scanned input} |
100 | 98 | init-contact
|
101 | 99 | validate-contact)]
|
102 | 100 | (case state
|
103 |
| - |
104 | 101 | :empty {:db (dissoc db :contacts/new-identity)}
|
105 | 102 | (:valid :invalid) {:db (assoc db :contacts/new-identity contact)}
|
106 | 103 | :decompress-key {:db (assoc db :contacts/new-identity contact)
|
107 | 104 | :serialization/decompress-public-key
|
108 | 105 | {:compressed-key id
|
109 | 106 | :on-success
|
110 |
| - (dispatcher :contacts/set-new-identity-success input) |
| 107 | + #(re-frame/dispatch [:contacts/set-new-identity-success |
| 108 | + {:input input |
| 109 | + :pubkey % |
| 110 | + :build-success-fn build-success-fn}]) |
111 | 111 | :on-error
|
112 |
| - (dispatcher :contacts/set-new-identity-error input)}} |
| 112 | + #(re-frame/dispatch [:contacts/set-new-identity-error |
| 113 | + {:input input |
| 114 | + :pubkey % |
| 115 | + :failure-fn failure-fn}])}} |
113 | 116 | :resolve-ens {:db (assoc db :contacts/new-identity contact)
|
114 | 117 | :effects.contacts/resolve-public-key-from-ens
|
115 | 118 | {:chain-id (chain/chain-id db)
|
116 | 119 | :ens ens
|
117 | 120 | :on-success
|
118 |
| - (dispatcher :contacts/set-new-identity-success input) |
| 121 | + #(re-frame/dispatch [:contacts/set-new-identity-success |
| 122 | + {:input input |
| 123 | + :pubkey % |
| 124 | + :build-success-fn build-success-fn}]) |
119 | 125 | :on-error
|
120 |
| - (dispatcher :contacts/set-new-identity-error input)}}))) |
121 |
| - |
| 126 | + #(re-frame/dispatch [:contacts/set-new-identity-error |
| 127 | + {:input input |
| 128 | + :pubkey % |
| 129 | + :failure-fn failure-fn}])}}))) |
122 | 130 |
|
| 131 | +(re-frame/reg-event-fx :contacts/set-new-identity set-new-identity) |
123 | 132 |
|
124 |
| -(rf/defn build-contact |
125 |
| - {:events [:contacts/build-contact]} |
126 |
| - [_ pubkey ens open-profile-modal?] |
127 |
| - {:json-rpc/call [{:method "wakuext_buildContact" |
128 |
| - :params [{:publicKey pubkey |
129 |
| - :ENSName ens}] |
130 |
| - :js-response true |
131 |
| - :on-success #(rf/dispatch [:contacts/contact-built |
132 |
| - pubkey |
133 |
| - open-profile-modal? |
134 |
| - (data-store.contacts/<-rpc-js %)])}]}) |
135 |
| - |
136 |
| -(rf/defn contact-built |
137 |
| - {:events [:contacts/contact-built]} |
138 |
| - [{:keys [db]} pubkey open-profile-modal? contact] |
139 |
| - (merge {:db (assoc-in db [:contacts/contacts pubkey] contact)} |
140 |
| - (when open-profile-modal? |
141 |
| - {:dispatch [:open-modal :profile]}))) |
142 |
| - |
143 |
| -(rf/defn set-new-identity-success |
144 |
| - {:events [:contacts/set-new-identity-success]} |
145 |
| - [{:keys [db]} input pubkey] |
| 133 | +(defn- set-new-identity-success |
| 134 | + [{:keys [db]} [{:keys [input pubkey build-success-fn]}]] |
146 | 135 | (let [contact (get-in db [:contacts/new-identity])]
|
147 | 136 | (when (= (:input contact) input)
|
148 |
| - (rf/merge {:db (assoc db |
149 |
| - :contacts/new-identity |
150 |
| - (->state (assoc contact :public-key pubkey)))} |
151 |
| - (build-contact pubkey (:ens contact) false))))) |
152 |
| - |
153 |
| -(rf/defn set-new-identity-error |
154 |
| - {:events [:contacts/set-new-identity-error]} |
155 |
| - [{:keys [db]} input err] |
| 137 | + {:db (assoc db :contacts/new-identity (->state (assoc contact :public-key pubkey))) |
| 138 | + :dispatch [:contacts/build-contact |
| 139 | + {:pubkey pubkey |
| 140 | + :ens (:ens contact) |
| 141 | + :success-fn build-success-fn}]}))) |
| 142 | + |
| 143 | +(re-frame/reg-event-fx :contacts/set-new-identity-success set-new-identity-success) |
| 144 | + |
| 145 | +(defn- set-new-identity-error |
| 146 | + [{:keys [db]} [{:keys [input err failure-fn]}]] |
156 | 147 | (let [contact (get-in db [:contacts/new-identity])]
|
157 | 148 | (when (= (:input contact) input)
|
158 | 149 | (let [state (cond
|
|
163 | 154 | (string/includes? (:message err) "no such host")))
|
164 | 155 | {:state :invalid :msg :t/lost-connection}
|
165 | 156 | :else {:state :invalid})]
|
166 |
| - {:db (assoc db :contacts/new-identity (merge contact state))})))) |
| 157 | + (merge {:db (assoc db :contacts/new-identity (merge contact state))} |
| 158 | + (when failure-fn |
| 159 | + (failure-fn))))))) |
| 160 | + |
| 161 | +(re-frame/reg-event-fx :contacts/set-new-identity-error set-new-identity-error) |
167 | 162 |
|
168 |
| -(rf/defn clear-new-identity |
169 |
| - {:events [:contacts/clear-new-identity :contacts/new-chat-focus]} |
| 163 | +(defn- build-contact |
| 164 | + [_ [{:keys [pubkey ens success-fn]}]] |
| 165 | + {:json-rpc/call [{:method "wakuext_buildContact" |
| 166 | + :params [{:publicKey pubkey |
| 167 | + :ENSName ens}] |
| 168 | + :js-response true |
| 169 | + :on-success #(re-frame/dispatch [:contacts/build-contact-success |
| 170 | + {:pubkey pubkey |
| 171 | + :contact (data-store.contacts/<-rpc-js %) |
| 172 | + :success-fn success-fn}])}]}) |
| 173 | + |
| 174 | +(re-frame/reg-event-fx :contacts/build-contact build-contact) |
| 175 | + |
| 176 | +(defn- build-contact-success |
| 177 | + [{:keys [db]} [{:keys [pubkey contact success-fn]}]] |
| 178 | + (merge {:db (assoc-in db [:contacts/contacts pubkey] contact)} |
| 179 | + (when success-fn |
| 180 | + (success-fn contact)))) |
| 181 | + |
| 182 | +(re-frame/reg-event-fx :contacts/build-contact-success build-contact-success) |
| 183 | + |
| 184 | +(defn- clear-new-identity |
170 | 185 | [{:keys [db]}]
|
171 | 186 | {:db (dissoc db :contacts/new-identity)})
|
172 | 187 |
|
173 |
| -(rf/defn qr-code-scanned |
174 |
| - {:events [:contacts/qr-code-scanned]} |
175 |
| - [{:keys [db] :as cofx} scanned] |
176 |
| - (rf/merge cofx |
177 |
| - (set-new-identity scanned scanned) |
178 |
| - (navigation/navigate-back))) |
| 188 | +(re-frame/reg-event-fx :contacts/clear-new-identity clear-new-identity) |
179 | 189 |
|
180 |
| -(rf/defn set-new-identity-reconnected |
| 190 | +(defn set-new-identity-reconnected |
181 | 191 | [{:keys [db]}]
|
182 | 192 | (let [input (get-in db [:contacts/new-identity :input])]
|
183 |
| - (rf/dispatch [:contacts/set-new-identity input]))) |
| 193 | + (re-frame/dispatch [:contacts/set-new-identity {:input input}]))) |
| 194 | + |
0 commit comments