From fa6e8c913ad3de277c024231be4fa7c083e7958a Mon Sep 17 00:00:00 2001 From: virvar Date: Tue, 8 Mar 2016 17:45:24 +0300 Subject: [PATCH] Send request to server --- README.md | 3 +++ src/messenger/android/login.cljs | 18 +++++++++++------ src/messenger/android/sign_up_confirm.cljs | 16 ++++++++++----- src/messenger/android/utils.cljs | 23 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index aeef024ab7..5bee2ef202 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ switching between android device https://github.com/drapanjanas/re-natal#switching-between-android-devices + +Change `server-address` value at `/src/messenger/android/utils.cljs` + ## Issues `Requiring unknown module "react"` diff --git a/src/messenger/android/login.cljs b/src/messenger/android/login.cljs index c2785beb17..edc68695d6 100644 --- a/src/messenger/android/login.cljs +++ b/src/messenger/android/login.cljs @@ -9,7 +9,7 @@ [re-natal.support :as sup] [syng-im.protocol.whisper :as whisper] [messenger.state :as state] - [messenger.android.utils :refer [log toast]] + [messenger.android.utils :refer [log toast http-post]] [messenger.android.resources :as res] [messenger.android.sign-up-confirm :refer [sign-up-confirm]])) @@ -25,11 +25,16 @@ :name "sign-up-confirm"})))) (defn sign-in [phone-number whisper-identity] - (toast (str "TODO: send number: " phone-number ", " - (subs whisper-identity 0 2) ".." - (subs whisper-identity (- (count whisper-identity) 2) - (count whisper-identity)))) - (show-confirm-view)) + ;; (toast (str "TODO: send number: " phone-number ", " + ;; (subs whisper-identity 0 2) ".." + ;; (subs whisper-identity (- (count whisper-identity) 2) + ;; (count whisper-identity)))) + (http-post "sign-up" + {:phone-number phone-number + :whisper-identity whisper-identity} + (fn [body] + (log body) + (show-confirm-view)))) (defn identity-handler [error result] (if error @@ -42,6 +47,7 @@ (let [web3 (whisper/make-web3 ethereum-rpc-url)] (str (.newIdentity (whisper/whisper web3) handler)))) + (defn get-whisper-identity-handler [phone-number] (fn [identity] ;; TODO to test newIdentity. Change to 'identity' to use saved identity. diff --git a/src/messenger/android/sign_up_confirm.cljs b/src/messenger/android/sign_up_confirm.cljs index 67692cbc5e..a288dc7a2d 100644 --- a/src/messenger/android/sign_up_confirm.cljs +++ b/src/messenger/android/sign_up_confirm.cljs @@ -10,7 +10,7 @@ [re-natal.support :as sup] [syng-im.protocol.whisper :as whisper] [messenger.state :as state] - [messenger.android.utils :refer [log toast]] + [messenger.android.utils :refer [log toast http-post]] [messenger.android.resources :as res] [messenger.android.contacts-list :refer [contacts-list]])) @@ -27,16 +27,22 @@ (defn send-check-contacts [] ) -(defn handle-send-code-response [response] - ) +(defn handle-send-code-response [body] + (log body) + (toast (if (:confirmed body) + "Confirmed" + "Wrong code")) + (when (:confirmed body) + (show-home-view))) (defn code-valid? [code] (= 4 (count code))) (defn send-code [code] (when (code-valid? code) - (toast (str code)) - (show-home-view))) + (http-post "sign-up-confirm" + {:code code} + handle-send-code-response))) (defn update-code [value] (let [formatted value] diff --git a/src/messenger/android/utils.cljs b/src/messenger/android/utils.cljs index 5ac16311d4..ba3c27f222 100644 --- a/src/messenger/android/utils.cljs +++ b/src/messenger/android/utils.cljs @@ -4,8 +4,31 @@ [natal-shell.alert :refer [alert]] [natal-shell.toast-android :as toast])) +(def server-address "http://10.0.3.2:3000/") + (defn log [obj] (.log js/console obj)) (defn toast [s] (toast/show s (toast/long))) + +(defn http-post + ([action data on-success] + (http-post action data on-success nil)) + ([action data on-success on-error] + (-> (.fetch js/window + (str server-address action) + (clj->js {:method "POST" + :headers {:accept "application/json" + :content-type "application/json"} + :body (.stringify js/JSON (clj->js data))})) + (.then (fn [response] + (log response) + (.text response))) + (.then (fn [text] + (let [json (.parse js/JSON text) + obj (js->clj json :keywordize-keys true)] + (on-success obj)))) + (.catch (or on-error + (fn [error] + (toast (str error))))))))