diff --git a/src/syng_im/components/chat/input/phone.cljs b/src/syng_im/components/chat/input/phone.cljs index ed8c71db1e..5c35704c4e 100644 --- a/src/syng_im/components/chat/input/phone.cljs +++ b/src/syng_im/components/chat/input/phone.cljs @@ -2,8 +2,11 @@ (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [syng-im.components.chat.input.simple-command :refer [simple-command-input-view]] + [syng-im.utils.phone-number :refer [valid-mobile-number?]] [syng-im.utils.utils :refer [log toast http-post]] [syng-im.utils.logging :as log])) (defn phone-input-view [command] - [simple-command-input-view command {:keyboardType "phone-pad"}]) + [simple-command-input-view command {:keyboardType "phone-pad"} + :validator (fn [message] + (valid-mobile-number? message))]) diff --git a/src/syng_im/components/chat/input/simple_command.cljs b/src/syng_im/components/chat/input/simple_command.cljs index f80c4ce73c..82c95f31c1 100644 --- a/src/syng_im/components/chat/input/simple_command.cljs +++ b/src/syng_im/components/chat/input/simple_command.cljs @@ -26,10 +26,15 @@ (dispatch [:stage-command chat-id command text]) (cancel-command-input)) -(defn simple-command-input-view [command input-options] +(defn valid? [message validator] + (if validator + (validator message) + (pos? (count message)))) + +(defn simple-command-input-view [command input-options & {:keys [validator]}] (let [chat-id-atom (subscribe [:get-current-chat-id]) message-atom (subscribe [:get-chat-command-content])] - (fn [command input-options] + (fn [command input-options & {:keys [validator]}] (let [chat-id @chat-id-atom message @message-atom] [view {:style {:flexDirection "row" @@ -63,10 +68,11 @@ :onChangeText (fn [new-text] (set-input-message new-text)) :onSubmitEditing (fn [e] - (send-command chat-id command message))} + (when (valid? message validator) + (send-command chat-id command message)))} input-options) message] - (if (pos? (count message)) + (if (valid? message validator) [touchable-highlight {:on-press (fn [] (send-command chat-id command message)) :underlay-color :transparent} diff --git a/src/syng_im/utils/phone_number.cljs b/src/syng_im/utils/phone_number.cljs index 6a7481faf4..0d5a960193 100644 --- a/src/syng_im/utils/phone_number.cljs +++ b/src/syng_im/utils/phone_number.cljs @@ -7,3 +7,9 @@ (defn format-phone-number [number] (str (.getNumber (js/PhoneNumber. number country-code "international")))) + +(defn valid-mobile-number? [number] + (when (string? number) + (let [number-obj (js/PhoneNumber. number country-code "international")] + (and (.isValid number-obj) + (.isMobile number-obj)))))