From 3a65dcd3c268d395a050b81a515b8ac629a98ef7 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Sat, 13 Jan 2018 19:30:40 +0000 Subject: [PATCH] Start chat when user adds a pending contact from the "new contact" view When a contact is removed it is set to pending?. If the same contact is added again we now dispatch a :start-chat event which initiate a 1-1 chat with them. --- src/status_im/chat/handlers.cljs | 6 +-- src/status_im/ui/screens/contacts/events.cljs | 11 +++--- test/cljs/status_im/test/contacts/events.cljs | 37 +++++++++++++++---- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 7ea23394ae..7dc00b7f38 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -1,5 +1,5 @@ -(ns status-im.chat.handlers - (:require [re-frame.core :refer [enrich after debug dispatch reg-fx]] +(ns status-im.chat.handlers + (:require [re-frame.core :refer [enrich after debug dispatch reg-fx]] [clojure.string :as string] [status-im.ui.components.styles :refer [default-chat-color]] [status-im.chat.constants :as chat-consts] @@ -13,7 +13,7 @@ console-chat-id]] [status-im.utils.random :as random] [status-im.utils.handlers :refer [register-handler register-handler-fx] :as u] - status-im.chat.events + status-im.chat.events status-im.chat.handlers.send-message)) (defn remove-chat diff --git a/src/status_im/ui/screens/contacts/events.cljs b/src/status_im/ui/screens/contacts/events.cljs index 752a21540c..b3c5ca45b7 100644 --- a/src/status_im/ui/screens/contacts/events.cljs +++ b/src/status_im/ui/screens/contacts/events.cljs @@ -439,8 +439,9 @@ (fn [{:keys [db]} [_ id]] (if (spec/valid? :global/address id) {::request-contact-by-address id} - {:dispatch (if (get-in db [:contacts/contacts id]) - [:add-pending-contact id] - [:add-new-contact-and-open-chat {:name (generate-gfy id) - :photo-path (identicon id) - :whisper-identity id}])}))) + {:dispatch-n (if (get-in db [:contacts/contacts id]) + [[:add-pending-contact id] + [:start-chat id {:navigation-replace? true}]] + [[:add-new-contact-and-open-chat {:name (generate-gfy id) + :photo-path (identicon id) + :whisper-identity id}]])}))) diff --git a/test/cljs/status_im/test/contacts/events.cljs b/test/cljs/status_im/test/contacts/events.cljs index 4eb1eb33da..ef86716995 100644 --- a/test/cljs/status_im/test/contacts/events.cljs +++ b/test/cljs/status_im/test/contacts/events.cljs @@ -124,6 +124,8 @@ (rf/reg-fx ::group-events/save-contact-group-property #()) (rf/reg-fx ::group-events/add-contacts-to-contact-group #()) + (rf/reg-fx :save-chat #()) + (rf/reg-cofx ::contacts-events/get-all-contacts (fn [coeffects _] @@ -152,7 +154,7 @@ load-contact-groups load-default-contacts (add-contact-groups, add-contacts, add-contacts-to-group ;TODO add-chat, load-commands!) add-contact-handler (add-new-contact-and-open-chat, status-im.contacts.events/add-new-contact, - status-im.contacts.events/send-contact-request ;TODO start-chat) + status-im.contacts.events/send-contact-request, status-im.chat.events.start-chat) contact-request-received (update-contact, watch-contact ;TODO :update-chat!) contact-update-received (update-contact ;TODO :update-chat!) hide-contact (update-contact ;TODO :account-update-keys) @@ -174,7 +176,8 @@ (rf/dispatch [:initialize-db]) (let [contacts (rf/subscribe [:get-contacts]) - contact-groups (rf/subscribe [:get-contact-groups])] + contact-groups (rf/subscribe [:get-contact-groups]) + view-id (rf/subscribe [:get :view-id])] (testing ":load-contacts event" @@ -230,17 +233,20 @@ :photo-path "" :whisper-identity new-contact-public-key :address new-contact-address} - contact (rf/subscribe [:contact-by-identity new-contact-public-key])] + contact (rf/subscribe [:contact-by-identity new-contact-public-key]) + current-chat-id (rf/subscribe [:get-current-chat-id])] (testing ":add-contact-handler event - new contact" + (rf/dispatch [:set :view-id nil]) + (rf/dispatch [:set :current-chat-id nil]) + ;; :add-contact-handler event dispatches next 4 events for new contact ;; ;; :add-new-contact-and-open-chat ;; :status-im.contacts.events/add-new-contact ;; :status-im.contacts.events/send-contact-request - ;;TODO :start-chat - (rf/reg-event-db :start-chat (fn [db _] db)) + ;; :status-im.chat.events/start-chat (rf/dispatch [:add-contact-handler new-contact-public-key]) @@ -251,7 +257,13 @@ (is (= new-contact (-> @contacts (get new-contact-public-key) - (assoc :photo-path "" :name "")))))) + (assoc :photo-path "" :name ""))))) + + (testing "it loads the 1-1 chat" + (is (= :chat @view-id))) + + (testing "it adds the new contact to the chat" + (is (= new-contact-public-key @current-chat-id)))) (testing ":contact-request-received event" @@ -324,14 +336,25 @@ ;; :add-pending-contact ;; :status-im.contacts.events/add-new-contact ;; :status-im.contacts.events/send-contact-request + ;; :status-im.chat.events/start-chat + ;;TODO :discoveries-send-portions (rf/reg-event-db :discoveries-send-portions (fn [db _] db)) + (rf/dispatch [:set :view-id nil]) + (rf/dispatch [:set :current-chat-id nil]) + (rf/dispatch [:add-contact-handler new-contact-public-key]) (testing "it sets the pending? flag to false" (is (= (assoc received-contact'' :pending? false) - (get @contacts new-contact-public-key))))) + (get @contacts new-contact-public-key)))) + + (testing "it loads the 1-1 chat" + (is (= :chat @view-id))) + + (testing "it adds the new contact to the chat" + (is (= new-contact-public-key @current-chat-id)))) (testing ":create-new-contact-group event"