2016-05-19 18:31:56 +02:00
|
|
|
(ns status-im.utils.utils
|
2016-10-19 15:22:05 +03:00
|
|
|
(:require [status-im.constants :as const]
|
|
|
|
[reagent.core :as r]))
|
2016-03-27 17:59:03 +03:00
|
|
|
|
2016-05-29 21:14:34 +03:00
|
|
|
(defn require [module]
|
|
|
|
(if (exists? js/window)
|
|
|
|
(js/require module)
|
|
|
|
#js {}))
|
|
|
|
|
2016-03-27 17:59:03 +03:00
|
|
|
(defn log [obj]
|
|
|
|
(.log js/console obj))
|
|
|
|
|
2016-10-24 23:46:06 +03:00
|
|
|
(def react-native (js/require "react-native"))
|
2016-03-27 17:59:03 +03:00
|
|
|
|
2016-09-16 18:30:19 +03:00
|
|
|
(defn show-popup [title content]
|
|
|
|
(.alert (.-Alert react-native)
|
|
|
|
title
|
|
|
|
content))
|
2016-03-27 17:59:03 +03:00
|
|
|
|
|
|
|
(defn http-post
|
|
|
|
([action data on-success]
|
|
|
|
(http-post action data on-success nil))
|
|
|
|
([action data on-success on-error]
|
|
|
|
(-> (.fetch js/window
|
2016-03-28 15:14:57 +03:00
|
|
|
(str const/server-address action)
|
2016-03-27 17:59:03 +03:00
|
|
|
(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]
|
2016-09-16 18:30:19 +03:00
|
|
|
(show-popup "Error" (str error))))))))
|
2016-04-14 12:49:50 +03:00
|
|
|
|
|
|
|
(defn http-get
|
2016-06-08 15:14:35 +03:00
|
|
|
([url on-success on-error]
|
|
|
|
(-> (.fetch js/window url (clj->js {:method "GET"}))
|
2016-04-14 12:49:50 +03:00
|
|
|
(.then (fn [response]
|
|
|
|
(log response)
|
|
|
|
(.text response)))
|
2016-04-26 12:42:08 +03:00
|
|
|
(.then on-success)
|
2016-04-14 12:49:50 +03:00
|
|
|
(.catch (or on-error
|
|
|
|
(fn [error]
|
2016-09-16 18:30:19 +03:00
|
|
|
(show-popup "Error" (str error))))))))
|
2016-05-20 16:36:00 +03:00
|
|
|
|
|
|
|
(defn truncate-str [s max]
|
2016-07-18 10:38:24 +03:00
|
|
|
(if (and (< max (count s)) s)
|
2016-05-20 16:36:00 +03:00
|
|
|
(str (subs s 0 (- max 3)) "...")
|
|
|
|
s))
|
2016-08-04 18:36:13 +03:00
|
|
|
|
|
|
|
(defn first-index
|
|
|
|
[cond coll]
|
|
|
|
(loop [index 0
|
|
|
|
cond cond
|
|
|
|
coll coll]
|
|
|
|
(when (seq coll)
|
|
|
|
(if (cond (first coll))
|
|
|
|
index
|
|
|
|
(recur (inc index) cond (next coll))))))
|
2016-10-19 15:22:05 +03:00
|
|
|
|
|
|
|
(defn get-react-property [name]
|
|
|
|
(aget react-native name))
|
|
|
|
|
|
|
|
(defn adapt-class [class]
|
|
|
|
(when class
|
|
|
|
(r/adapt-react-class class)))
|
|
|
|
|
|
|
|
(defn get-class [name]
|
|
|
|
(adapt-class (get-react-property name)))
|