From 601465ad3922facf3467495a7ee5fcb73ff20a13 Mon Sep 17 00:00:00 2001 From: kagel Date: Tue, 23 Aug 2016 02:27:49 +0300 Subject: [PATCH] Update user address --- resources/scss/screen.scss | 24 +++++++++++ resources/sql/queries.sql | 2 +- src/clj/commiteth/db/users.clj | 5 +++ src/clj/commiteth/routes/services.clj | 49 +++++++--------------- src/cljs/commiteth/core.cljs | 58 ++++++++++++++++++++++----- src/cljs/commiteth/db.cljs | 5 ++- src/cljs/commiteth/handlers.cljs | 25 +++++++++++- src/cljs/commiteth/subscriptions.cljs | 5 +++ 8 files changed, 125 insertions(+), 48 deletions(-) diff --git a/resources/scss/screen.scss b/resources/scss/screen.scss index dbafb82..ec65959 100644 --- a/resources/scss/screen.scss +++ b/resources/scss/screen.scss @@ -4,6 +4,30 @@ html, body { padding-top: 0px; } +.app { + width: 100%; + padding: 0; + margin: 0; + max-width: 100%; +} + +.container { + padding-top: 20px; +} + +.profile-settings { + padding-top: 20px; + + .form-group { + padding-top: 20px; + } + + button { + margin-top: 20px; + } + +} + .profile-link { display: inline-block; padding-top: .425rem; diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index bf8366e..0b719c1 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -13,7 +13,7 @@ WHERE id = :id; -- :name update-user-address! :! :n UPDATE users SET address = :address -WHERE id = :id; +WHERE login = :login; -- :name get-user :? :1 -- :doc retrieve a user given the login. diff --git a/src/clj/commiteth/db/users.clj b/src/clj/commiteth/db/users.clj index df65465..0f7d6bc 100644 --- a/src/clj/commiteth/db/users.clj +++ b/src/clj/commiteth/db/users.clj @@ -24,3 +24,8 @@ [login] (jdbc/with-db-connection [con-db *db*] (some? (db/get-user con-db {:login login})))) + +(defn update-user-address + [login address] + (jdbc/with-db-connection [con-db *db*] + (db/update-user-address! con-db {:login login :address address}))) diff --git a/src/clj/commiteth/routes/services.clj b/src/clj/commiteth/routes/services.clj index 4598a80..35156c9 100644 --- a/src/clj/commiteth/routes/services.clj +++ b/src/clj/commiteth/routes/services.clj @@ -4,7 +4,8 @@ [schema.core :as s] [compojure.api.meta :refer [restructure-param]] [buddy.auth.accessrules :refer [restrict]] - [buddy.auth :refer [authenticated?]])) + [buddy.auth :refer [authenticated?]] + [commiteth.db.users :as users])) (defn access-error [_ _] (unauthorized {:error "unauthorized"})) @@ -24,43 +25,21 @@ (defapi service-routes {:swagger {:ui "/swagger-ui" :spec "/swagger.json" - :data {:info {:version "1.0.0" - :title "Sample API" - :description "Sample Services"}}}} + :data {:info {:version "0.1" + :title "commitETH API" + :description "commitETH API"}}}} (GET "/authenticated" [] :auth-rules authenticated? :current-user user (ok {:user user})) + (context "/api" [] - :tags ["thingie"] - - (GET "/plus" [] - :return Long - :query-params [x :- Long, {y :- Long 1}] - :summary "x+y with query-parameters. y defaults to 1." - (ok (+ x y))) - - (POST "/minus" [] - :return Long - :body-params [x :- Long, y :- Long] - :summary "x-y with body-parameters." - (ok (- x y))) - - (GET "/times/:x/:y" [] - :return Long - :path-params [x :- Long, y :- Long] - :summary "x*y with path-parameters" - (ok (* x y))) - - (POST "/divide" [] - :return Double - :form-params [x :- Long, y :- Long] - :summary "x/y with form-parameters" - (ok (/ x y))) - - (GET "/power" [] - :return Long - :header-params [x :- Long, y :- Long] - :summary "x^y with header-parameters" - (ok (long (Math/pow x y)))))) + (POST "/user/address" [] + :auth-rules authenticated? + :body-params [user :- String, address :- String] + :summary "Update user address" + (let [result (users/update-user-address user address)] + (if (= 1 result) + (ok) + (internal-server-error)))))) diff --git a/src/cljs/commiteth/core.cljs b/src/cljs/commiteth/core.cljs index 5e6d3e4..47d42b4 100644 --- a/src/cljs/commiteth/core.cljs +++ b/src/cljs/commiteth/core.cljs @@ -8,7 +8,8 @@ [ajax.core :refer [GET POST]] [commiteth.ajax :refer [load-interceptors!]] [commiteth.handlers] - [commiteth.subscriptions]) + [commiteth.subscriptions] + [reagent.core :as reagent]) (:import goog.History)) (defn nav-link [uri title page collapsed?] @@ -21,8 +22,9 @@ (defn login-link [] (let [user (rf/subscribe [:user])] (if-let [login (:login @user)] - [:li.pull-right - [:span.profile-link (str "Logged in as " login)] + [:li.pull-right.p + [:span.profile-link "Logged in as " + [:a {:href "/#/profile"} login]] [:a.btn.btn-primary.btn-sm {:href "/logout"} "Logout"]] [:li.pull-right [:a.btn.btn-social.btn-github @@ -43,17 +45,53 @@ [login-link]]]])) (defn home-page [] - [:div.container - [:div.jumbotron - [:h3 "Welcome to commitETH"]]]) + [:div + [:h3 "Welcome to commitETH"]]) + +(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])))] + (fn [props] + [:input.form-control + (merge props {:type "text" + :value @val + :auto-focus true + :on-blur save + :on-change #(reset! val (-> % .-target .-value))})]))) + +(defn save-address + [login address] + (fn [_] + (rf/dispatch [:save-user-address login address]))) + +(defn address-settings [] + (let [user (rf/subscribe [:user]) + login (:login @user) + address (rf/subscribe [:address])] + (fn [] + [:div.form-group + [:label "Address"] + [input {:placeholder "Address" + :value-path [:address]}] + [:button.btn.btn-primary.btn-lg + {:on-click (save-address login @address)} + "Save"]]))) + +(defn profile-page [] + [:div.profile-settings + [:h1 "Profile"] + [address-settings]]) (def pages - {:home #'home-page}) + {:home #'home-page + :profile #'profile-page}) (defn page [] - [:div + [:div.app [navbar] - [(pages @(rf/subscribe [:page]))]]) + [:div.container [(pages @(rf/subscribe [:page]))]]]) ;; ------------------------- ;; Routes @@ -61,6 +99,8 @@ (secretary/defroute "/" [] (rf/dispatch [:set-active-page :home])) +(secretary/defroute "/profile" [] + (rf/dispatch [:set-active-page :profile])) ;; ------------------------- ;; History diff --git a/src/cljs/commiteth/db.cljs b/src/cljs/commiteth/db.cljs index e4fda3e..b797f5e 100644 --- a/src/cljs/commiteth/db.cljs +++ b/src/cljs/commiteth/db.cljs @@ -1,5 +1,6 @@ (ns commiteth.db) (def default-db - {:page :home - :user nil}) + {:page :home + :user nil + :address nil}) diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index b16dbe9..13d9c84 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -1,12 +1,18 @@ (ns commiteth.handlers (:require [commiteth.db :as db] - [re-frame.core :refer [dispatch reg-event-db]])) + [re-frame.core :refer [dispatch reg-event-db]] + [ajax.core :refer [GET POST]])) (reg-event-db :initialize-db (fn [_ _] db/default-db)) +(reg-event-db + :assoc-in + (fn [db [_ path value]] + (assoc-in db path value))) + (reg-event-db :set-active-page (fn [db [_ page]] @@ -16,3 +22,20 @@ :set-active-user (fn [db [_ user]] (assoc db :user user))) + +(reg-event-db + :set-user-address + (fn [db [_ address]] + (assoc db :address address))) + +(defn save-user-address [params] + (POST "/api/user/address" + {:headers {"Accept" "application/transit+json"} + :params params + :handler #(println %)})) + +(reg-event-db + :save-user-address + (fn [db [_ user address]] + (save-user-address {:user user :address address}) + db)) diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index 9d0a9be..0d527a8 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -10,3 +10,8 @@ :user (fn [db _] (:user db))) + +(reg-sub + :address + (fn [db _] + (:address db)))