 ; Handlers
 ;------------------------------
 
+(defn remove-token-on-unauth
+  "Receives an application state and an authorization result. If the status is 401, then it
+  removes the token from the applicaton state."
+  [app-state result]
+  (if (= 401 (:status result))
+    (assoc-in app-state [:credentials :token] nil)
+    app-state))
+
 (register-handler
   :initialize
   (fn [app-state _]

      (POST "/api/memory" {:params        {:thought note}
                           :headers       {:authorization (str "Token " (get-in app-state [:credentials :token]))}
                           :handler       #(dispatch [:save-note-success note])
                           :error-handler #(dispatch [:save-note-error (str "Error saving note: " %)])}))
    app-state
    ))



(register-handler
  :save-note-error
  (fn [app-state [_ result]]
    (dispatch [:set-message (str "Error saving note: " result) "alert-danger"])
    (-> app-state
        (assoc-in [:ui-state :is-busy?] false)
        (remove-token-on-unauth result))))


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