-
-
Save olivergeorge/86b1626b7241018d7439462fc4349a77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns app.fx | |
(:require [re-frame.core :as rf] | |
[interop.redmap-api :as redmap-api] | |
[interop.sentry :as sentry] | |
[interop.react-native-keychain :as keychain] | |
[react-native-splash-screen] | |
[interop.promise :as promise] | |
[app.impl.user2 :as user2])) | |
(defn dispatchify | |
[{:keys [cb resolve reject finally] :as m}] | |
(cond-> m | |
(vector? cb) (assoc :cb #(rf/dispatch (conj cb %))) | |
(vector? resolve) (assoc :resolve #(rf/dispatch (conj resolve %))) | |
(vector? reject) (assoc :reject #(rf/dispatch (conj reject %))) | |
(vector? finally) (assoc :finally #(rf/dispatch (conj finally %))))) | |
(defn promise-fx | |
[f] | |
(fn [fx-args] | |
(let [{:keys [resolve reject finally]} (dispatchify fx-args) | |
args (dissoc fx-args :resolve :reject :finally) | |
reject (or reject sentry/capture-exception)] | |
(promise/time | |
(cond-> (f args) | |
resolve (.then resolve) | |
reject (.catch reject) | |
finally (.finally finally)))))) | |
(defn user-auth | |
[m] | |
(let [*token (atom nil) | |
*payload (atom nil)] | |
(-> (redmap-api/api-token-auth m) | |
(promise/unavailable) | |
(promise/timeout 5000) | |
(.then promise/throw-anoms) | |
(.then (fn [{:strs [token]}] | |
(reset! *token token) | |
(keychain/set-token {:token @*token}))) | |
(.then (fn [] | |
(redmap-api/get-user-details-and-sightings {:token @*token}))) | |
(promise/unavailable) | |
(promise/timeout 5000) | |
(.then promise/throw-anoms) | |
(.then (fn [payload] | |
(reset! *payload payload) | |
(user2/insert-row {:payload payload}))) | |
(promise/timeout 2000) | |
(.then promise/throw-anoms) | |
(.then (fn [] | |
{"token" @*token "user" @*payload})) | |
(.catch promise/as-anom)))) | |
(rf/reg-fx ::user-auth (promise-fx user-auth)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment