2016-05-19 16:31:56 +00:00
|
|
|
(ns status-im.utils.utils
|
2016-10-19 12:22:05 +00:00
|
|
|
(:require [status-im.constants :as const]
|
2017-09-05 13:14:27 +00:00
|
|
|
[status-im.i18n :as i18n]
|
2018-01-09 02:36:48 +00:00
|
|
|
[clojure.string :as str]
|
2017-07-16 09:04:35 +00:00
|
|
|
[status-im.react-native.js-dependencies :as rn-dependencies]))
|
2016-03-27 14:59:03 +00:00
|
|
|
|
2018-01-09 02:36:48 +00:00
|
|
|
;; Default HTTP request timeout ms
|
|
|
|
(def http-request-default-timeout-ms 3000)
|
|
|
|
|
2016-09-16 15:30:19 +00:00
|
|
|
(defn show-popup [title content]
|
2017-07-16 09:04:35 +00:00
|
|
|
(.alert (.-Alert rn-dependencies/react-native)
|
2016-09-16 15:30:19 +00:00
|
|
|
title
|
|
|
|
content))
|
2016-03-27 14:59:03 +00:00
|
|
|
|
2017-05-12 22:44:17 +00:00
|
|
|
(defn show-confirmation
|
|
|
|
([title content on-accept]
|
|
|
|
(show-confirmation title content nil on-accept))
|
2017-10-05 11:23:25 +00:00
|
|
|
([title content confirm-button-text on-accept]
|
|
|
|
(show-confirmation title content confirm-button-text on-accept nil))
|
|
|
|
([title content confirm-button-text on-accept on-cancel]
|
2017-07-16 09:04:35 +00:00
|
|
|
(.alert (.-Alert rn-dependencies/react-native)
|
2017-05-12 22:44:17 +00:00
|
|
|
title
|
|
|
|
content
|
2017-07-20 11:59:57 +00:00
|
|
|
;; Styles are only relevant on iOS. On Android first button is 'neutral' and second is 'positive'
|
2017-05-12 22:44:17 +00:00
|
|
|
(clj->js
|
2018-03-04 02:56:39 +00:00
|
|
|
(vector (merge {:text (i18n/label :t/cancel)
|
|
|
|
:style "cancel"
|
|
|
|
:accessibility-label :cancel-button}
|
2017-07-20 11:59:57 +00:00
|
|
|
(when on-cancel {:onPress on-cancel}))
|
2018-03-04 02:56:39 +00:00
|
|
|
{:text (or confirm-button-text "OK")
|
|
|
|
:onPress on-accept
|
|
|
|
:style "destructive"
|
|
|
|
:accessibility-label :confirm-button})))))
|
2017-05-12 22:44:17 +00:00
|
|
|
|
2017-09-27 10:40:50 +00:00
|
|
|
(defn show-question
|
|
|
|
([title content on-accept]
|
|
|
|
(show-question title content on-accept nil))
|
|
|
|
([title content on-accept on-cancel]
|
|
|
|
(.alert (.-Alert rn-dependencies/react-native)
|
|
|
|
title
|
|
|
|
content
|
|
|
|
(clj->js
|
2018-03-04 02:56:39 +00:00
|
|
|
(vector (merge {:text (i18n/label :t/no)
|
|
|
|
:accessibility-label :no-button}
|
2017-10-25 15:33:46 +00:00
|
|
|
(when on-cancel {:onPress on-cancel}))
|
2018-03-04 02:56:39 +00:00
|
|
|
{:text (i18n/label :t/yes)
|
|
|
|
:onPress on-accept
|
|
|
|
:accessibility-label :yes-button})))))
|
2017-09-27 10:40:50 +00:00
|
|
|
|
2016-03-27 14:59:03 +00:00
|
|
|
(defn http-post
|
2018-01-09 02:36:48 +00:00
|
|
|
"Performs an HTTP POST request"
|
2016-03-27 14:59:03 +00:00
|
|
|
([action data on-success]
|
|
|
|
(http-post action data on-success nil))
|
|
|
|
([action data on-success on-error]
|
2018-01-09 02:36:48 +00:00
|
|
|
(http-post action data on-success on-error nil))
|
|
|
|
([action data on-success on-error {:keys [timeout-ms] :as opts}]
|
|
|
|
(-> (rn-dependencies/fetch (str const/server-address action)
|
|
|
|
(clj->js {:method "POST"
|
|
|
|
:headers {:accept "application/json"
|
|
|
|
:content-type "application/json"}
|
|
|
|
:body (.stringify js/JSON (clj->js data))
|
|
|
|
:timeout (or timeout-ms http-request-default-timeout-ms)}))
|
2016-03-27 14:59:03 +00:00
|
|
|
(.then (fn [response]
|
|
|
|
(.text response)))
|
|
|
|
(.then (fn [text]
|
|
|
|
(let [json (.parse js/JSON text)
|
2018-01-09 02:36:48 +00:00
|
|
|
obj (js->clj json :keywordize-keys true)]
|
2016-03-27 14:59:03 +00:00
|
|
|
(on-success obj))))
|
|
|
|
(.catch (or on-error
|
|
|
|
(fn [error]
|
2016-09-16 15:30:19 +00:00
|
|
|
(show-popup "Error" (str error))))))))
|
2016-04-14 09:49:50 +00:00
|
|
|
|
|
|
|
(defn http-get
|
2018-01-09 02:36:48 +00:00
|
|
|
"Performs an HTTP GET request"
|
2016-06-08 12:14:35 +00:00
|
|
|
([url on-success on-error]
|
2018-01-09 02:36:48 +00:00
|
|
|
(http-get url on-success on-error nil))
|
|
|
|
([url on-success on-error {:keys [valid-response? timeout-ms] :as opts}]
|
|
|
|
(-> (rn-dependencies/fetch url
|
|
|
|
(clj->js {:method "GET"
|
|
|
|
:headers {"Cache-Control" "no-cache"}
|
|
|
|
:timeout (or timeout-ms http-request-default-timeout-ms)}))
|
2016-04-14 09:49:50 +00:00
|
|
|
(.then (fn [response]
|
2017-03-08 13:53:21 +00:00
|
|
|
(let [ok? (.-ok response)
|
|
|
|
ok?' (if valid-response?
|
|
|
|
(and ok? (valid-response? response))
|
|
|
|
ok?)]
|
2017-04-13 09:37:03 +00:00
|
|
|
[(.-_bodyText response) ok?'])))
|
2017-03-07 10:36:13 +00:00
|
|
|
(.then (fn [[response ok?]]
|
|
|
|
(cond
|
|
|
|
ok? (on-success response)
|
|
|
|
|
|
|
|
(and on-error (not ok?))
|
|
|
|
(on-error response)
|
|
|
|
|
|
|
|
:else false)))
|
2016-04-14 09:49:50 +00:00
|
|
|
(.catch (or on-error
|
|
|
|
(fn [error]
|
2016-09-16 15:30:19 +00:00
|
|
|
(show-popup "Error" (str error))))))))
|
2018-01-18 09:27:11 +00:00
|
|
|
|
2017-12-30 04:35:25 +00:00
|
|
|
;; background-timer
|
|
|
|
|
|
|
|
(defn set-timeout [cb ms]
|
|
|
|
(.setTimeout rn-dependencies/background-timer cb ms))
|
|
|
|
|
|
|
|
(defn clear-timeout [id]
|
|
|
|
(.clearTimeout rn-dependencies/background-timer id))
|
|
|
|
|
|
|
|
(defn set-interval [cb ms]
|
|
|
|
(.setInterval rn-dependencies/background-timer cb ms))
|
|
|
|
|
|
|
|
(defn clear-interval [id]
|
|
|
|
(.clearInterval rn-dependencies/background-timer id))
|