(ns auto-ap.views.pages.company.yodlee2.form
  (:require
   [auto-ap.views.components.modal :as modal]
   [re-frame.core :as re-frame]
   [reagent.core :as r]))

(def form
  (r/create-class {
                   :component-did-mount (fn [this]
                                          (let [{:keys [authentication provider-account-id]} (r/props this)]
                                            (try 
                                              (println (js/parseInt  provider-account-id) (:url authentication) (:token authentication))
                                              (.open (.-fastlink js/window)
                                                     #js {"fastLinkURL" (:url authentication)
                                                          "accessToken" (:token authentication)
                                                          "params"      #js {"configName"       "Aggregation"
                                                                             "providerAccountId" (js/parseInt  provider-account-id)
                                                                             "flow"              "edit"}}
                                                     "fa-modal-spot")
                                              (catch js/Error e
                                                (println e)))))

                   :reagent-render (fn [_]
                                     [:div#fa-modal-spot])}))



(re-frame/reg-event-fx
  ::reauthenticate-start
  (fn [_ [_ provider-account-id authentication]]
    {:dispatch [::modal/modal-requested {:title   (str "Reauthenticate " provider-account-id)
                                         :body    [form {:provider-account-id provider-account-id
                                                         :authentication authentication}]
                                         :cancel? false}]}))


