From 7632de08cfb67b72dffd881e0fba5d099e7726c2 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 25 Jun 2016 16:28:45 +0300 Subject: [PATCH] handle responses from jail --- resources/commands.js | 2 +- resources/status.js | 4 ++ src/status_im/commands/handlers/jail.cljs | 66 ++++++++++---------- src/status_im/commands/handlers/loading.cljs | 5 +- src/status_im/commands/utils.cljs | 3 +- src/status_im/components/jail.cljs | 11 +++- src/status_im/utils/types.cljs | 5 +- 7 files changed, 53 insertions(+), 43 deletions(-) diff --git a/resources/commands.js b/resources/commands.js index 47bd9909d4..94c631b979 100644 --- a/resources/commands.js +++ b/resources/commands.js @@ -124,7 +124,7 @@ status.response({ status.command({ name: "help", description: "Help", - color: "#9a5dcf", + color: "#7099e6", params: [{ name: "query", type: status.types.STRING diff --git a/resources/status.js b/resources/status.js index c25535b159..924decbf47 100644 --- a/resources/status.js +++ b/resources/status.js @@ -53,6 +53,10 @@ function call(pathStr, paramsStr) { _status_catalog ); + if(!fn) { + return null; + } + res = fn(params); return JSON.stringify(res); diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index f7fd12067a..ce2f8be32b 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -4,15 +4,14 @@ [status-im.utils.handlers :as u] [status-im.utils.utils :refer [http-get toast]] [status-im.components.jail :as j] - - [status-im.commands.utils :refer [json->cljs generate-hiccup - reg-handler]])) + [status-im.utils.types :refer [json->clj]] + [status-im.commands.utils :refer [generate-hiccup reg-handler]] + [clojure.string :as s])) (defn init-render-command! [_ [chat-id command message-id data]] (j/call chat-id [command :render] data - (fn [res] - (dispatch [::render-command chat-id message-id (json->cljs res)])))) + #(dispatch [::render-command chat-id message-id %]))) (defn render-command [db [chat-id message-id markup]] @@ -26,30 +25,19 @@ (def regular-events {}) -(defn print-error! [error] - (toast error) - (println error)) - (defn command-hadler! - [_ [{:keys [to] :as command} response]] - (let [{:keys [error result]} (json->cljs response)] - (if error - (let [m (str "Error on command handling!\n" command error)] - (print-error! m)) - (let [{:keys [event params]} result - events (if (= "console" to) - (merge regular-events console-events) - regular-events)] - (when-let [handler (events (keyword event))] - (apply handler params)))))) + [_ [{:keys [to]} {:keys [result]} ]] + (when result + (let [{:keys [event params]} result + events (if (= "console" to) + (merge regular-events console-events) + regular-events)] + (when-let [handler (events (keyword event))] + (apply handler params))))) (defn suggestions-handler! - [db [{:keys [chat-id]} response-json]] - (let [{:keys [error result]} (json->cljs response-json)] - (when error - (let [m (str "Error on param suggestions!\n" error)] - (print-error! m))) - (assoc-in db [:suggestions chat-id] (generate-hiccup result)))) + [db [{:keys [chat-id]} {:keys [result]} ]] + (assoc-in db [:suggestions chat-id] (generate-hiccup result))) (defn suggestions-events-handler! [db [[n data]]] @@ -59,23 +47,35 @@ nil)) (defn command-preview - [db [chat-id response-json]] - (if-let [response (json->cljs response-json)] + [db [chat-id {:keys [result]}]] + (println :wuuut) + (if result (let [path [:chats chat-id :staged-commands] commands-cnt (count (get-in db path))] ;; todo (dec commands-cnt) looks like hack have to find better way to ;; do this (update-in db (conj path (dec commands-cnt)) assoc - :preview (generate-hiccup response) - :preview-string (str response))) + :preview (generate-hiccup result) + :preview-string (str result))) db)) +(defn print-error-message! [message] + (fn [_ params] + (when (:error (last params)) + (toast (s/join "\n" [message params])) + (println message params)))) + (reg-handler :init-render-command! init-render-command!) (reg-handler ::render-command render-command) -(reg-handler :command-handler! (u/side-effect! command-hadler!)) +(reg-handler :command-handler! + (after (print-error-message! "Error on command handling")) + (u/side-effect! command-hadler!)) (reg-handler :suggestions-handler - (after #(dispatch [:animate-show-response])) + [(after #(dispatch [:animate-show-response])) + (after (print-error-message! "Error on param suggestions"))] suggestions-handler!) (reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!)) -(reg-handler :command-preview command-preview) +(reg-handler :command-preview + (after (print-error-message! "Error on command preview")) + command-preview) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index d65bf21ec0..74b08e7d86 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -6,7 +6,8 @@ [clojure.string :as s] [status-im.persistence.realm :as realm] [status-im.components.jail :as j] - [status-im.commands.utils :refer [json->cljs reg-handler]])) + [status-im.utils.types :refer [json->clj]] + [status-im.commands.utils :refer [reg-handler]])) (def commands-js "commands.js") @@ -44,7 +45,7 @@ (defn parse-commands! [_ [identity file]] (j/parse identity file (fn [result] - (let [{:keys [error result]} (json->cljs result)] + (let [{:keys [error result]} (json->clj result)] (if error (dispatch [::loading-failed! identity ::error-in-jail error]) (dispatch [::add-commands identity file result])))))) diff --git a/src/status_im/commands/utils.cljs b/src/status_im/commands/utils.cljs index 1c07a5fe8e..8ae48bf0e7 100644 --- a/src/status_im/commands/utils.cljs +++ b/src/status_im/commands/utils.cljs @@ -5,12 +5,11 @@ image touchable-highlight]] [re-frame.core :refer [register-handler dispatch trim-v debug]])) -(defn json->cljs [json] +(defn json->clj [json] (if (= json "undefined") nil (js->clj (.parse js/JSON json) :keywordize-keys true))) - (def elements {:text text :view view diff --git a/src/status_im/components/jail.cljs b/src/status_im/components/jail.cljs index 758d620706..daf278f4f8 100644 --- a/src/status_im/components/jail.cljs +++ b/src/status_im/components/jail.cljs @@ -1,6 +1,7 @@ (ns status-im.components.jail (:require-macros [status-im.utils.slurp :refer [slurp]]) - (:require [status-im.components.react :as r])) + (:require [status-im.components.react :as r] + [status-im.utils.types :as t])) (def status-js (slurp "resources/status.js")) @@ -18,5 +19,9 @@ (.stringify js/JSON (clj->js data))) (defn call [chat-id path params callback] - ;(println :call chat-id (cljs->json path) (cljs->json params)) - (.call jail chat-id (cljs->json path) (cljs->json params) callback)) + (println :call chat-id (cljs->json path) (cljs->json params)) + (let [cb (fn [r] + (let [r' (t/json->clj r)] + (println r') + (callback r')))] + (.call jail chat-id (cljs->json path) (cljs->json params) cb))) diff --git a/src/status_im/utils/types.cljs b/src/status_im/utils/types.cljs index 93c942df77..d30d09804b 100644 --- a/src/status_im/utils/types.cljs +++ b/src/status_im/utils/types.cljs @@ -11,5 +11,6 @@ (defn clj->json [data] (.stringify js/JSON (clj->js data))) -(defn json->clj [data] - (js->clj (.parse js/JSON data) :keywordize-keys true)) +(defn json->clj [json] + (when-not (= json "undefined") + (js->clj (.parse js/JSON json) :keywordize-keys true)))