diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index 85cedba498..a8f6806043 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -257,35 +257,41 @@ (fn [cofx [chat-id chat-props]] (models/add-chat cofx chat-id chat-props))) -(defn navigate-to-chat +(defn- ensure-chat-exists + "Takes chat-id and coeffects map and returns fx to create chat if it doesn't exist" + [chat-id cofx] + (when-not (get-in cofx [:db :chats chat-id]) + (models/add-chat cofx chat-id))) + +(defn- navigate-to-chat "Takes coeffects map and chat-id, returns effects necessary for navigation and preloading data" - ([cofx chat-id] - (navigate-to-chat cofx chat-id false)) - ([cofx chat-id navigation-replace?] - (let [nav-fn (if navigation-replace? - #(navigation/replace-view % :chat) - #(navigation/navigate-to % :chat))] - (-> (preload-chat-data cofx chat-id) - (update :db nav-fn))))) + [chat-id navigation-replace? cofx] + (let [nav-fn (if navigation-replace? + #(navigation/replace-view % :chat) + #(navigation/navigate-to % :chat))] + (-> (preload-chat-data cofx chat-id) + (update :db nav-fn)))) (handlers/register-handler-fx :navigate-to-chat [re-frame/trim-v] (fn [cofx [chat-id {:keys [navigation-replace?]}]] - (navigate-to-chat cofx chat-id navigation-replace?))) + (navigate-to-chat chat-id navigation-replace? cofx))) + +(defn start-chat + "Start a chat, making sure it exists" + [chat-id navigation-replace? {:keys [db] :as cofx}] + (when (not= (:current-public-key db) chat-id) ; don't allow to open chat with yourself + (handlers/merge-fx + cofx + (ensure-chat-exists chat-id) + (navigate-to-chat chat-id navigation-replace?)))) (handlers/register-handler-fx :start-chat [(re-frame/inject-cofx :get-stored-chat) re-frame/trim-v] - (fn [{:keys [db] :as cofx} [contact-id {:keys [navigation-replace?]}]] - (when (not= (:current-public-key db) contact-id) ; don't allow to open chat with yourself - (if (get (:chats db) contact-id) - (navigate-to-chat cofx contact-id navigation-replace?) ; existing chat, just preload and displey - (let [add-chat-fx (models/add-chat cofx contact-id)] ; new chat, create before preload & display - (merge add-chat-fx - (navigate-to-chat (assoc cofx :db (:db add-chat-fx)) - contact-id - navigation-replace?))))))) + (fn [cofx [contact-id {:keys [navigation-replace?]}]] + (start-chat contact-id navigation-replace? cofx))) ;; TODO(janherich): remove this unnecessary event in the future (only model function `update-chat` will stay) (handlers/register-handler-fx @@ -346,4 +352,4 @@ [re-frame/trim-v] (fn [{:keys [db]} [chat-id]] (merge (remove-chats db chat-id) - {:dispatch [:navigation-replace :home]}))) \ No newline at end of file + {:dispatch [:navigation-replace :home]}))) diff --git a/src/status_im/ui/screens/profile/contact/views.cljs b/src/status_im/ui/screens/profile/contact/views.cljs index 770f6c7ea2..3a6c33935b 100644 --- a/src/status_im/ui/screens/profile/contact/views.cljs +++ b/src/status_im/ui/screens/profile/contact/views.cljs @@ -16,7 +16,7 @@ toolbar/default-nav-back [toolbar/content-title ""]]) -(defn actions [{:keys [pending? whisper-identity dapp?]} chat-id] +(defn actions [{:keys [pending? whisper-identity dapp?]}] (concat (if pending? [{:label (i18n/label :t/add-to-contacts) :icon :icons/add-contact @@ -30,7 +30,7 @@ (when-not dapp? [{:label (i18n/label :t/send-transaction) :icon :icons/arrow-right - :action #(re-frame/dispatch [:profile/send-transaction chat-id whisper-identity])}]))) + :action #(re-frame/dispatch [:profile/send-transaction whisper-identity])}]))) (defn profile-info-item [{:keys [label value options accessibility-label]}] [react/view styles/profile-info-item @@ -54,8 +54,7 @@ (defview profile [] (letsubs [identity [:current-contact-identity] - maybe-contact [:contact] - chat-id [:get :current-chat-id]] + maybe-contact [:contact]] (let [contact (or maybe-contact (utils.contacts/whisper-id->new-contact identity))] [react/view profile.components.styles/profile [status-bar/status-bar] @@ -63,7 +62,7 @@ [react/scroll-view [react/view profile.components.styles/profile-form [profile.components/profile-header contact false false nil nil]] - [list/action-list (actions contact chat-id) + [list/action-list (actions contact) {:container-style styles/action-container :action-style styles/action :action-label-style styles/action-label diff --git a/src/status_im/ui/screens/profile/events.cljs b/src/status_im/ui/screens/profile/events.cljs index 697d01c84e..45ce6a8d64 100644 --- a/src/status_im/ui/screens/profile/events.cljs +++ b/src/status_im/ui/screens/profile/events.cljs @@ -27,12 +27,11 @@ (handlers/register-handler-fx :profile/send-transaction - [re-frame/trim-v] + [(re-frame/inject-cofx :get-stored-chat) re-frame/trim-v] (fn [{{:contacts/keys [contacts] :as db} :db :as cofx} [chat-id]] - (let [send-command (get-in contacts chat-const/send-command-ref)] - (-> (chat-events/navigate-to-chat cofx chat-id) - (as-> fx - (merge fx (input-events/select-chat-input-command (:db fx) send-command nil true))))))) + (let [send-command (get-in contacts chat-const/send-command-ref) + fx (chat-events/start-chat chat-id true cofx)] + (merge fx (input-events/select-chat-input-command (:db fx) send-command nil true))))) (defn get-current-account [{:accounts/keys [current-account-id] :as db}] (get-in db [:accounts/accounts current-account-id]))