From 656f7ce84a43277bd9520c52be647c0b6bd99ac3 Mon Sep 17 00:00:00 2001 From: kagel Date: Thu, 25 Aug 2016 02:58:55 +0300 Subject: [PATCH] Embrace re-frame 0.8.0 effects --- src/cljs/commiteth/handlers.cljs | 89 +++++++++++++++++--------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index c52813a..6281585 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -1,8 +1,16 @@ (ns commiteth.handlers (:require [commiteth.db :as db] - [re-frame.core :refer [dispatch reg-event-db]] + [re-frame.core :refer [dispatch reg-event-db reg-event-fx reg-fx]] [ajax.core :refer [GET POST]])) +(reg-fx + :http + (fn [{:keys [method url on-success params]}] + (method url + {:headers {"Accept" "application/transit+json"} + :handler on-success + :params params}))) + (reg-event-db :initialize-db (fn [_ _] @@ -18,21 +26,21 @@ (fn [db [_ page]] (assoc db :page page))) -(reg-event-db +(reg-event-fx :set-active-user - (fn [db [_ user]] - (dispatch [:load-user-profile]) - (dispatch [:load-user-repos]) - (dispatch [:load-enabled-repos]) - (assoc db :user user))) + (fn [{:keys [db]} [_ user]] + {:db (assoc db :user user) + :dispatch-n [[:load-user-profile] + [:load-user-repos] + [:load-enabled-repos]]})) -(reg-event-db +(reg-event-fx :load-user-profile - (fn [db [_]] - (GET "/api/user" - {:headers {"Accept" "application/transit+json"} - :handler #(dispatch [:set-user-profile %])}) - db)) + (fn [{:keys [db]} [_]] + {:db db + :http {:method GET + :url "/api/user" + :on-success #(dispatch [:set-user-profile %])}})) (reg-event-db :set-user-profile @@ -44,44 +52,41 @@ (fn [db [_ repos]] (assoc db :repos repos))) -(reg-event-db +(reg-event-fx :load-user-repos - (fn [db [_]] - (GET "/api/user/repositories" - {:headers {"Accept" "application/transit+json"} - :handler #(dispatch [:set-user-repos (:repositories %)])}) - db)) + (fn [{:keys [db]} [_]] + {:db db + :http {:method GET + :url "/api/user/repositories" + :on-success #(dispatch [:set-user-repos (:repositories %)])}})) (reg-event-db :set-enabled-repos (fn [db [_ repos]] (assoc db :enabled-repos (zipmap repos (repeat true))))) -(reg-event-db +(reg-event-fx :load-enabled-repos - (fn [db [_]] - (GET "/api/repositories" - {:headers {"Accept" "application/transit+json"} - :handler #(dispatch [:set-enabled-repos %])}) - db)) + (fn [{:keys [db]} [_]] + {:db db + :http {:method GET + :url "/api/repositories" + :on-success #(dispatch [:set-enabled-repos %])}})) -(reg-event-db +(reg-event-fx :toggle-repo - (fn [db [_ repo]] - (POST "/api/repository/toggle" - {:headers {"Accept" "application/transit+json"} - :params (select-keys repo [:id :login :name]) - :handler #(println %)}) - db)) + (fn [{:keys [db]} [_ repo]] + {:db db + :http {:method POST + :url "/api/repository/toggle" + :on-success #(println %) + :params (select-keys repo [:id :login :name])}})) -(defn save-user-address [params] - (POST "/api/user/address" - {:headers {"Accept" "application/transit+json"} - :params params - :handler #(println %)})) - -(reg-event-db +(reg-event-fx :save-user-address - (fn [db [_ user address]] - (save-user-address {:user user :address address}) - db)) + (fn [{:keys [db]} [_ user address]] + {:db db + :http {:method POST + :url "/api/user/address" + :on-success #(println %) + :params {:user user :address address}}}))