       (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)))
 

   ;; 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]))

;; -------------------------------------------------------------

(register-handler-for
 :set-page
 (fn [db value]
   (let [navigator (get-in db [:services :navigator])]
     (.push navigator (clj->js {:index (-> navigator
                                           (.getCurrentRoutes)
                                           (.-length)
                                           (+ 1))
                                :name (str value)})))
   (assoc-in db [:page] value)))

(register-handler-for
 :navigate/back
 (fn [db _]
   (let [navigator (get-in db [:services :navigator])]
     (if (< 1 (-> navigator
                  (.getCurrentRoutes)
                  (.-length)))
       (do (.pop navigator)
           (assoc-in db [:page] (-> navigator
                                    (.getCurrentRoutes)
                                    (js->clj :keywordize-keys true)
                                    (butlast)
                                    (last)
                                    (:name)
                                    (str/split #":")
                                    (last)
                                    (keyword))))
       db))))

;; DB handlers
;; -------------------------------------------------------------

(register-handler-for
 :set-db
 (fn [db new-state]
   (if-let [problems (s/check schema new-state)]
     (do
       (log problems)
       db)
     new-state)))


(register-handler-for
 :storage-error
 (fn [db err-event error]
   (log error)
   ;; currently do nothing error handling should be here
   db))

(register-handler-for
 :load-db
 (fn [db _]
   (db/load #(dispatch [:set-db %1]) #(dispatch [:storage-error :load-db %1]))
   db))

(register-handler-for
 :save-db
 (fn [db _]
   (println db)
   (let [db-filtered (apply dissoc db db-ignored-keys)]
     (db/save! db-filtered #(dispatch [:storage-error :save-db %1]))
     db)))

;; db handlers
;; -------------------------------------------------------------

;; (register-handler-for
;;   :logout
;;   (fn [db _]
;;     (api/signout
;;       :on-success (fn []))
;;     (auth/set-user! db nil)
;;     (dispatch [:save-db])
;;     (dispatch [:set-page :login])
;;     (-> app-db
;;       (assoc :services (:services db)))))  ;; we need to preserve the instance of Navigator and Toaster

(register-handler-for
 :login
