diff --git a/src/clj/commiteth/routes/services.clj b/src/clj/commiteth/routes/services.clj index 35156c9..9b3e042 100644 --- a/src/clj/commiteth/routes/services.clj +++ b/src/clj/commiteth/routes/services.clj @@ -29,11 +29,6 @@ :title "commitETH API" :description "commitETH API"}}}} - (GET "/authenticated" [] - :auth-rules authenticated? - :current-user user - (ok {:user user})) - (context "/api" [] (POST "/user/address" [] :auth-rules authenticated? @@ -42,4 +37,8 @@ (let [result (users/update-user-address user address)] (if (= 1 result) (ok) - (internal-server-error)))))) + (internal-server-error)))) + (GET "/user" [] + :auth-rules authenticated? + :current-user user + (ok {:user (users/get-user (:login user))})))) diff --git a/src/cljs/commiteth/common.cljs b/src/cljs/commiteth/common.cljs index 7665ea1..950a0a7 100644 --- a/src/cljs/commiteth/common.cljs +++ b/src/cljs/commiteth/common.cljs @@ -3,10 +3,11 @@ [re-frame.core :as rf])) (defn input [{:keys [value-path]}] - (let [val (reagent/atom nil) - save #(let [v (-> @val str clojure.string/trim)] - (when (seq v) - (rf/dispatch [:assoc-in value-path v])))] + (let [init-val @(rf/subscribe [:get-in value-path]) + val (reagent/atom init-val) + save #(let [v (-> @val str clojure.string/trim)] + (when (seq v) + (rf/dispatch [:assoc-in value-path v])))] (fn [props] [:input.form-control (merge props {:type "text" diff --git a/src/cljs/commiteth/db.cljs b/src/cljs/commiteth/db.cljs index b797f5e..d2da957 100644 --- a/src/cljs/commiteth/db.cljs +++ b/src/cljs/commiteth/db.cljs @@ -1,6 +1,6 @@ (ns commiteth.db) (def default-db - {:page :home - :user nil - :address nil}) + {:page :home + :user nil + :user-profile nil}) diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index 13d9c84..a2d1e6e 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -21,12 +21,19 @@ (reg-event-db :set-active-user (fn [db [_ user]] + (dispatch [:load-user-profile]) (assoc db :user user))) (reg-event-db - :set-user-address - (fn [db [_ address]] - (assoc db :address address))) + :load-user-profile + (GET "/api/user" + {:headers {"Accept" "application/transit+json"} + :handler #(dispatch [:set-user-profile %])})) + +(reg-event-db + :set-user-profile + (fn [db [_ user-profile]] + (assoc db :user-profile user-profile))) (defn save-user-address [params] (POST "/api/user/address" diff --git a/src/cljs/commiteth/profile/page.cljs b/src/cljs/commiteth/profile/page.cljs index 724fb74..07f70d3 100644 --- a/src/cljs/commiteth/profile/page.cljs +++ b/src/cljs/commiteth/profile/page.cljs @@ -1,6 +1,7 @@ (ns commiteth.profile.page (:require [re-frame.core :as rf] - [commiteth.common :refer [input]])) + [commiteth.common :refer [input]] + [commiteth.subscriptions :refer [user-address-path]])) (defn save-address [login address] @@ -10,12 +11,12 @@ (defn address-settings [] (let [user (rf/subscribe [:user]) login (:login @user) - address (rf/subscribe [:address])] + address (rf/subscribe [:get-in user-address-path])] (fn [] [:div.form-group [:label "Address"] [input {:placeholder "Address" - :value-path [:address]}] + :value-path user-address-path}] [:button.btn.btn-primary.btn-lg {:on-click (save-address login @address)} "Save"]]))) diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index 0d527a8..67c484e 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -12,6 +12,8 @@ (:user db))) (reg-sub - :address - (fn [db _] - (:address db))) + :get-in + (fn [db [_ path] ] + (get-in db path))) + +(def user-address-path [:user-profile :user :address])