Validate phone number

Former-commit-id: 152626da18
This commit is contained in:
virvar 2016-05-09 12:26:42 +03:00
parent a077b29f9e
commit 65210893fe
3 changed files with 20 additions and 5 deletions

View File

@ -2,8 +2,11 @@
(:require (:require
[re-frame.core :refer [subscribe dispatch dispatch-sync]] [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[syng-im.components.chat.input.simple-command :refer [simple-command-input-view]] [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.utils :refer [log toast http-post]]
[syng-im.utils.logging :as log])) [syng-im.utils.logging :as log]))
(defn phone-input-view [command] (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))])

View File

@ -26,10 +26,15 @@
(dispatch [:stage-command chat-id command text]) (dispatch [:stage-command chat-id command text])
(cancel-command-input)) (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]) (let [chat-id-atom (subscribe [:get-current-chat-id])
message-atom (subscribe [:get-chat-command-content])] message-atom (subscribe [:get-chat-command-content])]
(fn [command input-options] (fn [command input-options & {:keys [validator]}]
(let [chat-id @chat-id-atom (let [chat-id @chat-id-atom
message @message-atom] message @message-atom]
[view {:style {:flexDirection "row" [view {:style {:flexDirection "row"
@ -63,10 +68,11 @@
:onChangeText (fn [new-text] :onChangeText (fn [new-text]
(set-input-message new-text)) (set-input-message new-text))
:onSubmitEditing (fn [e] :onSubmitEditing (fn [e]
(send-command chat-id command message))} (when (valid? message validator)
(send-command chat-id command message)))}
input-options) input-options)
message] message]
(if (pos? (count message)) (if (valid? message validator)
[touchable-highlight {:on-press (fn [] [touchable-highlight {:on-press (fn []
(send-command chat-id command message)) (send-command chat-id command message))
:underlay-color :transparent} :underlay-color :transparent}

View File

@ -7,3 +7,9 @@
(defn format-phone-number [number] (defn format-phone-number [number]
(str (.getNumber (js/PhoneNumber. number country-code "international")))) (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)))))