 (ns memento.core
   (:require [ajax.core :refer [GET POST PUT]]
             [clojure.string :refer [trim split]]
+            [reagent.cookies :as cookies]
             [reagent.core :as reagent :refer [atom]]
             [re-frame.core :refer [dispatch register-sub register-handler subscribe dispatch-sync]]
             [goog.events :as events]

(register-handler
  :initialize
  (fn [app-state _]
    (dispatch [:set-token (get-cookie :token)])
    (merge app-state {:ui-state {:is-busy?      false
                                 :section       :login
                                 :current-query ""

  (fn [app-state [_ token]]
    (if (not-empty token)
      (dispatch [:set-message ""]))
    (cookies/set! :token token)
    (-> app-state
        (assoc-in [:credentials :token] token)
        (assoc-in [:ui-state :section] (if (empty? token) :login :write))
