       (fn [error]
         (log error)
         (condp = (:type error)
-           :invalid-credentials (dispatch [:logout])
-           :network-error       (do (dispatch [:set-offline true])
-                                  (dispatch [:message/send-to-fail to-user-id content]))
-           (dispatch [:unknown-error]))))
+          :invalid-credentials (dispatch [:logout])
+          :network-error       (do (dispatch [:set-offline true])
+                                   (dispatch [:message/send-to-fail to-user-id content]))
+          (dispatch [:unknown-error]))))
 
      db)))
 

(register-api-call-handler
 :begin-api-event-key  :auth/logout
 :api-call             api/signout
 :params-fn            (fn [db _]
                          {})
 :success-fn           (fn [db _ _]
                          (dispatch [:save-db])
                          (db/cache! user-pwd-cache-key nil)
                          (dispatch [:set-page :login])
                          (-> app-db
                            (assoc :services (:services db))))  ;; we need to preserve the instance of Navigator and Toaster
 :error-fn             (fn [db]
                         (-> db
                             (auth/set-error! "Logout failed"))))


(register-handler-for
  :auth-fail
  (fn [db error]
    (-> db
        (auth/set-in-progress! false)
        (auth/set-user!        nil)
        (auth/set-error!       "Authentication failed"))))

(register-handler-for
  :auth-success
  (fn [db user]
    (dispatch [:save-db])
    (when (= (:page db) "login")
      (dispatch [:set-page :inbox]))
    (-> db
        (auth/set-in-progress! false)
        (auth/set-user!        user)
        (auth/set-error!       nil))))

(register-handler-for
 :set-offline
 (fn [db mode]
   (when-let [toaster (get-in db [:services :toaster])]
            (log toaster)
            (toaster "You are currently offline" 5000))

   (assoc db :network-state mode)))


   ;; check once
   ;; Currently any way to check NetInfo does not work
   ;; This is left here as warning
   ;;(js/setTimeout
   ;;    check-nework-state
   ;;    15000)
   db))

(comment
  (re-frame.core/dispatch [:set-page :inbox])
  (re-frame.core/dispatch [:inbox/fetch])
  (re-frame.core/subscribe [:inbox/get]))
