Skip to content

Instantly share code, notes, and snippets.

@qleguennec
Created September 7, 2017 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qleguennec/5887b570de6e675c3235ec7db2dcbe23 to your computer and use it in GitHub Desktop.
Save qleguennec/5887b570de6e675c3235ec7db2dcbe23 to your computer and use it in GitHub Desktop.
;; event-db
(rf/reg-event-db
:auth/ok
(fn [db [_ resp]]
(assoc db :token (:access_token resp))))
;; event-fx
(rf/reg-event-fx
:auth
(fn [{:keys [db]} _]
{:POST
{:url "https://api.intra.42.fr/oauth/token"
:on-success [:auth/ok]
:on-fail [:auth/nok]
:params
{:grant_type "client_credentials"}}}))
;; effect
(rf/reg-fx
:POST
(fn [{:keys [url params on-success on-fail]}]
(take! (http/post url {:form-params params})
#(rf/dispatch (conj on-success %)))))
;; subscription
(rf/reg-sub
:token
(fn [db]
(:token db)))
;; component
(defn main-panel []
(let [name (rf/subscribe [:name])
token (rf/subscribe [:token])]
(prn @token)
[:div {:class "main-panel"}
[:h1 {:class "title"} @name]
[text-input "filter projects: " #(rf/dispatch [:text-input %])]
[project-list]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment